Presentation is loading. Please wait.

Presentation is loading. Please wait.

記事本 物件導向系統實務.

Similar presentations


Presentation on theme: "記事本 物件導向系統實務."— Presentation transcript:

1 記事本 物件導向系統實務

2 建立記事本的5步驟 建立視窗並加上文字區 加上選單功能表 為選單功能表上的選項加上聽者和動作

3 建立視窗並加上文字區

4 2018/12/4 JTextArea文字區域元件-說明 JTextArea文字區域元件能夠輸入或顯示多行文字內容,使用”\n”、”\n\r”或”\r”符號換行(需視作業系統而定),如下圖所示:

5 2018/12/4 JTextArea文字區域元件-說明 JTextArea文字區域元件使用”\n”、”\n\r”或”\r”符號換行(需視作業系統而定),如下所示: JTextArea area = new JTextArea("等待輸入資料...\n", 15, 30); JScrollPane scroll = new JScrollPane(area); 上述程式碼建立指定初始字串、寬和高的JTextArea物件,因為文字區域需要捲動,所以使用JScrollPane容器元件替文字區域加上捲動軸。

6 JTextArea建構子和方法 方法: 建構子: void append(String) JTextArea()
2018/12/4 JTextArea建構子和方法 方法: void append(String) void insert(String, int) void replaceRange(String, int, int) String getText() void setFont(Font) void setRows(int) void setColumns(int) int getColumns() int getLineCount() void setLineWrap(boolean) boolean getLineWrap() void setTabSize(int) int getTabSize() 建構子: JTextArea() JTextArea(String) JTextField(int, int) JTextField(String, int, int)

