1 / 9

Ch16 筆記本

Ch16 筆記本. 物件導向系統實務. Step 1: 建立視窗. Win_NoteBook_01 import javax.swing.*; import java.awt.*; import java.awt.event.*; class Win_NoteBook_01 extends JFrame implements ActionListener { private Container c; Win_NoteBook_01() { super(" 筆記本 ");

lorant
Télécharger la présentation

Ch16 筆記本

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. Ch16 筆記本 物件導向系統實務

  2. Step 1:建立視窗

  3. Win_NoteBook_01 import javax.swing.*; import java.awt.*; import java.awt.event.*; class Win_NoteBook_01 extends JFrame implements ActionListener { private Container c; Win_NoteBook_01() { super("筆記本"); c = getContentPane(); c.setBackground(Color.white); } public void actionPerformed (ActionEvent e) { } } Win_NoteBook_Main.java import javax.swing.*; import java.awt.*; import java.awt.event.*; class Win_NoteBook_Main { public static void main(String [] args) { Win_NoteBook_01 NB = new Win_NoteBook_01(); NB.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } } ); NB.setSize(300, 200); NB.setVisible(true); } }

  4. Step2: 加上下式拉式選單

  5. import javax.swing.*; import java.awt.*; import java.awt.event.*; class Win_NoteBook_02 extends JFrame implements ActionListener { private Container c; private JMenuBar jmb = new JMenuBar(); private JMenu file = new JMenu("檔案(F)"); private JMenu edit = new JMenu("編輯(E)"); private JMenu help = new JMenu("說明(H)"); Win_NoteBook_02() { super("筆記本"); c = getContentPane(); c.setBackground(Color.white); setJMenuBar(jmb); file.setMnemonic(KeyEvent.VK_F); edit.setMnemonic(KeyEvent.VK_E); help.setMnemonic(KeyEvent.VK_H); JMenuItem item; file.add(item = new JMenuItem("新增(N)", KeyEvent.VK_N)); item.addActionListener(this); file.add(item = new JMenuItem("開啟(O)", KeyEvent.VK_O)); item.addActionListener(this); file.add(item = new JMenuItem("儲存(S)", KeyEvent.VK_S)); item.addActionListener(this); file.addSeparator(); //分隔線 file.add(item = new JMenuItem("離開(X)", KeyEvent.VK_X)); item.addActionListener(this); jmb.add(file); } public void actionPerformed (ActionEvent e) { if(e.getActionCommand()=="離開(X)") System.exit(0); } }

  6. Step 3:加入有捲軸的文字區域

  7. import javax.swing.*; import java.awt.*; import java.awt.event.*; class Win_NoteBook_03 extends JFrame implements ActionListener { private Container c; private JMenuBar jmb = new JMenuBar(); private JMenu file = new JMenu("檔案(F)"); private JMenu edit = new JMenu("編輯(E)"); private JMenu help = new JMenu("說明(H)"); private JTextArea area = new JTextArea(15,30); Win_NoteBook_03() { super("筆記本"); c = getContentPane(); c.setBackground(Color.white); setJMenuBar(jmb); file.setMnemonic(KeyEvent.VK_F); edit.setMnemonic(KeyEvent.VK_E); help.setMnemonic(KeyEvent.VK_H); JMenuItem item; file.add(item = new JMenuItem("新增(N)", KeyEvent.VK_N)); item.addActionListener(this); file.add(item = new JMenuItem("開啟(O)", KeyEvent.VK_O)); item.addActionListener(this); file.add(item = new JMenuItem("儲存(S)", KeyEvent.VK_S)); item.addActionListener(this); file.addSeparator(); //分隔線 file.add(item = new JMenuItem("離開(X)", KeyEvent.VK_X)); item.addActionListener(this); jmb.add(file); JScrollPane scroll = new JScrollPane(area); c.add(scroll); } public void actionPerformed (ActionEvent e) { if(e.getActionCommand()=="離開(X)") System.exit(0); } }

  8. Step 4:加入檔案選擇元件

  9. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.filechooser.*; class Win_NoteBook_04 extends JFrame implements ActionListener { private Container c; private JMenuBar jmb = new JMenuBar(); private JMenu file = new JMenu("檔案(F)"); private JMenu edit = new JMenu("編輯(E)"); private JMenu help = new JMenu("說明(H)"); private JTextArea area = new JTextArea(15,30); private JFileChooser jfc = new JFileChooser(); Win_NoteBook_04() { super("筆記本"); c = getContentPane(); c.setBackground(Color.white); setJMenuBar(jmb); file.setMnemonic(KeyEvent.VK_F); edit.setMnemonic(KeyEvent.VK_E); help.setMnemonic(KeyEvent.VK_H); JMenuItem item; file.add(item = new JMenuItem("新增(N)", KeyEvent.VK_N)); item.addActionListener(this); file.add(item = new JMenuItem("開啟(O)", KeyEvent.VK_O)); item.addActionListener(this); file.add(item = new JMenuItem("儲存(S)", KeyEvent.VK_S)); item.addActionListener(this); file.addSeparator(); //分隔線 file.add(item = new JMenuItem("離開(X)", KeyEvent.VK_X)); item.addActionListener(this); jmb.add(file); JScrollPane scroll = new JScrollPane(area); c.add(scroll); } public void actionPerformed (ActionEvent e) { if(e.getActionCommand() == "新增(N)") area.setText(""); if(e.getActionCommand() == "開啟(O)") { int n = jfc.showOpenDialog(Win_NoteBook_04.this); if ( n == JFileChooser.APPROVE_OPTION) { File selectedFile = jfc.getSelectedFile(); area.append("開啟檔案:"); area.append(selectedFile.getName() + "\n"); } } if(e.getActionCommand() == "儲存(S)") { int n = jfc.showSaveDialog(Win_NoteBook_04.this); if (n == JFileChooser.APPROVE_OPTION) { File selectedFile = jfc.getSelectedFile(); area.append("儲存檔案:"); area.append(selectedFile.getName() + "\n"); } } if(e.getActionCommand()=="離開(X)") System.exit(0); } }

More Related