![Swing的圖形介面元件(II)課件_第1頁](http://file4.renrendoc.com/view/100c1a266b7b67b7aa30b73da37de43c/100c1a266b7b67b7aa30b73da37de43c1.gif)
![Swing的圖形介面元件(II)課件_第2頁](http://file4.renrendoc.com/view/100c1a266b7b67b7aa30b73da37de43c/100c1a266b7b67b7aa30b73da37de43c2.gif)
![Swing的圖形介面元件(II)課件_第3頁](http://file4.renrendoc.com/view/100c1a266b7b67b7aa30b73da37de43c/100c1a266b7b67b7aa30b73da37de43c3.gif)
![Swing的圖形介面元件(II)課件_第4頁](http://file4.renrendoc.com/view/100c1a266b7b67b7aa30b73da37de43c/100c1a266b7b67b7aa30b73da37de43c4.gif)
![Swing的圖形介面元件(II)課件_第5頁](http://file4.renrendoc.com/view/100c1a266b7b67b7aa30b73da37de43c/100c1a266b7b67b7aa30b73da37de43c5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
Ch13Swing的圖形介面元件(II)JAVA程式設(shè)計入門(II)2023/11/272JPopupMenu彈出式選單元件JMenuBar、JMenu與JMenuItem下拉式選單元件JToolBar工具列元件JFileChooser檔案選擇元件JColorChooser色彩選擇元件多重視窗大綱2023/11/273視窗功能表和工具列元件Swing套件提供功能強大的視窗功能表和工具列元件,可以輕鬆建立應(yīng)用程式視窗上方的下拉式功能表、工具列和彈出式選單。同樣的,視窗功能表和工具列元件也都是繼承自JComponent,其繼承架構(gòu)如下圖所示:2023/11/274JPopupMenu彈出式選單元件-說明JPopupMenu彈出式選單元件繼承自JComponent,可以建立視窗應(yīng)用程式滑鼠右鍵顯示的快顯功能表,內(nèi)含選項的JMenuItem物件或JSeparator分隔線物件,如下圖所示:2023/11/275JPopupMenu彈出式選單元件-建立物件在建立JPopupMenu物件後,使用add()方法新增選項的JMenuItem物件,addSeparator()方法可以新增選單分隔線的JSeparator物件。popup=newJPopupMenu();popup.add(blue=newJMenuItem("藍(lán)色"));popup.add(yellow=newJMenuItem("黃色"));popup.add(green=newJMenuItem("綠色"));popup.addSeparator();popup.add("紅色");2023/11/276JPopupMenu彈出式選單元件-事件處理新增MouseListener傾聽者物件且實作mousePressed()和mouseReleased()方法來顯示彈出式視窗,如下所示:publicvoidmousePressed(MouseEventevt){if(evt.isPopupTrigger())popup.show(evt.getComponent(),evt.getX(),evt.getY());}publicvoidmouseReleased(MouseEventevt){if(evt.isPopupTrigger())popup.show(evt.getComponent(),evt.getX(),evt.getY());}2023/11/277JPopupMenu建構(gòu)子與方法建構(gòu)子:JPopupMenu()JPopupMenu(String):參數(shù)String是標(biāo)題文字方法:JMenuItemadd(JMenuItem)JMenuItemadd(String)voidaddSeparator()voidinsert(Component,int)voidremove(JMenuItem)voidremove(int)voidremoveAll()voidshow(Component,int,int)2023/11/278範(fàn)例1:使用PopupMenu(1/4)建立PopupMenu的選擇項:importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;//繼承JFrame類別,實作ActionListener介面publicclassCh09_01extendsJFrameimplementsActionListener{privateJPopupMenupopup;privateJMenuItemblue,yellow,green;privateContainerc;//建構(gòu)子
publicCh09_01(){super("JPopupMenu元件範(fàn)例");c=getContentPane();c.setBackground(Color.pink);
popup=newJPopupMenu();popup.add(blue=newJMenuItem("藍(lán)色"));blue.addActionListener(this);popup.add(yellow=newJMenuItem("黃色"));yellow.addActionListener(this);popup.add(green=newJMenuItem("綠色"));green.addActionListener(this);popup.addSeparator();popup.add("紅色");//使用字串新增選項2023/11/279範(fàn)例1:使用PopupMenu(2/4)傾聽者:
addMouseListener(newMouseAdapter(){publicvoidmousePressed(MouseEventevt){if(evt.isPopupTrigger())//顯示選單
popup.show(evt.getComponent(),evt.getX(),evt.getY());}publicvoidmouseReleased(MouseEventevt){if(evt.isPopupTrigger())//顯示選單
popup.show(evt.getComponent(),evt.getX(),evt.getY());}});}2023/11/2710範(fàn)例1:使用PopupMenu(3/4)實作事件處理方法:
publicvoidactionPerformed(ActionEventevt){if(evt.getSource()==blue)c.setBackground(Color.blue);//藍(lán)色
if(evt.getSource()==yellow)c.setBackground(Color.yellow);//黃色
if(evt.getSource()==green)c.setBackground(Color.green);//綠色
repaint();//重繪
}2023/11/2711範(fàn)例1:使用PopupMenu(4/4)主程式:
publicstaticvoidmain(String[]args){//建立Swing應(yīng)用程式
Ch09_01app=newCh09_01();//關(guān)閉視窗事件,結(jié)束程式的執(zhí)行
app.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevt){System.exit(0);}});app.setSize(300,200);//設(shè)定尺寸
app.setVisible(true);//顯示視窗
}}2023/11/2712Menu屬MenuComponent的延伸類別122023/11/2713Menu屬MenuComponent的延伸類別13以「記事本」為例說明選單及其相關(guān)元件2023/11/271414Menu屬MenuComponent的延伸類別Menu位於視窗標(biāo)題列的下方使用選單時,框架(Frame)會有一個選單列(MenuBar),選單列內(nèi)有數(shù)個選單(Menu)每個Menu內(nèi)會有多個選項(MenuItem)或核選式選項(CheckboxMenuItem),選單是選項的容器選單也可以是另一個選單的容器2023/11/2715JMenuBar、JMenu與JMenuItem下拉式選單元件-說明在JFrame、JInternalFrame、JApplet和JDialog等類別的視窗新增下拉式功能表選單,類別建構(gòu)子需要使用JMenuBar、JMenu和JMenuItem物件來建立下拉式功能表的物件。2023/11/2716JMenuBar、JMenu與JMenuItem下拉式選單元件-
JMenuBar元件JMenuBar元件是視窗上方的功能表列,如下所示:JMenuBarjmb=newJMenuBar();setJMenuBar(jmb);上述程式碼建立JMenuBar物件後,預(yù)設(shè)是空的功能表列,然後使用setJMenuBar()方法新增到JFrame視窗,換句話說,目前在視窗上方已經(jīng)擁有一個空的功能表列。2023/11/2717JMenuBar、JMenu與JMenuItem下拉式選單元件-JMenu元件在建立好JMenuBar物件後,就可以新增功能表列下拉式子選單的JMenu物件,如下所示:JMenufile=newJMenu("檔案(F)");JMenuItemitem;file.add(item=newJMenuItem("新增(N)",KeyEvent.VK_N));file.add(item=newJMenuItem("開啟(O)",KeyEvent.VK_O));JMenusetting=newJMenu("參數(shù)設(shè)定");file.add(setting);file.addSeparator();file.add(item=newJMenuItem("關(guān)閉(X)",KeyEvent.VK_X));jmb.add(file);2023/11/2718JMenuBar、JMenu與JMenuItem下拉式選單元件-Item元件JMenuItem、JCheckBoxMenuItem與JRadioButtonMenuItem元件JMenuItem、JCheckBoxMenuItem與JRadioButtonMenuItem類別的建構(gòu)子可以新增選單的選項、核取方塊和選項鈕選項。2023/11/2719範(fàn)例2:建立Menu(1/5)基本宣告:/*程式範(fàn)例:Ch09_02.java*/importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;//繼承JFrame類別,實作ActionListener介面publicclassCh09_02extendsJFrameimplementsActionListener{privateJRadioButtonMenuItemred,green,blue;privateJMenuItemopenItem,newItem,exitItem,codeItem,typeItem;privateJMenusetting;privateContainerc;//建構(gòu)子
publicCh09_02(){super("JMenuBar元件範(fàn)例");c=getContentPane();c.setBackground(Color.white);JMenuBarjmb=newJMenuBar();setJMenuBar(jmb);2023/11/2720範(fàn)例2:建立Menu(2/5)建立第一個File的Menu:
JMenufile=newJMenu("檔案(F)");file.setMnemonic(KeyEvent.VK_F);openItem=newJMenuItem("新增(N)",KeyEvent.VK_N);newItem=newJMenuItem("開啟(O)",KeyEvent.VK_O);exitItem=newJMenuItem("關(guān)閉(X)",KeyEvent.VK_X);setting=newJMenu("參數(shù)設(shè)定");codeItem=newJMenuItem("編碼");typeItem=newJMenuItem("字型");
openItem.addActionListener(this);newItem.addActionListener(this);exitItem.addActionListener(this);codeItem.addActionListener(this);typeItem.addActionListener(this);
file.add(openItem);file.add(newItem);setting.add(codeItem);setting.add(typeItem);file.add(setting);file.addSeparator();//分隔線
file.add(exitItem);jmb.add(file);//新增file選單2023/11/2721範(fàn)例2:建立Menu(3/5)建立第二個Menu:
JMenuchoice=newJMenu("選項(C)");choice.setMnemonic(KeyEvent.VK_C);JCheckBoxMenuItemcheck;check=newJCheckBoxMenuItem("切換");check.addActionListener(this);choice.add(check);ButtonGroupbuttongroup=newButtonGroup();red=newJRadioButtonMenuItem("紅色");choice.add(red);buttongroup.add(red);red.addActionListener(this);green=newJRadioButtonMenuItem("綠色");choice.add(green);buttongroup.add(green);green.addActionListener(this);blue=newJRadioButtonMenuItem("藍(lán)色");choice.add(blue);buttongroup.add(blue);blue.addActionListener(this);jmb.add(choice);}2023/11/2722範(fàn)例2:建立Menu(4/5)實作事件處理方法:
publicvoidactionPerformed(ActionEventevt){if(evt.getSource()==exitItem)System.exit(0);if(evt.getSource()==red)c.setBackground(Color.red);if(evt.getSource()==green)c.setBackground(Color.green);if(evt.getSource()==blue)c.setBackground(Color.blue);repaint();//重繪
}2023/11/2723範(fàn)例2:建立Menu(5/5)主程式
publicstaticvoidmain(String[]args){//建立Swing應(yīng)用程式
Ch09_02app=newCh09_02();//關(guān)閉視窗事件,結(jié)束程式的執(zhí)行
app.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevt){System.exit(0);}});app.setSize(300,200);//設(shè)定尺寸
app.setVisible(true);//顯示視窗
}}2023/11/2724JToolBar工具列元件-說明JToolBar工具列元件繼承自JComponent類別,可以建立視窗的工具列按鈕,它也屬於一種容器元件,在建立好JToolBar物件後,就可以新增GUI元件到工具列,如下圖所示:2023/11/2725JToolBar工具列元件-建立物件程式碼在建立好JToolBar元件後,使用add()方法新增GUI元件,最後只需將JToolBar元件視為GUI元件,新增到最上層容器物件即可。JToolBartoolBar=newJToolBar();blue=newJButton(newImageIcon("blue1.gif"));yellow=newJButton(newImageIcon("yellow1.gif"));green=newJButton(newImageIcon("green1.gif"));toolBar.add(blue);toolBar.add(yellow);toolBar.add(green);2023/11/2726範(fàn)例3:建立toolbar(1/2)宣告及toolbar建立:/*程式範(fàn)例:Ch09_03.java*/importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;//繼承JFrame類別,實作ActionListener介面publicclassCh09_03extendsJFrameimplementsActionListener{privateJButtonblue,yellow,green;privateContainerc;//建構(gòu)子
publicCh09_03(){super("JToolBar元件範(fàn)例");c=getContentPane();c.setBackground(Color.white);JToolBartoolBar=newJToolBar();blue=newJButton(newImageIcon("blue.jpg"));blue.setToolTipText("藍(lán)色");blue.addActionListener(this);yellow=newJButton(newImageIcon("yellow.jpg"));yellow.setToolTipText("黃色");yellow.addActionListener(this);green=newJButton(newImageIcon("green.jpg"));green.setToolTipText("綠色");green.addActionListener(this);toolBar.add(blue);toolBar.add(yellow);toolBar.add(green);c.add(toolBar,BorderLayout.NORTH);}2023/11/2727範(fàn)例3:建立toolbar(2/2)實作事件處理方法及主程式:
publicvoidactionPerformed(ActionEventevt){if(evt.getSource()==blue)c.setBackground(Color.blue);if(evt.getSource()==yellow)c.setBackground(Color.yellow);if(evt.getSource()==green)c.setBackground(Color.green);repaint();//重繪
}//主程式
publicstaticvoidmain(String[]args){//建立Swing應(yīng)用程式
Ch09_03app=newCh09_03();//關(guān)閉視窗事件,結(jié)束程式的執(zhí)行
app.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevt){System.exit(0);}});app.setSize(300,200);//設(shè)定尺寸
app.setVisible(true);//顯示視窗
}}2023/11/2728檔案與色彩選擇元件-說明Swing套件擁有瀏覽檔案系統(tǒng)選取檔案或資料夾的JFileChooser和選取色彩的JColorChooser元件2種選擇元件,這2個元件都是繼承自JComponent,其繼承架構(gòu)如下圖所示:2023/11/2729JFileChooser檔案選擇元件-說明JFileChooser檔案選擇元件可以顯示對話方塊瀏覽檔案系統(tǒng),以便讓使用者選取檔案或資料夾。2023/11/2730JFileChooser檔案選擇元件-開啟檔案對話方塊例如:開啟或儲存指定檔案,如下所示:JFileChooserjfc=newJFileChooser();上述程式碼建立JFileChooser物件後,使用showOpenDialog()方法顯示開啟檔案對話方塊,如下所示:intn=jfc.showOpenDialog(Ch11_4_1.this);if(n==JFileChooser.APPROVE_OPTION){Filefile=jfc.getSelectedFile();
……}2023/11/2731JFileChooser檔案選擇元件-儲存檔案對話方塊儲存檔案對話方塊是使用showSaveDialog()方法來顯示,如下所示:intm=jfc.showSaveDialog(Ch11_4_1.this);if(m==JFileChooser.APPROVE_OPTION){Filefile=jfc.getSelectedFile();
……}2023/11/2732範(fàn)例4:使用FileChooser元件(1/3)基本宣告:/*程式範(fàn)例:Ch09_04.java*/importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.filechooser.*;//繼承JFrame類別publicclassCh09_04extendsJFrame{//建構(gòu)子
publicCh09_04(){super("JFileChooser元件範(fàn)例");Containerc=getContentPane();//建立擁有捲動軸的文字區(qū)域元件
finalJTextAreaarea=newJTextArea(15,30);JScrollPanescroll=newJScrollPane(area);//建立JFileChooser元件
finalJFileChooserjfc=newJFileChooser();JPanelbutton=newJPanel();//按鈕的JPanel2023/11/2733範(fàn)例4:使用FileChooser元件(2/3)新增兩個按鈕及傾聽者及事件處理方法:
JButtonopen=newJButton("開啟檔案");open.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){intn=jfc.showOpenDialog(Ch09_04.this);if(n==JFileChooser.APPROVE_OPTION){Filefile=jfc.getSelectedFile();area.append("開啟檔案名稱:");area.append(file.getName()+"\n");}}});button.add(open);//建立儲存檔案按鈕
JButtonsave=newJButton("儲存檔案");save.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){intm=jfc.showSaveDialog(Ch09_04.this);if(m==JFileChooser.APPROVE_OPTION){Filefile=jfc.getSelectedFile();area.append("儲存檔案名稱:");area.append(file.getName()+"\n");}}});button.add(save);c.add(scroll,BorderLayout.CENTER);c.add(button,BorderLayout.SOUTH);}2023/11/2734練習(xí)1參考範(fàn)例2,範(fàn)例3,範(fàn)例4建立一個類似筆記本畫面的視窗2023/11/2735範(fàn)例4:使用FileChooser元件(3/3)主程式:publicstaticvoidmain(String[]args){//建立Swing應(yīng)用程式
Ch09_04app=newCh09_04();//關(guān)閉視窗事件,結(jié)束程式的執(zhí)行
app.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevt){System.exit(0);}});app.setSize(300,200);//設(shè)定尺寸
app.setVisible(true);//顯示視窗
}}2023/11/2736JColorChooser色彩選擇元件-說明JColorChooser色彩選擇元件提供多種標(biāo)籤和調(diào)色盤的色彩選擇對話方塊,如果Java應(yīng)用程式需要讓使用者選擇色彩,就可以使用JColorChooser元件,如右圖所示:2023/11/2737JColorChooser色彩選擇元件-建立物件JColorChooser色彩選擇元件的建立,如下所示:JColorChooserjcc=newJColorChooser();程式碼建立JColorChooser物件後,使用showDialog()方法顯示色彩選擇對話方塊,如下所示:ColornewColor=jcc.showDialog(Ch11_4_2.this,"選擇背景色彩",c.getBackground());if(newColor!=null)c.setBackground(newColor);2023/11/2738多重視窗介面JInternalFrame-說明一般來說,視窗應(yīng)用程式都不會只有一個視窗,如果需要在JFrame視窗開啟其它視窗,就可以使用JInternalFrame類別在JFrame視窗內(nèi)建立多重視窗。其繼承架構(gòu)如下圖所示:2023/11/2739多重視窗介面JInternalFrame-JDesktopPane和JLayeredPane類別JInternalFrame物件是新增在JDesktopPane物件(在使用上如同JFrame的ContentPane),所以需要先建立JDesktopPane物件,如下所示:JDesktopPanejdesktop=newJDesktopPane();上述程式碼建立JDesktopPane物件後,JInternalFrame物件就是新增到此容器物件,因為JDesktopPane是JLayeredPane的子類別,所以能夠建立多個重疊的內(nèi)層視窗。2023/11/2740多重視窗介面JInternalFrame-JInternalFrame類別(說明)JInternalFrame類別在JInternalFrame類別部分,筆者準(zhǔn)備直接繼承JInternalFrame建立InternalFrame類別。2023/11/2741多重視窗介面JInternalFrame-JInternalFrame類別(範(fàn)例)classInternalFrameextendsJInternalFrame{staticintiframeCount=0;staticfinalintoffsetX=25;staticfinalintoffsetY=25;publicInternalFrame(){super("內(nèi)層視窗:"+(++iframeCount),true,//可調(diào)整尺寸
true,//可關(guān)閉
true,//可最大化
true);//可縮小成圖示
setSize(300,200);//設(shè)定尺寸
//設(shè)定位置
setLocation(offsetX*iframeCount,offsetY*iframeCount);}}2023/11/2742多重視窗介面JInternalFrame-JInternalFrame類別(createInternalFrame()方法)privatevoidcreateInternalFrame(){InternalFrameiframe=newInternalFrame();iframe.setVisible(true);//顯示內(nèi)層視窗
jdesktop.add(iframe);//加入上層視窗
try{iframe.setSelected(true);}catch(java.beans.PropertyVetoExceptione){}}Ch22_Main.javaimportjavax.swing.*;importjava.awt.*;importjava.awt.event.*;classCh22_Main{publicstaticvoidmain(String[]args){Ch22_Win_09w=newCh22_Win_09();w.setSize(300,250);w.setVisible(true);w.setDefaultCloseOperation(Wi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年畜禽水產(chǎn)行業(yè)深度研究分析報告
- 借款合同范本免息
- 2025年國際貿(mào)易融資與風(fēng)險管理實訓(xùn)合同
- 2025年脫膠毛大豆油項目投資可行性研究分析報告
- 代辦股權(quán)糾紛居間合同范本
- 2025年藤竹制品項目可行性研究報告
- 2025年度智慧水務(wù)系統(tǒng)工程簡易建筑工程合同
- 2023-2029年中國公路工程承包行業(yè)市場發(fā)展現(xiàn)狀及投資規(guī)劃建議報告
- 2020-2025年中國疾病遠(yuǎn)距檢測行業(yè)發(fā)展趨勢預(yù)測及投資戰(zhàn)略規(guī)劃分析報告
- 2025年度建筑垃圾處理技術(shù)研發(fā)與成果轉(zhuǎn)化合同
- QC成果地下室基礎(chǔ)抗浮錨桿節(jié)點處防水施工方法的創(chuàng)新
- 第一章:公共政策理論模型
- 中藥審核處方的內(nèi)容(二)
- (完整)金正昆商務(wù)禮儀答案
- RB/T 101-2013能源管理體系電子信息企業(yè)認(rèn)證要求
- GB/T 10205-2009磷酸一銨、磷酸二銨
- 公司財務(wù)制度及流程
- 高支模專項施工方案(專家論證)
- 《物流與供應(yīng)鏈管理-新商業(yè)、新鏈接、新物流》配套教學(xué)課件
- 物聯(lián)網(wǎng)項目實施進度計劃表
- MDD指令附錄一 基本要求檢查表2013版
評論
0/150
提交評論