7 Step1: 視窗設計並加上文字區 主程式 視窗程式 import java.awt.*; import java.awt.event.*;
class NoteBook extends JFrame { private Container c; private JPanel panel; private JTextArea textArea; private JScrollPane sp; NoteBook(String title) { super(title); c = getContentPane(); panel = new JPanel(); panel.setLayout(new BorderLayout()); //設定文字框 textArea = new JTextArea(); //設定文字過長時自動折行 textArea.setLineWrap(true); //加上捲軸 sp = new JScrollPane(textArea); //將文字框及捲軸加到視窗上 panel.add(sp, BorderLayout.CENTER); c.add(panel); } import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; class MyNote { public static void main(String [] args) NoteBook nb = new NoteBook("記事本"); nb.setSize(500,500); nb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); nb.setVisible(true); } NB1

8 執行結果

9 加上選單功能表

10 Menu屬MenuComponent的延伸類別
2018/12/4 Menu屬MenuComponent的延伸類別 10

11 Menu屬MenuComponent的延伸類別
2018/12/4 Menu屬MenuComponent的延伸類別 以「記事本」為例說明選單及其相關元件 11

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

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

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

15 JMenuBar、JMenu與JMenuItem下拉式選單元件-JMenu元件
2018/12/4 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);

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

17 class NoteBook extends JFrame
{ private Container c; private JPanel panel; private JTextArea textArea; private JScrollPane sp; private JMenuBar mb; NoteBook(String title) super(title); c = getContentPane(); panel = new JPanel(); panel.setLayout(new BorderLayout()); //加上選單功能表 mb = new JMenuBar(); //建立選單功能表的第一個檔案選單 JMenu fileMenu = new JMenu("檔案"); //建立檔案選單下的選項 JMenuItem newFile = new JMenuItem("開新檔案"); JMenuItem oldFile = new JMenuItem("開啟舊檔"); JMenuItem saveFile = new JMenuItem("儲存檔案"); JMenuItem exitNote = new JMenuItem("離開"); //將選項加到檔案中 fileMenu.add(newFile); fileMenu.add(oldFile); fileMenu.add(saveFile); //加一條分隔線 fileMenu.addSeparator(); fileMenu.add(exitNote); //將檔案選單加到功能表上 mb.add(fileMenu); //將選單功能表加到視窗上 panel.add(mb, BorderLayout.NORTH); //設定文字框 textArea = new JTextArea(); //設定文字過長時自動折行 textArea.setLineWrap(true); //加上捲軸 sp = new JScrollPane(textArea); //將文字框及捲軸加到視窗上 panel.add(sp, BorderLayout.CENTER); c.add(panel); } NB2

18 執行結果

19 為新增的選單項目 加上LISTENER和相對應的動作(先處理開新檔和離開的動作)

20 class NoteBook extends JFrame { private Container c;
private JPanel panel; private JTextArea textArea; private JScrollPane sp; private JMenuBar mb; NoteBook(String title) { super(title); c = getContentPane(); panel = new JPanel(); panel.setLayout(new BorderLayout()); //加上選單功能表 mb = new JMenuBar(); //建立選單功能表的第一個檔案選單 JMenu fileMenu = new JMenu("檔案"); //建立檔案選單下的選項 JMenuItem newFile = new JMenuItem("開新檔案"); JMenuItem oldFile = new JMenuItem("開啟舊檔"); JMenuItem saveFile = new JMenuItem("儲存檔案"); JMenuItem exitNote = new JMenuItem("離開"); NB3

21 //為開新檔案加上Listener和動作,動作是將文字區域清為空字串"
newFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.setText(""); } } ); //為開啟舊檔加上Listener和動作, 動作要搭配FileChooser,暫不作 oldFile.addActionListener(new ActionListener () { textArea.setText("開啟舊檔還不能使用"); //為儲存檔案加上Listener和動作, 動作要搭配FileChooser,暫不作 saveFile.addActionListener(new ActionListener () { textArea.setText("儲存檔案還不能使用"); //為離開加上Listener和動作, 動作要搭配FileChooser,暫不作 exitNote.addActionListener(new ActionListener () { System.exit(0);

22 //將選項加到檔案中 fileMenu.add(newFile); fileMenu.add(oldFile); fileMenu.add(saveFile); //加一條分隔線 fileMenu.addSeparator(); fileMenu.add(exitNote); //將檔案選單加到功能表上 mb.add(fileMenu); //將選單功能表加到視窗上 panel.add(mb, BorderLayout.NORTH); //設定文字框 textArea = new JTextArea(); //設定文字過長時自動折行 textArea.setLineWrap(true); //加上捲軸 sp = new JScrollPane(textArea); //將文字框及捲軸加到視窗上 panel.add(sp, BorderLayout.CENTER); c.add(panel); }

23 執行結果

24 練習:加上另一個編輯選單(含剪下,複製,貼上)
練習:加上另一個編輯選單(含剪下,複製,貼上) 

25 為剪下,複製,貼上加上聽者及動作 NB4

26 import java.awt.event.*; import javax.swing.*; import java.io.*;
class MyNote { public static void main(String [] args) NoteBook nb = new NoteBook("記事本"); nb.setSize(500,500); nb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); nb.setVisible(true); } class NoteBook extends JFrame private Container c; private JPanel panel; private JTextArea textArea; private JScrollPane sp; private JMenuBar mb; NoteBook(String title) { super(title); c = getContentPane(); panel = new JPanel(); panel.setLayout(new BorderLayout()); //加上選單功能表 mb = new JMenuBar(); //建立選單功能表的第一個檔案選單 JMenu fileMenu = new JMenu("檔案"); //建立檔案選單下的選項 JMenuItem newFile = new JMenuItem("開新檔案"); JMenuItem oldFile = new JMenuItem("開啟舊檔"); JMenuItem saveFile = new JMenuItem("儲存檔案"); JMenuItem exitNote = new JMenuItem("離開"); //為開新檔案加上Listener和動作,動作是將文字區域清為空字串" newFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.setText(""); } } );

27 //為開啟舊檔加上Listener和動作, 動作要搭配FileChooser,暫不作
oldFile.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { textArea.setText("開啟舊檔還不能使用"); } } ); //為儲存檔案加上Listener和動作, 動作要搭配FileChooser,暫不作 saveFile.addActionListener(new ActionListener () { textArea.setText("儲存檔案還不能使用"); //為離開加上Listener和動作, 動作要搭配FileChooser,暫不作 exitNote.addActionListener(new ActionListener () { System.exit(0); //將選項加到檔案中 fileMenu.add(newFile); fileMenu.add(oldFile); fileMenu.add(saveFile); //加一條分隔線 fileMenu.addSeparator(); fileMenu.add(exitNote); //將檔案選單加到功能表上 mb.add(fileMenu);

