1 / 42

Ch06 Swing 的圖形介面元件 (II)

Ch06 Swing 的圖形介面元件 (II). 物件導向系統實務. 大綱. JPopupMenu 彈出式選單元件 JMenuBar 、 JMenu 與 JMenuItem 下拉式選單元件 JToolBar 工具列元件 JFileChooser 檔案選擇元件 JColorChooser 色彩選擇元件 多重視窗. 視窗功能表和工具列元件. Swing 套件提供功能強大的視窗功能表和工具列元件,可以輕鬆建立應用程式視窗上方的下拉式功能表、工具列和彈出式選單。 同樣的,視窗功能表和工具列元件也都是繼承自 JComponent ,其繼承架構如下圖所示:.

alesia
Download Presentation

Ch06 Swing 的圖形介面元件 (II)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch06 Swing的圖形介面元件(II) 物件導向系統實務

  2. 大綱 • JPopupMenu彈出式選單元件 • JMenuBar、JMenu與JMenuItem下拉式選單元件 • JToolBar工具列元件 • JFileChooser檔案選擇元件 • JColorChooser色彩選擇元件 • 多重視窗

  3. 視窗功能表和工具列元件 • Swing套件提供功能強大的視窗功能表和工具列元件,可以輕鬆建立應用程式視窗上方的下拉式功能表、工具列和彈出式選單。 • 同樣的,視窗功能表和工具列元件也都是繼承自JComponent,其繼承架構如下圖所示:

  4. JPopupMenu彈出式選單元件-說明 • JPopupMenu彈出式選單元件繼承自JComponent,可以建立視窗應用程式滑鼠右鍵顯示的快顯功能表,內含選項的JMenuItem物件或JSeparator分隔線物件,如下圖所示:

  5. JPopupMenu彈出式選單元件-建立物件 • 在建立JPopupMenu物件後,使用add()方法新增選項的JMenuItem物件,addSeparator()方法可以新增選單分隔線的JSeparator物件。 popup = new JPopupMenu(); popup.add(blue = new JMenuItem("藍色")); popup.add(yellow = new JMenuItem("黃色")); popup.add(green = new JMenuItem("綠色")); popup.addSeparator(); popup.add("紅色");

  6. JPopupMenu彈出式選單元件-事件處理 • 新增MouseListener傾聽者物件且實作mousePressed()和mouseReleased()方法來顯示彈出式視窗,如下所示: public void mousePressed(MouseEvent evt) { if (evt.isPopupTrigger()) popup.show(evt.getComponent(), evt.getX(), evt.getY()); } public void mouseReleased(MouseEvent evt) { if (evt.isPopupTrigger()) popup.show(evt.getComponent(), evt.getX(), evt.getY()); }

  7. 建構子: JPopupMenu() JPopupMenu(String):參數String是標題文字 方法: JMenuItem add(JMenuItem) JMenuItem add(String) void addSeparator() void insert(Component, int) void remove(JMenuItem) void remove(int) void removeAll() void show(Component, int, int) JPopupMenu建構子與方法

  8. 範例1:使用PopupMenu(1/4) • 建立Popup Menu的選擇項: • import javax.swing.*; • import java.awt.*; • import java.awt.event.*; • // 繼承JFrame類別, 實作ActionListener介面 • public class Ch09_01 extends JFrame • implements ActionListener • { private JPopupMenu popup; • private JMenuItem blue, yellow, green; • private Container c; • // 建構子 • public Ch09_01() • { super("JPopupMenu元件範例"); • c = getContentPane(); • c.setBackground(Color.pink); • popup = new JPopupMenu(); • popup.add(blue = new JMenuItem("藍色")); • blue.addActionListener(this); • popup.add(yellow = new JMenuItem("黃色")); • yellow.addActionListener(this); • popup.add(green = new JMenuItem("綠色")); • green.addActionListener(this); • popup.addSeparator(); • popup.add("紅色"); // 使用字串新增選項

  9. 範例1:使用PopupMenu(2/4) • 傾聽者: • addMouseListener(new MouseAdapter() • { public void mousePressed(MouseEvent evt) • { if ( evt.isPopupTrigger() ) // 顯示選單 • popup.show(evt.getComponent(), • evt.getX(), evt.getY()); • } • public void mouseReleased(MouseEvent evt) • { if ( evt.isPopupTrigger() ) // 顯示選單 • popup.show(evt.getComponent(), • evt.getX(), evt.getY()); • } • }); • }

  10. 範例1:使用PopupMenu(3/4) • 實作事件處理方法: • public void actionPerformed(ActionEvent evt) • { 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(); // 重繪 • }

  11. 範例1:使用PopupMenu(4/4) • 主程式: • public static void main(String[] args) • { // 建立Swing應用程式 • Ch09_01 app = new Ch09_01(); • // 關閉視窗事件, 結束程式的執行 • app.addWindowListener(new WindowAdapter() • { public void windowClosing(WindowEvent evt) • { System.exit(0); } • }); • app.setSize(300,200); // 設定尺寸 • app.setVisible(true); // 顯示視窗 • } • }

  12. Menu屬MenuComponent的延伸類別 12

  13. Menu屬MenuComponent的延伸類別 • 以「記事本」為例說明選單及其相關元件 13

  14. Menu屬MenuComponent的延伸類別 • Menu位於視窗標題列的下方 • 使用選單時,框架(Frame)會有一個選單列(MenuBar),選單列內有數個選單(Menu) • 每個Menu內會有多個選項(MenuItem)或核選式選項(CheckboxMenuItem),選單是選項的容器 • 選單也可以是另一個選單的容器 14

  15. JMenuBar、JMenu與JMenuItem下拉式選單元件-說明 • 在JFrame、JInternalFrame、JApplet和JDialog等類別的視窗新增下拉式功能表選單,類別建構子需要使用JMenuBar、JMenu和JMenuItem物件來建立下拉式功能表的物件。

  16. JMenuBar、JMenu與JMenuItem下拉式選單元件-JMenuBar元件JMenuBar、JMenu與JMenuItem下拉式選單元件-JMenuBar元件 • JMenuBar元件是視窗上方的功能表列,如下所示: JMenuBar jmb = new JMenuBar(); setJMenuBar(jmb); • 上述程式碼建立JMenuBar物件後,預設是空的功能表列,然後使用setJMenuBar()方法新增到JFrame視窗,換句話說,目前在視窗上方已經擁有一個空的功能表列。

  17. JMenuBar、JMenu與JMenuItem下拉式選單元件-JMenu元件JMenuBar、JMenu與JMenuItem下拉式選單元件-JMenu元件 • 在建立好JMenuBar物件後,就可以新增功能表列下拉式子選單的JMenu物件,如下所示: JMenu file = new JMenu("檔案(F)"); JMenuItem item; file.add(item = new JMenuItem("新增(N)",KeyEvent.VK_N)); file.add(item = new JMenuItem("開啟(O)",KeyEvent.VK_O)); JMenu setting = new JMenu("參數設定"); file.add(setting); file.addSeparator(); file.add(item = new JMenuItem("關閉(X)",KeyEvent.VK_X)); jmb.add(file);

  18. JMenuBar、JMenu與JMenuItem下拉式選單元件-Item元件 JMenuItem、JCheckBoxMenuItem與JRadioButtonMenuItem元件 • JMenuItem、JCheckBoxMenuItem與JRadioButtonMenuItem類別的建構子可以新增選單的選項、核取方塊和選項鈕選項。

  19. 範例2:建立Menu(1/5) • 基本宣告: • /* 程式範例: Ch09_02.java */ • import javax.swing.*; • import java.awt.*; • import java.awt.event.*; • // 繼承JFrame類別, 實作ActionListener介面 • public class Ch09_02 extends JFrame • implements ActionListener • { private JRadioButtonMenuItem red, green, blue; • private JMenuItem openItem, newItem, exitItem, codeItem, typeItem; • private JMenu setting; • private Container c; • // 建構子 • public Ch09_02() • { super ("JMenuBar元件範例"); • c = getContentPane(); • c.setBackground(Color.white); • JMenuBar jmb = new JMenuBar(); • setJMenuBar(jmb);

  20. 範例2:建立Menu(2/5) • 建立第一個File的Menu: • JMenu file = new JMenu("檔案(F)"); • file.setMnemonic(KeyEvent.VK_F); • openItem = new JMenuItem("新增(N)", KeyEvent.VK_N); • newItem = new JMenuItem("開啟(O)",KeyEvent.VK_O); • exitItem = new JMenuItem("關閉(X)",KeyEvent.VK_X); • setting = new JMenu("參數設定"); • codeItem = new JMenuItem("編碼"); • typeItem = new JMenuItem("字型"); • 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選單

  21. 範例2:建立Menu(3/5) • 建立第二個Menu: • JMenu choice = new JMenu("選項(C)"); • choice.setMnemonic(KeyEvent.VK_C); • JCheckBoxMenuItem check; • check = new JCheckBoxMenuItem("切換"); • check.addActionListener(this); • choice.add(check); • ButtonGroup buttongroup = new ButtonGroup(); • red = new JRadioButtonMenuItem("紅色"); • choice.add(red); • buttongroup.add(red); • red.addActionListener(this); • green = new JRadioButtonMenuItem("綠色"); • choice.add(green); • buttongroup.add(green); • green.addActionListener(this); • blue = new JRadioButtonMenuItem("藍色"); • choice.add(blue); • buttongroup.add(blue); • blue.addActionListener(this); • jmb.add(choice); • }

  22. 範例2:建立Menu(4/5) • 實作事件處理方法: • public void actionPerformed(ActionEvent evt) • { • 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(); // 重繪 • }

  23. 範例2:建立Menu(5/5) • 主程式 • public static void main(String[] args) • { // 建立Swing應用程式 • Ch09_02 app = new Ch09_02(); • // 關閉視窗事件, 結束程式的執行 • app.addWindowListener(new WindowAdapter() • { public void windowClosing(WindowEvent evt) • { System.exit(0); } • }); • app.setSize(300,200); // 設定尺寸 • app.setVisible(true); // 顯示視窗 • } • }

  24. JToolBar工具列元件-說明 • JToolBar工具列元件繼承自JComponent類別,可以建立視窗的工具列按鈕,它也屬於一種容器元件,在建立好JToolBar物件後,就可以新增GUI元件到工具列,如下圖所示:

  25. JToolBar工具列元件-建立物件 • 程式碼在建立好JToolBar元件後,使用add()方法新增GUI元件,最後只需將JToolBar元件視為GUI元件,新增到最上層容器物件即可。 JToolBar toolBar = new JToolBar(); blue = new JButton(new ImageIcon("blue1.gif")); yellow = new JButton(new ImageIcon("yellow1.gif")); green = new JButton(new ImageIcon("green1.gif")); toolBar.add(blue); toolBar.add(yellow); toolBar.add(green);

  26. 範例3:建立toolbar(1/2) • 宣告及toolbar建立: • /* 程式範例: Ch09_03.java */ • import javax.swing.*; • import java.awt.*; • import java.awt.event.*; • // 繼承JFrame類別, 實作ActionListener介面 • public class Ch09_03 extends JFrame implements ActionListener • { private JButton blue, yellow, green; • private Container c; • // 建構子 • public Ch09_03() • { super ("JToolBar元件範例"); • c = getContentPane(); • c.setBackground(Color.white); • JToolBar toolBar = new JToolBar(); • blue = new JButton(new ImageIcon("blue.jpg")); • blue.setToolTipText("藍色"); • blue.addActionListener(this); • yellow = new JButton(new ImageIcon("yellow.jpg")); • yellow.setToolTipText("黃色"); • yellow.addActionListener(this); • green = new JButton(new ImageIcon("green.jpg")); • green.setToolTipText("綠色"); • green.addActionListener(this); • toolBar.add(blue); • toolBar.add(yellow); • toolBar.add(green); • c.add(toolBar, BorderLayout.NORTH); • }

  27. 範例3:建立toolbar(2/2) • 實作事件處理方法及主程式: • public void actionPerformed(ActionEvent evt) • { 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(); // 重繪 • } • // 主程式 • public static void main(String[] args) • { // 建立Swing應用程式 • Ch09_03 app = new Ch09_03(); • // 關閉視窗事件, 結束程式的執行 • app.addWindowListener(new WindowAdapter() • { public void windowClosing(WindowEvent evt) • { System.exit(0); } • }); • app.setSize(300,200); // 設定尺寸 • app.setVisible(true); // 顯示視窗 • } • }

  28. 檔案與色彩選擇元件-說明 • Swing套件擁有瀏覽檔案系統選取檔案或資料夾的JFileChooser和選取色彩的JColorChooser元件2種選擇元件,這2個元件都是繼承自JComponent,其繼承架構如下圖所示:

  29. JFileChooser檔案選擇元件-說明 • JFileChooser檔案選擇元件可以顯示對話方塊瀏覽檔案系統,以便讓使用者選取檔案或資料夾。

  30. JFileChooser檔案選擇元件-開啟檔案對話方塊 • 例如:開啟或儲存指定檔案,如下所示: JFileChooser jfc = new JFileChooser(); • 上述程式碼建立JFileChooser物件後,使用showOpenDialog()方法顯示開啟檔案對話方塊,如下所示: int n = jfc.showOpenDialog(Ch11_4_1.this); if (n == JFileChooser.APPROVE_OPTION) { File file = jfc.getSelectedFile(); …… }

  31. JFileChooser檔案選擇元件-儲存檔案對話方塊 • 儲存檔案對話方塊是使用showSaveDialog()方法來顯示,如下所示: int m = jfc.showSaveDialog(Ch11_4_1.this); if (m == JFileChooser.APPROVE_OPTION) { File file = jfc.getSelectedFile(); …… }

  32. 範例4:使用FileChooser元件(1/3) • 基本宣告: • /* 程式範例: Ch09_04.java */ • import java.io.*; • import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • import javax.swing.filechooser.*; • // 繼承JFrame類別 • public class Ch09_04 extends JFrame • { // 建構子 • public Ch09_04() • { super("JFileChooser元件範例"); • Container c = getContentPane(); • // 建立擁有捲動軸的文字區域元件 • final JTextArea area = new JTextArea(15,30); • JScrollPane scroll = new JScrollPane(area); • // 建立JFileChooser元件 • final JFileChooser jfc = new JFileChooser(); • JPanel button = new JPanel(); // 按鈕的JPanel

  33. 範例4:使用FileChooser元件(2/3) • 新增兩個按鈕及傾聽者及事件處理方法: • JButton open = new JButton("開啟檔案"); • open.addActionListener(new ActionListener() • { public void actionPerformed(ActionEvent evt) • { int n = jfc.showOpenDialog(Ch09_04.this); • if ( n == JFileChooser.APPROVE_OPTION ) • { File file = jfc.getSelectedFile(); • area.append("開啟檔案名稱: "); • area.append(file.getName() + "\n"); • } • } • }); • button.add(open); • // 建立儲存檔案按鈕 • JButton save = new JButton("儲存檔案"); • save.addActionListener(new ActionListener() • { public void actionPerformed(ActionEvent evt) • { int m = jfc.showSaveDialog(Ch09_04.this); • if ( m == JFileChooser.APPROVE_OPTION ) • { File file = jfc.getSelectedFile(); • area.append("儲存檔案名稱: "); • area.append(file.getName() + "\n"); • } • } • }); • button.add(save); • c.add(scroll, BorderLayout.CENTER); • c.add(button, BorderLayout.SOUTH); • }

  34. 練習1 • 參考範例2,範例3,範例4建立一個類似筆記本畫面的視窗

  35. 範例4:使用FileChooser元件(3/3) • 主程式: • public static void main(String[] args) • { // 建立Swing應用程式 • Ch09_04 app = new Ch09_04(); • // 關閉視窗事件, 結束程式的執行 • app.addWindowListener(new WindowAdapter() • { public void windowClosing(WindowEvent evt) • { System.exit(0); } • }); • app.setSize(300,200); // 設定尺寸 • app.setVisible(true); // 顯示視窗 • } • }

  36. JColorChooser色彩選擇元件-說明 • JColorChooser色彩選擇元件提供多種標籤和調色盤的色彩選擇對話方塊,如果Java應用程式需要讓使用者選擇色彩,就可以使用JColorChooser元件,如右圖所示:

  37. JColorChooser色彩選擇元件-建立物件 • JColorChooser色彩選擇元件的建立,如下所示: JColorChooser jcc = new JColorChooser(); • 程式碼建立JColorChooser物件後,使用showDialog()方法顯示色彩選擇對話方塊,如下所示: Color newColor = jcc.showDialog(Ch11_4_2.this, "選擇背景色彩", c.getBackground()); if (newColor != null) c.setBackground(newColor);

  38. 多重視窗介面JInternalFrame-說明 • 一般來說,視窗應用程式都不會只有一個視窗,如果需要在JFrame視窗開啟其它視窗,就可以使用JInternalFrame類別在JFrame視窗內建立多重視窗。其繼承架構如下圖所示:

  39. 多重視窗介面JInternalFrame-JDesktopPane和JLayeredPane類別多重視窗介面JInternalFrame-JDesktopPane和JLayeredPane類別 • JInternalFrame物件是新增在JDesktopPane物件(在使用上如同JFrame的ContentPane),所以需要先建立JDesktopPane物件,如下所示: JDesktopPane jdesktop = new JDesktopPane(); • 上述程式碼建立JDesktopPane物件後,JInternalFrame物件就是新增到此容器物件,因為JDesktopPane是JLayeredPane的子類別,所以能夠建立多個重疊的內層視窗。

  40. 多重視窗介面JInternalFrame- JInternalFrame類別(說明) JInternalFrame類別 • 在JInternalFrame類別部分,筆者準備直接繼承JInternalFrame建立InternalFrame類別。

  41. 多重視窗介面JInternalFrame- JInternalFrame類別(範例) class InternalFrame extends JInternalFrame { static int iframeCount = 0; static final int offsetX = 25; static final int offsetY = 25; public InternalFrame() { super("內層視窗: " + (++iframeCount), true, // 可調整尺寸 true, // 可關閉 true, // 可最大化 true); // 可縮小成圖示 setSize(300,200); // 設定尺寸 // 設定位置 setLocation(offsetX*iframeCount, offsetY*iframeCount); } }

  42. 多重視窗介面JInternalFrame- JInternalFrame類別(createInternalFrame()方法) private void createInternalFrame() { InternalFrame iframe = new InternalFrame(); iframe.setVisible(true); // 顯示內層視窗 jdesktop.add(iframe); // 加入上層視窗 try { iframe.setSelected(true); }catch(java.beans.PropertyVetoException e){} }

More Related