90 likes | 269 Views
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(" 筆記本 ");
E N D
Ch16 筆記本 物件導向系統實務
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); } }
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); } }
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); } }
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); } }