28 //建立選單功能表的第二個編輯選單 JMenu editMenu = new JMenu("編輯"); //建立編輯選單下的選項 JMenuItem cutEdit = new JMenuItem("剪下"); JMenuItem copyEdit = new JMenuItem("複製"); JMenuItem pasteEdit = new JMenuItem("貼上"); //為剪下加上Listener和動作,動作是將文字區域中被選的文字剪掉" cutEdit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.cut(); } } ); //為複製加上Listener和動作 copyEdit.addActionListener(new ActionListener () { textArea.copy(); //為貼上加上Listener和動作 pasteEdit.addActionListener(new ActionListener () { textArea.paste(); //將選項加到編輯中 editMenu.add(cutEdit); editMenu.add(copyEdit); editMenu.add(pasteEdit); //將編輯選單加到功能表上 mb.add(editMenu);

29 //將選單功能表加到視窗上 panel.add(mb, BorderLayout.NORTH); //設定文字框 textArea = new JTextArea(); //設定文字過長時自動折行 textArea.setLineWrap(true); //加上捲軸 sp = new JScrollPane(textArea); //將文字框及捲軸加到視窗上 panel.add(sp, BorderLayout.CENTER); c.add(panel); }

30 為選單功能表上的每一個選項加上快速鍵

31 快速鍵對應表 選單 選項 快速鍵 檔案 Alt+F 開新檔案 Alt+N 開啟舊檔 Alt+O 儲存檔案 Alt+S 離開 Alt+X 編輯
Alt+E 剪下 Alt+U 複製 Alt+C 貼上 Alt+P

32 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; class MyNote { public static void main(String [] args) { NoteBook nb = new NoteBook("記事本"); nb.setSize(500,500); nb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); nb.setVisible(true); } class NoteBook extends JFrame { private Container c; private JPanel panel; private JTextArea textArea; private JScrollPane sp; private JMenuBar mb; NoteBook(String title) { super(title); NB5

33 c = getContentPane(); panel = new JPanel(); panel.setLayout(new BorderLayout()); //加上選單功能表 mb = new JMenuBar(); //建立選單功能表的第一個檔案選單 JMenu fileMenu = new JMenu("檔案(F)"); fileMenu.setMnemonic(KeyEvent.VK_F); //建立檔案選單下的選項 JMenuItem newFile = new JMenuItem("開新檔案(N)", KeyEvent.VK_N); JMenuItem oldFile = new JMenuItem("開啟舊檔(O)", KeyEvent.VK_O); JMenuItem saveFile = new JMenuItem("儲存檔案(S)", KeyEvent.VK_S); JMenuItem exitNote = new JMenuItem("離開(X)", KeyEvent.VK_X); //為開新檔案加上Listener和動作,動作是將文字區域清為空字串" newFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.setText(""); } } ); //為開啟舊檔加上Listener和動作, 動作要搭配FileChooser,暫不作 oldFile.addActionListener(new ActionListener () { textArea.setText("開啟舊檔還不能使用");

34 //為儲存檔案加上Listener和動作, 動作要搭配FileChooser,暫不作
saveFile.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { textArea.setText("儲存檔案還不能使用"); } } ); //為離開加上Listener和動作, 動作要搭配FileChooser,暫不作 exitNote.addActionListener(new ActionListener () { System.exit(0); //將選項加到檔案中 fileMenu.add(newFile); fileMenu.add(oldFile); fileMenu.add(saveFile); //加一條分隔線 fileMenu.addSeparator(); fileMenu.add(exitNote); //將檔案選單加到功能表上 mb.add(fileMenu);

35 //建立選單功能表的第二個編輯選單 JMenu editMenu = new JMenu("編輯(E)"); editMenu.setMnemonic(KeyEvent.VK_E); //建立編輯選單下的選項 JMenuItem cutEdit = new JMenuItem("剪下(U)", KeyEvent.VK_U); JMenuItem copyEdit = new JMenuItem("複製(C)", KeyEvent.VK_C); JMenuItem pasteEdit = new JMenuItem("貼上(P)", KeyEvent.VK_P); //為剪下加上Listener和動作,動作是將文字區域中被選的文字剪掉" cutEdit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.cut(); } } ); //為複製加上Listener和動作 copyEdit.addActionListener(new ActionListener () { textArea.copy(); //為貼上加上Listener和動作 pasteEdit.addActionListener(new ActionListener () { textArea.paste();

36 //將選項加到編輯中 editMenu.add(cutEdit); editMenu.add(copyEdit); editMenu.add(pasteEdit); //將編輯選單加到功能表上 mb.add(editMenu); //將選單功能表加到視窗上 panel.add(mb, BorderLayout.NORTH); //設定文字框 textArea = new JTextArea(); //設定文字過長時自動折行 textArea.setLineWrap(true); //加上捲軸 sp = new JScrollPane(textArea); //將文字框及捲軸加到視窗上 panel.add(sp, BorderLayout.CENTER); c.add(panel); }

37 加入CHECKBOXMENUITEM和RADIOBUTTONMENUITEM等樣式的選單項目

38 import java.awt.event.*;
import javax.swing.*; import java.io.*; class MyNote { public static void main(String [] args) NoteBook nb = new NoteBook("記事本"); nb.setSize(500,500); nb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); nb.setVisible(true); } class NoteBook extends JFrame private Container c; private JPanel panel; private JTextArea textArea; private JScrollPane sp; private JMenuBar mb; private int bold = Font.PLAIN; private int italic = Font.PLAIN; private int fonts = 12;

39 NoteBook(String title)
{ super(title); c = getContentPane(); panel = new JPanel(); panel.setLayout(new BorderLayout()); //加上選單功能表 mb = new JMenuBar(); //建立選單功能表的第一個檔案選單 JMenu fileMenu = new JMenu("檔案(F)"); fileMenu.setMnemonic(KeyEvent.VK_F); //建立檔案選單下的選項 JMenuItem newFile = new JMenuItem("開新檔案(N)", KeyEvent.VK_N); JMenuItem oldFile = new JMenuItem("開啟舊檔(O)", KeyEvent.VK_O); JMenuItem saveFile = new JMenuItem("儲存檔案(S)", KeyEvent.VK_S); JMenuItem exitNote = new JMenuItem("離開(X)", KeyEvent.VK_X); //為開新檔案加上Listener和動作,動作是將文字區域清為空字串" newFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.setText(""); } } ); //為開啟舊檔加上Listener和動作, 動作要搭配FileChooser,暫不作 oldFile.addActionListener(new ActionListener () { textArea.setText("開啟舊檔還不能使用");

40 //為儲存檔案加上Listener和動作, 動作要搭配FileChooser,暫不作
saveFile.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { textArea.setText("儲存檔案還不能使用"); } } ); //為離開加上Listener和動作, 動作要搭配FileChooser,暫不作 exitNote.addActionListener(new ActionListener () { System.exit(0); //將選項加到檔案中 fileMenu.add(newFile); fileMenu.add(oldFile); fileMenu.add(saveFile); //加一條分隔線 fileMenu.addSeparator(); fileMenu.add(exitNote); //將檔案選單加到功能表上 mb.add(fileMenu); //建立選單功能表的第二個編輯選單 JMenu editMenu = new JMenu("編輯(E)"); editMenu.setMnemonic(KeyEvent.VK_E); //建立編輯選單下的選項 JMenuItem cutEdit = new JMenuItem("剪下(U)", KeyEvent.VK_U); JMenuItem copyEdit = new JMenuItem("複製(C)", KeyEvent.VK_C); JMenuItem pasteEdit = new JMenuItem("貼上(P)", KeyEvent.VK_P); JMenuItem selectAllEdit = new JMenuItem("全選(A)", KeyEvent.VK_A);

41 //為剪下加上Listener和動作,動作是將文字區域中被選的文字剪掉"
cutEdit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.cut(); } } ); //為複製加上Listener和動作 copyEdit.addActionListener(new ActionListener () { textArea.copy(); //為貼上加上Listener和動作 pasteEdit.addActionListener(new ActionListener () { textArea.paste(); //為全選加上Listener和動作 selectAllEdit.addActionListener(new ActionListener () { textArea.selectAll(); //將選項加到編輯中 editMenu.add(cutEdit); editMenu.add(copyEdit); editMenu.add(pasteEdit); editMenu.add(selectAllEdit); //將編輯選單加到功能表上 mb.add(editMenu);

42 //建立選單功能表的第三個字型選單 JMenu fontMenu = new JMenu("字型(T)"); fontMenu.setMnemonic(KeyEvent.VK_T); //建立字型選單下的選項 JCheckBoxMenuItem boldCheck = new JCheckBoxMenuItem("粗體字"); JCheckBoxMenuItem italicCheck = new JCheckBoxMenuItem("斜體字"); ButtonGroup fontsGroup = new ButtonGroup(); JRadioButtonMenuItem smallFonts = new JRadioButtonMenuItem("10號"); JRadioButtonMenuItem midFonts = new JRadioButtonMenuItem("12號"); JRadioButtonMenuItem bigFonts = new JRadioButtonMenuItem("14號"); //為粗體字加上Listener和動作 boldCheck.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent a) { if(a.getStateChange() == ItemEvent.SELECTED) bold = Font.BOLD; else bold = Font.PLAIN; textArea.setFont(new Font("", bold+italic, fonts)); textArea.repaint(); } } ); //為斜體加上Listener和動作 italicCheck.addItemListener(new ItemListener() italic = Font.ITALIC; italic = Font.PLAIN;

43 //為10號字型大小加上Listener和動作
smallFonts.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent a) { if(a.getStateChange() == ItemEvent.SELECTED) fonts= 10; textArea.setFont(new Font("", bold+italic, fonts)); textArea.repaint(); } } ); //為12號字型大小加上Listener和動作 midFonts.addItemListener(new ItemListener() fonts= 12; //為14號字型大小加上Listener和動作 bigFonts.addItemListener(new ItemListener() fonts= 14; //將字型大小選項加到group中 fontsGroup.add(smallFonts); fontsGroup.add(midFonts); fontsGroup.add(bigFonts);

44 //將所有字型選項加到字型選單 fontMenu.add(boldCheck); fontMenu.add(italicCheck); fontMenu.addSeparator(); fontMenu.add(smallFonts); fontMenu.add(midFonts); fontMenu.add(bigFonts); //將字型選單加到功能表上 mb.add(fontMenu); //將選單功能表加到視窗上 panel.add(mb, BorderLayout.NORTH); //設定文字框 textArea = new JTextArea(); //設定字型 textArea.setFont(new Font("Serif", Font.PLAIN, 12)); //設定文字過長時自動折行 textArea.setLineWrap(true); //加上捲軸 sp = new JScrollPane(textArea); //將文字框及捲軸加到視窗上 panel.add(sp, BorderLayout.CENTER); c.add(panel); }

45 加上彈出表單(POPMENU)

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

47 JPopupMenu彈出式選單元件-建立物件
2018/12/4 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("紅色");

48 JPopupMenu彈出式選單元件-事件處理
2018/12/4 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)

49 JPopupMenu建構子與方法 方法: 建構子: JMenuItem add(JMenuItem)
2018/12/4 JPopupMenu建構子與方法 方法: 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() JPopupMenu(String):參數String是標題文字

50 範例1:使用PopupMenu(1/4) 建立Popup Menu的選擇項: import javax.swing.*;
2018/12/4 範例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("紅色"); // 使用字串新增選項

51 範例1:使用PopupMenu(2/4) 傾聽者: addMouseListener(new MouseAdapter()
2018/12/4 範例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) });

52 範例1:使用PopupMenu(3/4) 實作事件處理方法:
2018/12/4 範例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(); // 重繪 }

53 範例1:使用PopupMenu(4/4) 主程式: public static void main(String[] args)
2018/12/4 範例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); // 顯示視窗 }

54 程式設計小叮嚀 要加上popupMenu時,要將與popupMenu相關的元件上加上一個MouseListener,在這個記事本是與textArea有關,所以: //為文字框加上popup textArea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if(e.isPopupTrigger()) popup.show(e.getComponent(), e.getX(), e.getY()); } public void mouseReleased(MouseEvent e) } ); 在同一個視窗上,也可以多個元件與popupMenu相關 NB7

55 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; class MyNote { public static void main(String [] args) NoteBook nb = new NoteBook("記事本"); nb.setSize(500,500); nb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); nb.setVisible(true); } class NoteBook extends JFrame private Container c; private JPanel panel; private JTextArea textArea; private JScrollPane sp; private JMenuBar mb; private int bold = Font.PLAIN; private int italic = Font.PLAIN; private int fonts = 12; private JPopupMenu popup;

56 NoteBook(String title)
{ super(title); c = getContentPane(); panel = new JPanel(); panel.setLayout(new BorderLayout()); //加上選單功能表 mb = new JMenuBar(); //建立選單功能表的第一個檔案選單 JMenu fileMenu = new JMenu("檔案(F)"); fileMenu.setMnemonic(KeyEvent.VK_F); //建立檔案選單下的選項 JMenuItem newFile = new JMenuItem("開新檔案(N)", KeyEvent.VK_N); JMenuItem oldFile = new JMenuItem("開啟舊檔(O)", KeyEvent.VK_O); JMenuItem saveFile = new JMenuItem("儲存檔案(S)", KeyEvent.VK_S); JMenuItem exitNote = new JMenuItem("離開(X)", KeyEvent.VK_X); //為開新檔案加上Listener和動作,動作是將文字區域清為空字串" newFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.setText(""); } } );

57 //為開啟舊檔加上Listener和動作, 動作要搭配FileChooser,暫不作
oldFile.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { textArea.setText("開啟舊檔還不能使用"); } } ); //為儲存檔案加上Listener和動作, 動作要搭配FileChooser,暫不作 saveFile.addActionListener(new ActionListener () { textArea.setText("儲存檔案還不能使用"); //為離開加上Listener和動作, 動作要搭配FileChooser,暫不作 exitNote.addActionListener(new ActionListener () { System.exit(0); //將選項加到檔案中 fileMenu.add(newFile); fileMenu.add(oldFile); fileMenu.add(saveFile); //加一條分隔線 fileMenu.addSeparator(); fileMenu.add(exitNote);

58 //將檔案選單加到功能表上 mb.add(fileMenu); //建立選單功能表的第二個編輯選單 JMenu editMenu = new JMenu("編輯(E)"); editMenu.setMnemonic(KeyEvent.VK_E); //建立編輯選單下的選項 JMenuItem cutEdit = new JMenuItem("剪下(U)", KeyEvent.VK_U); JMenuItem copyEdit = new JMenuItem("複製(C)", KeyEvent.VK_C); JMenuItem pasteEdit = new JMenuItem("貼上(P)", KeyEvent.VK_P); JMenuItem selectAllEdit = new JMenuItem("全選(A)", KeyEvent.VK_A); //為剪下加上Listener和動作,動作是將文字區域中被選的文字剪掉" cutEdit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.cut(); } } ); //為複製加上Listener和動作 copyEdit.addActionListener(new ActionListener () { textArea.copy();

59 //為貼上加上Listener和動作 pasteEdit.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { textArea.paste(); } } ); //為全選加上Listener和動作 selectAllEdit.addActionListener(new ActionListener () { textArea.selectAll(); //將選項加到編輯中 editMenu.add(cutEdit); editMenu.add(copyEdit); editMenu.add(pasteEdit); editMenu.add(selectAllEdit); //將編輯選單加到功能表上 mb.add(editMenu); //建立選單功能表的第三個字型選單 JMenu fontMenu = new JMenu("字型(T)"); fontMenu.setMnemonic(KeyEvent.VK_T);

60 //建立字型選單下的選項 JCheckBoxMenuItem boldCheck = new JCheckBoxMenuItem("粗體字"); JCheckBoxMenuItem italicCheck = new JCheckBoxMenuItem("斜體字"); ButtonGroup fontsGroup = new ButtonGroup(); JRadioButtonMenuItem smallFonts = new JRadioButtonMenuItem("10號"); JRadioButtonMenuItem midFonts = new JRadioButtonMenuItem("12號"); JRadioButtonMenuItem bigFonts = new JRadioButtonMenuItem("14號"); //為粗體字加上Listener和動作 boldCheck.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent a) { if(a.getStateChange() == ItemEvent.SELECTED) bold = Font.BOLD; else bold = Font.PLAIN; textArea.setFont(new Font("", bold+italic, fonts)); textArea.repaint(); } } ); //為斜體加上Listener和動作 italicCheck.addItemListener(new ItemListener() italic = Font.ITALIC; italic = Font.PLAIN;

61 //為10號字型大小加上Listener和動作
smallFonts.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent a) { if(a.getStateChange() == ItemEvent.SELECTED) fonts= 10; textArea.setFont(new Font("", bold+italic, fonts)); textArea.repaint(); } } ); //為12號字型大小加上Listener和動作 midFonts.addItemListener(new ItemListener() fonts= 12; //為14號字型大小加上Listener和動作 bigFonts.addItemListener(new ItemListener() fonts= 14;

62 //將字型大小選項加到group中 fontsGroup.add(smallFonts); fontsGroup.add(midFonts); fontsGroup.add(bigFonts); //將所有字型選項加到字型選單 fontMenu.add(boldCheck); fontMenu.add(italicCheck); fontMenu.addSeparator(); fontMenu.add(smallFonts); fontMenu.add(midFonts); fontMenu.add(bigFonts); //將字型選單加到功能表上 mb.add(fontMenu); //將選單功能表加到視窗上 panel.add(mb, BorderLayout.NORTH); //設定文字框 textArea = new JTextArea(); //設定字型 textArea.setFont(new Font("Serif", Font.PLAIN, 12)); //設定文字過長時自動折行 textArea.setLineWrap(true); //加上捲軸 sp = new JScrollPane(textArea);

63 //在文字框上加裝popup選單 popup = new JPopupMenu(); //建立popup選單下的選項 JMenuItem popCut = new JMenuItem("剪下"); JMenuItem popCopy = new JMenuItem("複製"); JMenuItem popPaste = new JMenuItem("貼上"); JMenuItem popSelectAll = new JMenuItem("全選"); //為剪下加上Listener和動作,動作是將文字區域中被選的文字剪掉" popCut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { textArea.cut(); } } ); //為複製加上Listener和動作 popCopy.addActionListener(new ActionListener () { textArea.copy(); //為貼上加上Listener和動作 popPaste.addActionListener(new ActionListener () { textArea.paste();

64 //為全選加上Listener和動作 popSelectAll.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { textArea.selectAll(); } } ); //將選項加到popup中 popup.add(popCut); popup.add(popCopy); popup.add(popPaste); popup.add(popSelectAll); //為文字框加上popup textArea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if(e.isPopupTrigger()) popup.show(e.getComponent(), e.getX(), e.getY()); public void mouseReleased(MouseEvent e) //將文字框及捲軸加到視窗上 panel.add(sp, BorderLayout.CENTER); c.add(panel);

65 修飾”離開”,出現對話窗 新增關於記事本的對話窗

66 2018/12/4 JDialog最上層容器類別-說明 「對話方塊」(Dialog)是一種很重要的視窗介面,在視窗應用程式的執行過程中,一定會出現一些對話方塊,如果對話方塊會擱置使用者輸入資料至其它視窗,這種對話方塊稱為「程式的對話方塊」(Modal Dialog)。 2018/12/4 66

67 2018/12/4 JDialog最上層容器類別-類別架構 在Swing套件可以使用JOptionPane類別建立程式的對話方塊,否則需要直接使用JDialog類別來建立,其繼承架構如下圖所示: 2018/12/4 67

68 2018/12/4 JDialog最上層容器類別-建構子 JDialog類別的建構子,如下表所示: 2018/12/4 68

69 JDialog最上層容器類別- showMessageDialog()方法
2018/12/4 JDialog最上層容器類別- showMessageDialog()方法 showMessageDialog()方法可以顯示【確定】按鈕的訊息視窗,如下所示: JOptionPane.showMessageDialog(jpane, "這是一個測試的訊息視窗!"); 2018/12/4 69

70 範例2(1/4) 2018/12/4 import javax.swing.*; import java.awt.*;
import java.awt.event.*; public class Ch06_02 extends JFrame { //建構子 public Ch06_02() { super("顯示三種對話方塊範例"); //宣告JButton 物件 final JButton button1, button2, button3; Container c = getContentPane(); //建立JPanel物件 final JPanel jpane = new JPanel(); final JLabel label = new JLabel("測試對話方塊"); jpane.add(label); button1 = new JButton("showMessageDialog按鈕"); //新增滑鼠事件處理 button1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) label.setText("按下showMessageDialog按鈕"); JOptionPane.showMessageDialog(jpane, "這是一個測試的訊息視窗!"); } } ); jpane.add(button1);

71 JDialog最上層容器類別- showConfirmDialog()方法
2018/12/4 JDialog最上層容器類別- showConfirmDialog()方法 showConfirmDialog()方法可以顯示詢問問題的對話方塊,如下所示: int n = JOptionPane.showConfirmDialog(jpane, "您是否已經按下showMessageDialog按鈕? ", "操作問題", JOptionPane.YES_NO_OPTION); 2018/12/4 71

72 範例2(2/4) button2 = new JButton("showConfirmDialog按鈕"); //新增滑鼠事件處理
2018/12/4 範例2(2/4) button2 = new JButton("showConfirmDialog按鈕"); //新增滑鼠事件處理 button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) int n = JOptionPane.showConfirmDialog(jpane, "您是否已經按下showMessageDialog按鈕?", "操作問題", JOptionPane.YES_NO_OPTION); if (n==JOptionPane.YES_OPTION) label.setText("按下是"); else if(n==JOptionPane.NO_OPTION) label.setText("按下否"); label.setText("沒有選擇"); } } ); jpane.add(button2);

73 JDialog最上層容器類別- showOptionDialog()方法(說明)
2018/12/4 JDialog最上層容器類別- showOptionDialog()方法(說明) showOptionDialog()方法可以顯示指定標題文字、圖示、訊息和按鈕的對話方塊,如下所示: Object[] options = {"showMessageDialog按鈕", "showConfirmDialog按鈕"}; int m = JOptionPane.showOptionDialog(jpane, "哪一個按鈕顯示警告訊息?", "操作問題", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 2018/12/4 73

74 範例2(3/4) 2018/12/4 button3 = new JButton("showOptionDialog按鈕");
button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) Object[] options = {"showMessageDialog按鈕", "showConfirmDialog按鈕"}; int m = JOptionPane.showOptionDialog(jpane, "哪一個按鈕顯示警告訊息?", "操作問題", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); switch (m) case JOptionPane.YES_OPTION: label.setText("選showMessageDialog按鈕"); break; case JOptionPane.NO_OPTION: label.setText("選showConfirmDialog按鈕"); default: label.setText("沒有選擇"); } } ); jpane.add(button3); c.add(jpane);

75 範例2(4/4主程式) public static void main(String [] args) {
2018/12/4 範例2(4/4主程式) public static void main(String [] args) { Ch06_02 app = new Ch06_02(); app.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) { System.exit(0); } } ); app.setSize(300,150); app.setVisible(true); }

76 使用SHOWMESSAGEDIALOGD來顯示“關於” 使用SHOWCONFIRMDIALOG來處理“離開”

77 加上說明和關於 //建立選單功能表的第四個說明選單 JMenu aboutMenu = new JMenu("說明");
//建立說明選單下的選項 JMenuItem aboutMenuItem = new JMenuItem("關於"); //為關於加上Listener和動作 aboutMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(panel, "這是我的第1版記事本,我會再接再厲,謝謝使用!"); } } ); //將關於選項加到說明選單上 aboutMenu.add(aboutMenuItem); //將說明選單加到功能表上 mb.add(aboutMenu);

78 修改離開系統 //為離開加上Listener和動作, 動作要搭配FileChooser,暫不作
exitNote.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { int n = JOptionPane.showConfirmDialog(panel, "是否要離開?", "", JOptionPane.YES_NO_OPTION); if (n==JOptionPane.YES_OPTION) System.exit(0); } } );

79 JFILECHOOSER選擇檔案

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

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

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

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

84 利用JFILECHOOSER來 開啟舊檔案
NB9 但此處只取得檔案名稱,並沒有真的開啟檔案

85 //為開啟舊檔加上Listener和動作, 動作要搭配FileChooser
oldFile.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { JFileChooser jfc = new JFileChooser(); int n = jfc.showOpenDialog(NoteBook.this); if( n == JFileChooser.APPROVE_OPTION) { File file = jfc.getSelectedFile(); textArea.setText("開啟舊檔" + file.getName()); } } );

86 利用JFILECHOOSER來儲存檔案 NB9 但此處只取得檔案名稱,並沒有真的儲存

87 //為儲存檔案加上Listener和動作, 動作要搭配FileChooser
saveFile.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { JFileChooser jfc = new JFileChooser(); int n = jfc.showSaveDialog(NoteBook.this); if( n == JFileChooser.APPROVE_OPTION) { File file = jfc.getSelectedFile(); textArea.setText("儲存檔案" + file.getName()); } } );

88 FILEWRITER和BUFFEREDWRITER資料寫入檔案中

89 //為儲存檔案加上Listener和動作, 動作要搭配FileChooser
saveFile.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { jfc = new JFileChooser(); int n = jfc.showSaveDialog(NoteBook.this); if( n == JFileChooser.APPROVE_OPTION) { File file = jfc.getSelectedFile(); try { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write(textArea.getText()); bw.close(); } catch (Exception e) {} } ); NB10

90 FILEREADER和BUFFEREDREADER資料寫入檔案中

91 //為開啟舊檔加上Listener和動作, 動作要搭配FileChooser
oldFile.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent a) { jfc = new JFileChooser(); int n = jfc.showOpenDialog(NoteBook.this); if( n == JFileChooser.APPROVE_OPTION) { File file = jfc.getSelectedFile(); try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String str; while((str = br.readLine()) != null) { textArea.append(str + "\r\n"); } br.close(); catch (Exception e) {} } );


Download ppt "記事本 物件導向系統實務."

Similar presentations


Ads by Google