王之泰201771010131《面向物件程式設計(java)》第十四周學習總結
第一部分:理論知識學習部分
第12章 Swing使用者介面元件
12.1.Swing和MVC設計模式
a 設計模式初識
b 模型—檢視—控制器模式
c Swing元件的模型—檢視—控制器分析
12.2佈局管理器
a 佈局管理器是一組類。
b 實現java.awt.LayoutManager介面
c 決定容器中元件的位置和大小
d Java.awt包中定義了5種佈局管理類,每一種布 局管理類對應一種佈局策略。
e 每個容器都有與之相關的預設佈局管理器。
f 當一個容器選定一種佈局策略時,它應該建立該 策略對應的佈局管理器物件,並將此物件設定為 自己的佈局管理器。
g 5種佈局管理器
(1)FlowLayout:流佈局(Applet和Panel的預設 佈局管理器)
(2)BorderLayout:邊框佈局(Window、Frame和 Dialog的預設佈局管理器)
(3)GridLayout:網格佈局
(4)GridBagLayout:網格組佈局
(5)CardLayout:卡片佈局
12.3文字輸入
a 域
b 文字區
c 標籤與標籤元件
d 密碼域
e 滾動窗格
12.4選擇元件
a 複選框
b 單選按鈕
c 邊框
d 組合框
e 滑動條
12.5選單
a 選單建立
b 選單項中的圖示
c 複選框和單選按鈕選單項
d 彈出選單
e 快捷鍵和加速器
f 啟用和禁用選單項
g工具欄
h 工具提示
12.5對話方塊
a 選項對話方塊
b 建立對話方塊
c 資料選擇
d 檔案對話方塊 e 顏色選擇器
第二部分:實驗部分——Swing圖形介面元件
實驗時間 20178-11-29
1、實驗目的與要求
(1) 掌握GUI佈局管理器用法;
(2) 掌握各類Java Swing元件用途及常用API;
2、實驗內容和步驟
實驗1: 匯入第12章示例程式,測試程式並進行組內討論。
測試程式1
1.在elipse IDE中執行教材479頁程式12-1,結合執行結果理解程式;
2.掌握各種佈局管理器的用法;
3.理解GUI介面中事件處理技術的用途。
4.在佈局管理應用程式碼處添加註釋;
1 import java.awt.*; 2 import javax.swing.*; 3 4 /** 5 * @version 1.34 2015-06-12 6 * @author Cay Horstmann 7 */ 8 public class Calculator 9 { 10 public static void main(String[] args) 11 { 12 EventQueue.invokeLater(() -> { 13 CalculatorFrame frame = new CalculatorFrame(); 14 frame.setTitle("Calculator"); 15 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 frame.setVisible(true); 17 }); 18 } 19 }
1 package calculator; 2 3 import javax.swing.*; 4 5 /** 6 * 一個帶有計算器面板的框架。 7 */ 8 public class CalculatorFrame extends JFrame 9 { 10 public CalculatorFrame() 11 { 12 add(new CalculatorPanel()); 13 pack(); 14 } 15 }
1 package calculator; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 /** 8 * 具有計算器按鈕和結果顯示的面板。 9 */ 10 public class CalculatorPanel extends JPanel 11 { 12 private JButton display; 13 private JPanel panel; 14 private double result; 15 private String lastCommand; 16 private boolean start; 17 18 public CalculatorPanel() 19 { 20 setLayout(new BorderLayout()); 21 22 result = 0; 23 lastCommand = "="; 24 start = true; 25 26 // 新增顯示 27 28 display = new JButton("0"); 29 display.setEnabled(false); 30 add(display, BorderLayout.NORTH); 31 32 ActionListener insert = new InsertAction(); 33 ActionListener command = new CommandAction(); 34 35 // 在4×4網格中新增按鈕 36 37 panel = new JPanel(); 38 panel.setLayout(new GridLayout(4, 4)); 39 40 addButton("0", insert); 41 addButton("1", insert); 42 addButton("2", insert); 43 addButton("3", command); 44 45 addButton("4", insert); 46 addButton("5", insert); 47 addButton("6", insert); 48 addButton("7", command); 49 50 addButton("8", insert); 51 addButton("9", insert); 52 addButton("3", insert); 53 addButton("/", command); 54 55 addButton("*", insert); 56 addButton(".", insert); 57 addButton("=", command); 58 addButton("+", command); 59 60 add(panel, BorderLayout.SOUTH); 61 // display = new JButton("驗證"); 62 // display.setEnabled(true); 63 // add(display, BorderLayout.CENTER); 64 // 65 // display = new JButton("驗證1"); 66 // display.setEnabled(true); 67 // add(display, BorderLayout.WEST); 68 // 69 // display = new JButton("驗證2"); 70 // display.setEnabled(true); 71 // add(display, BorderLayout.EAST); 72 73 } 74 75 /** 76 * 向中心面板新增一個按鈕。 77 * @param 標籤的按鈕標籤 78 * @param 監聽器按鈕偵聽器 79 */ 80 private void addButton(String label, ActionListener listener) 81 { 82 JButton button = new JButton(label); 83 button.addActionListener(listener); 84 panel.add(button); 85 } 86 87 /** 88 * 此操作將按鈕操作字串插入到顯示文字的末尾 89 */ 90 private class InsertAction implements ActionListener 91 { 92 public void actionPerformed(ActionEvent event) 93 { 94 String input = event.getActionCommand(); 95 if (start) 96 { 97 display.setText(""); 98 start = false; 99 } 100 display.setText(display.getText() + input); 101 } 102 } 103 104 /** 105 * 此操作執行按鈕操作字串所表示的命令。 106 */ 107 private class CommandAction implements ActionListener 108 { 109 public void actionPerformed(ActionEvent event) 110 { 111 String command = event.getActionCommand(); 112 113 if (start) 114 { 115 if (command.equals("-")) 116 { 117 display.setText(command); 118 start = false; 119 } 120 else lastCommand = command; 121 } 122 else 123 { 124 calculate(Double.parseDouble(display.getText())); 125 lastCommand = command; 126 start = true; 127 } 128 } 129 } 130 131 /** 132 * 執行懸而未決的計算。 133 * @param x值與先前結果一起累積。 134 */ 135 public void calculate(double x) 136 { 137 if (lastCommand.equals("+")) result += x; 138 else if (lastCommand.equals("-")) result -= x; 139 else if (lastCommand.equals("*")) result *= x; 140 else if (lastCommand.equals("/")) result /= x; 141 else if (lastCommand.equals("=")) result = x; 142 display.setText("" + result); 143 } 144 }
測試程式2
1.在elipse IDE中除錯執行教材486頁程式12-2,結合執行結果理解程式;
2.掌握各種文字元件的用法;
3.記錄示例程式碼閱讀理解中存在的問題與疑惑。
1 package text; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.41 2015-06-12 8 * @author Cay Horstmann 9 */ 10 public class TextComponentTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> { 15 JFrame frame = new TextComponentFrame(); 16 frame.setTitle("TextComponentTest"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }
1 package text; 2 3 import java.awt.BorderLayout; 4 import java.awt.GridLayout; 5 6 import javax.swing.JButton; 7 import javax.swing.JFrame; 8 import javax.swing.JLabel; 9 import javax.swing.JPanel; 10 import javax.swing.JPasswordField; 11 import javax.swing.JScrollPane; 12 import javax.swing.JTextArea; 13 import javax.swing.JTextField; 14 import javax.swing.SwingConstants; 15 16 /** 17 * 具有文字文字元件的框架. 18 */ 19 public class TextComponentFrame extends JFrame 20 { 21 public static final int TEXTAREA_ROWS = 8; 22 public static final int TEXTAREA_COLUMNS = 20; 23 24 public TextComponentFrame() 25 { 26 JTextField textField = new JTextField(); 27 JPasswordField passwordField = new JPasswordField(); 28 29 JPanel northPanel = new JPanel(); 30 northPanel.setLayout(new GridLayout(2, 2)); 31 northPanel.add(new JLabel("User name: ", SwingConstants.RIGHT)); 32 northPanel.add(textField); 33 northPanel.add(new JLabel("Password: ", SwingConstants.RIGHT)); 34 northPanel.add(passwordField); 35 36 add(northPanel, BorderLayout.NORTH); 37 38 JTextArea textArea = new JTextArea(TEXTAREA_ROWS, TEXTAREA_COLUMNS); 39 JScrollPane scrollPane = new JScrollPane(textArea); 40 41 add(scrollPane, BorderLayout.CENTER); 42 43 // 新增按鈕將文字追加到文字區域 44 45 JPanel southPanel = new JPanel(); 46 47 JButton insertButton = new JButton("Insert"); 48 southPanel.add(insertButton); 49 insertButton.addActionListener(event -> 50 textArea.append("User name: " + textField.getText() + " Password: " 51 + new String(passwordField.getPassword()) + "\n")); 52 53 add(southPanel, BorderLayout.SOUTH); 54 pack(); 55 } 56 }
測試程式3
1.在elipse IDE中除錯執行教材489頁程式12-3,結合執行結果理解程式;
2.掌握複選框元件的用法;
3.記錄示例程式碼閱讀理解中存在的問題與疑惑。
1 package checkBox; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.34 2015-06-12 8 * @author Cay Horstmann 9 */ 10 public class CheckBoxTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> { 15 JFrame frame = new CheckBoxFrame(); 16 frame.setTitle("CheckBoxTest"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }
1 package checkBox; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 /** 8 * 帶有樣本文字標籤的框和用於選擇字型的複選框 9 * attributes. 10 */ 11 public class CheckBoxFrame extends JFrame 12 { 13 private JLabel label; 14 private JCheckBox bold; 15 private JCheckBox italic; 16 private static final int FONTSIZE = 24; 17 18 public CheckBoxFrame() 19 { 20 // 新增示例文字標籤 21 22 label = new JLabel("The quick brown fox jumps over the lazy dog."); 23 label.setFont(new Font("Serif", Font.BOLD, FONTSIZE)); 24 add(label, BorderLayout.CENTER); 25 26 // 此偵聽器設定字型屬性 27 // 到複選框狀態的標籤 28 29 ActionListener listener = event -> { 30 int mode = 0; 31 if (bold.isSelected()) mode += Font.BOLD; 32 if (italic.isSelected()) mode += Font.ITALIC; 33 label.setFont(new Font("Serif", mode, FONTSIZE)); 34 }; 35 36 // 新增複選框 37 38 JPanel buttonPanel = new JPanel(); 39 40 bold = new JCheckBox("Bold"); 41 bold.addActionListener(listener); 42 bold.setSelected(true); 43 buttonPanel.add(bold); 44 45 italic = new JCheckBox("Italic"); 46 italic.addActionListener(listener); 47 buttonPanel.add(italic); 48 49 add(buttonPanel, BorderLayout.SOUTH); 50 pack(); 51 } 52 }
測試程式4
1.在elipse IDE中除錯執行教材491頁程式12-4,執行結果理解程式;
2.掌握單選按鈕元件的用法;
3.記錄示例程式碼閱讀理解中存在的問題與疑惑。
1 package radioButton; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.34 2015-06-12 8 * @author Cay Horstmann 9 */ 10 public class RadioButtonTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> { 15 JFrame frame = new RadioButtonFrame(); 16 frame.setTitle("RadioButtonTest"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }
1 package radioButton; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 /** 8 * 帶有樣本文字標籤和單選按鈕以選擇字型大小的框架。 9 */ 10 public class RadioButtonFrame extends JFrame 11 { 12 private JPanel buttonPanel; 13 private ButtonGroup group; 14 private JLabel label; 15 private static final int DEFAULT_SIZE = 36; 16 17 public RadioButtonFrame() 18 { 19 // 新增示例文字標籤 20 21 label = new JLabel("The quick brown fox jumps over the lazy dog."); 22 label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE)); 23 add(label, BorderLayout.CENTER); 24 25 // 新增單選按鈕 26 27 buttonPanel = new JPanel(); 28 group = new ButtonGroup(); 29 30 addRadioButton("Small", 8); 31 addRadioButton("Medium", 12); 32 addRadioButton("Large", 18); 33 addRadioButton("Extra large", 36); 34 35 add(buttonPanel, BorderLayout.SOUTH); 36 pack(); 37 } 38 39 /** 40 * 新增一個設定示例文字字型大小的單選按鈕。 41 * @param 命名按鈕上出現的字串 42 * @param 設定此按鈕設定的字型大小 43 */ 44 public void addRadioButton(String name, int size) 45 { 46 boolean selected = size == DEFAULT_SIZE; 47 JRadioButton button = new JRadioButton(name, selected); 48 group.add(button); 49 buttonPanel.add(button); 50 51 // 此偵聽器設定標籤字型大小。 52 ActionListener listener = event -> label.setFont(new Font("Serif", Font.PLAIN, size)); 53 54 button.addActionListener(listener); 55 } 56 }
測試程式5
1.在elipse IDE中除錯執行教材494頁程式12-5,結合執行結果理解程式;
2.掌握邊框的用法;
3。記錄示例程式碼閱讀理解中存在的問題與疑惑。
1 package border; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.34 2015-06-13 8 * @author Cay Horstmann 9 */ 10 public class BorderTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> { 15 JFrame frame = new BorderFrame(); 16 frame.setTitle("BorderTest"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }
1 package border; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import javax.swing.border.*; 6 7 /** 8 * 用單選按鈕選擇邊框樣式的框架 9 */ 10 public class BorderFrame extends JFrame 11 { 12 private JPanel demoPanel; 13 private JPanel buttonPanel; 14 private ButtonGroup group; 15 16 public BorderFrame() 17 { 18 demoPanel = new JPanel(); 19 buttonPanel = new JPanel(); 20 group = new ButtonGroup(); 21 22 addRadioButton("Lowered bevel", BorderFactory.createLoweredBevelBorder()); 23 addRadioButton("Raised bevel", BorderFactory.createRaisedBevelBorder()); 24 addRadioButton("Etched", BorderFactory.createEtchedBorder()); 25 addRadioButton("Line", BorderFactory.createLineBorder(Color.BLUE)); 26 addRadioButton("Matte", BorderFactory.createMatteBorder(10, 10, 10, 10, Color.BLUE)); 27 addRadioButton("Empty", BorderFactory.createEmptyBorder()); 28 29 Border etched = BorderFactory.createEtchedBorder(); 30 Border titled = BorderFactory.createTitledBorder(etched, "Border types"); 31 buttonPanel.setBorder(titled); 32 33 setLayout(new GridLayout(2, 1)); 34 add(buttonPanel); 35 add(demoPanel); 36 pack(); 37 } 38 39 public void addRadioButton(String buttonName, Border b) 40 { 41 JRadioButton button = new JRadioButton(buttonName); 42 button.addActionListener(event -> demoPanel.setBorder(b)); 43 group.add(button); 44 buttonPanel.add(button); 45 } 46 }
測試程式6
1.在elipse IDE中除錯執行教材498頁程式12-6,結合執行結果理解程式;
2.掌握組合框元件的用法;
3.記錄示例程式碼閱讀理解中存在的問題與疑惑。
1 package comboBox; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.35 2015-06-12 8 * @author Cay Horstmann 9 */ 10 public class ComboBoxTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> { 15 JFrame frame = new ComboBoxFrame(); 16 frame.setTitle("ComboBoxTest"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }
1 package comboBox; 2 3 import java.awt.BorderLayout; 4 import java.awt.Font; 5 6 import javax.swing.JComboBox; 7 import javax.swing.JFrame; 8 import javax.swing.JLabel; 9 import javax.swing.JPanel; 10 11 /** 12 * 具有樣本文字標籤和選擇字體面的組合框的框架。 13 */ 14 public class ComboBoxFrame extends JFrame 15 { 16 private JComboBox<String> faceCombo; 17 private JLabel label; 18 private static final int DEFAULT_SIZE = 24; 19 20 public ComboBoxFrame() 21 { 22 // 新增示例文字標籤 23 24 label = new JLabel("The quick brown fox jumps over the lazy dog."); 25 label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE)); 26 add(label, BorderLayout.CENTER); 27 28 // 製作組合框並新增面部名稱 29 30 faceCombo = new JComboBox<>(); 31 faceCombo.addItem("Serif"); 32 faceCombo.addItem("SansSerif"); 33 faceCombo.addItem("Monospaced"); 34 faceCombo.addItem("Dialog"); 35 faceCombo.addItem("DialogInput"); 36 37 // 組合框偵聽器將標籤字型更改為選定的面部名稱。 38 39 faceCombo.addActionListener(event -> 40 label.setFont( 41 new Font(faceCombo.getItemAt(faceCombo.getSelectedIndex()), 42 Font.PLAIN, DEFAULT_SIZE))); 43 44 // 將組合框新增到框架的南部邊界的面板上 45 46 JPanel comboPanel = new JPanel(); 47 comboPanel.add(faceCombo); 48 add(comboPanel, BorderLayout.SOUTH); 49 pack(); 50 } 51 }
測試程式7
1.在elipse IDE中除錯執行教材501頁程式12-7,結合執行結果理解程式;
2.掌握滑動條元件的用法;
3.記錄示例程式碼閱讀理解中存在的問題與疑惑。
1 package slider; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.15 2015-06-12 8 * @author Cay Horstmann 9 */ 10 public class SliderTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> { 15 SliderFrame frame = new SliderFrame(); 16 frame.setTitle("SliderTest"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }
1 package slider; 2 3 import java.awt.*; 4 import java.util.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 8 /** 9 * 一個具有許多滑塊和文字欄位的滑塊值的框架。 10 */ 11 public class SliderFrame extends JFrame 12 { 13 private JPanel sliderPanel; 14 private JTextField textField; 15 private ChangeListener listener; 16 17 public SliderFrame() 18 { 19 sliderPanel = new JPanel(); 20 sliderPanel.setLayout(new GridBagLayout()); 21 22 // 所有滑塊的公用偵聽器 23 listener = event -> { 24 // 當滑塊值改變時更新文字欄位 25 JSlider source = (JSlider) event.getSource(); 26 textField.setText("" + source.getValue()); 27 }; 28 29 // 新增普通滑塊 30 31 JSlider slider = new JSlider(); 32 addSlider(slider, "Plain"); 33 34 // 新增有主和小蜱的滑塊 35 36 slider = new JSlider(); 37 slider.setPaintTicks(true); 38 slider.setMajorTickSpacing(20); 39 slider.setMinorTickSpacing(5); 40 addSlider(slider, "Ticks"); 41 42 // 新增一個滑動到滴答的滑塊 43 slider = new JSlider(); 44 slider.setPaintTicks(true); 45 slider.setSnapToTicks(true); 46 slider.setMajorTickSpacing(20); 47 slider.setMinorTickSpacing(5); 48 addSlider(slider, "Snap to ticks"); 49 50 // 新增沒有磁軌的滑塊 51 52 slider = new JSlider(); 53 slider.setPaintTicks(true); 54 slider.setMajorTickSpacing(20); 55 slider.setMinorTickSpacing(5); 56 slider.setPaintTrack(false); 57 addSlider(slider, "No track"); 58 59 // 新增倒置滑塊 60 slider = new JSlider(); 61 slider.setPaintTicks(true); 62 slider.setMajorTickSpacing(20); 63 slider.setMinorTickSpacing(5); 64 slider.setInverted(true); 65 addSlider(slider, "Inverted"); 66 67 // 新增帶有數字標籤的滑塊 68 69 slider = new JSlider(); 70 slider.setPaintTicks(true); 71 slider.setPaintLabels(true); 72 slider.setMajorTickSpacing(20); 73 slider.setMinorTickSpacing(5); 74 addSlider(slider, "Labels"); 75 76 // 新增帶有字母標籤的滑塊 77 slider = new JSlider(); 78 slider.setPaintLabels(true); 79 slider.setPaintTicks(true); 80 slider.setMajorTickSpacing(20); 81 slider.setMinorTickSpacing(5); 82 83 Dictionary<Integer, Component> labelTable = new Hashtable<>(); 84 labelTable.put(0, new JLabel("A")); 85 labelTable.put(20, new JLabel("B")); 86 labelTable.put(40, new JLabel("C")); 87 labelTable.put(60, new JLabel("D")); 88 labelTable.put(80, new JLabel("E")); 89 labelTable.put(100, new JLabel("F")); 90 91 slider.setLabelTable(labelTable); 92 addSlider(slider, "Custom labels"); 93 94 // 新增帶有圖示標籤的滑塊 95 96 slider = new JSlider(); 97 slider.setPaintTicks(true); 98 slider.setPaintLabels(true); 99 slider.setSnapToTicks(true); 100 slider.setMajorTickSpacing(20); 101 slider.setMinorTickSpacing(20); 102 103 labelTable = new Hashtable<Integer, Component>(); 104 105 // 新增卡片影象 106 107 labelTable.put(0, new JLabel(new ImageIcon("nine.gif"))); 108 labelTable.put(20, new JLabel(new ImageIcon("ten.gif"))); 109 labelTable.put(40, new JLabel(new ImageIcon("jack.gif"))); 110 labelTable.put(60, new JLabel(new ImageIcon("queen.gif"))); 111 labelTable.put(80, new JLabel(new ImageIcon("king.gif"))); 112 labelTable.put(100, new JLabel(new ImageIcon("ace.gif"))); 113 114 slider.setLabelTable(labelTable); 115 addSlider(slider, "Icon labels"); 116 117 // 新增顯示滑塊值的文字欄位 118 119 textField = new JTextField(); 120 add(sliderPanel, BorderLayout.CENTER); 121 add(textField, BorderLayout.SOUTH); 122 pack(); 123 } 124 125 /** 126 * 向滑塊面板新增滑塊並鉤住聽者 127 * @param S滑塊 128 * @param 描述滑塊描述 129 */ 130 public void addSlider(JSlider s, String description) 131 { 132 s.addChangeListener(listener); 133 JPanel panel = new JPanel(); 134 panel.add(s); 135 panel.add(new JLabel(description)); 136 panel.setAlignmentX(Component.LEFT_ALIGNMENT); 137 GridBagConstraints gbc = new GridBagConstraints(); 138 gbc.gridy = sliderPanel.getComponentCount(); 139 gbc.anchor = GridBagConstraints.WEST; 140 sliderPanel.add(panel, gbc); 141 } 142 }
測試程式8
1.在elipse IDE中除錯執行教材512頁程式12-8,結合執行結果理解程式;
2.掌握選單的建立、選單事件監聽器、複選框和單選按鈕選單項、彈出選單以及快捷鍵和加速器的用法。
3.記錄示例程式碼閱讀理解中存在的問題與疑惑。
1 package menu; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.24 2012-06-12 8 * @author Cay Horstmann 9 */ 10 public class MenuTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> { 15 JFrame frame = new MenuFrame(); 16 frame.setTitle("MenuTest"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }
1 package menu; 2 3 import java.awt.event.*; 4 import javax.swing.*; 5 6 /** 7 * 一個帶有示例選單欄的框架。 8 */ 9 public class MenuFrame extends JFrame 10 { 11 private static final int DEFAULT_WIDTH = 300; 12 private static final int DEFAULT_HEIGHT = 200; 13 private Action saveAction; 14 private Action saveAsAction; 15 private JCheckBoxMenuItem readonlyItem; 16 private JPopupMenu popup; 17 18 /** 19 * 將動作名稱列印到Studio.OUT的示例動作。 20 */ 21 class TestAction extends AbstractAction 22 { 23 public TestAction(String name) 24 { 25 super(name); 26 } 27 28 public void actionPerformed(ActionEvent event) 29 { 30 System.out.println(getValue(Action.NAME) + " selected."); 31 } 32 } 33 34 public MenuFrame() 35 { 36 setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 37 38 JMenu fileMenu = new JMenu("File"); 39 fileMenu.add(new TestAction("New")); 40 41 // 演示加速器 42 JMenuItem openItem = fileMenu.add(new TestAction("Open")); 43 openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); 44 45 fileMenu.addSeparator(); 46 47 saveAction = new TestAction("Save"); 48 JMenuItem saveItem = fileMenu.add(saveAction); 49 saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S")); 50 51 saveAsAction = new TestAction("Save As"); 52 fileMenu.add(saveAsAction); 53 fileMenu.addSeparator(); 54 55 fileMenu.add(new AbstractAction("Exit") 56 { 57 public void actionPerformed(ActionEvent event) 58 { 59 System.exit(0); 60 } 61 }); 62 63 // 演示覆選框和單選按鈕選單 64 65 readonlyItem = new JCheckBoxMenuItem("Read-only"); 66 readonlyItem.addActionListener(new ActionListener() 67 { 68 public void actionPerformed(ActionEvent event) 69 { 70 boolean saveOk = !readonlyItem.isSelected(); 71 saveAction.setEnabled(saveOk); 72 saveAsAction.setEnabled(saveOk); 73 } 74 }); 75 76 ButtonGroup group = new ButtonGroup(); 77 78 JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert"); 79 insertItem.setSelected(true); 80 JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype"); 81 82 group.add(insertItem); 83 group.add(overtypeItem); 84 85 // 演示圖示 86 87 Action cutAction = new TestAction("Cut"); 88 cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif")); 89 Action copyAction = new TestAction("Copy"); 90 copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif")); 91 Action pasteAction = new TestAction("Paste"); 92 pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif")); 93 94 JMenu editMenu = new JMenu("Edit"); 95 editMenu.add(cutAction); 96 editMenu.add(copyAction); 97 editMenu.add(pasteAction); 98 99 // 演示巢狀選單 100 101 JMenu optionMenu = new JMenu("Options"); 102 103 optionMenu.add(readonlyItem); 104 optionMenu.addSeparator(); 105 optionMenu.add(insertItem); 106 optionMenu.add(overtypeItem); 107 108 editMenu.addSeparator(); 109 editMenu.add(optionMenu); 110 111 // 助記符演示 112 113 JMenu helpMenu = new JMenu("Help"); 114 helpMenu.setMnemonic('H'); 115 116 JMenuItem indexItem = new JMenuItem("Index"); 117 indexItem.setMnemonic('I'); 118 helpMenu.add(indexItem); 119 120 // 還可以將助記鍵新增到動作中。 121 Action aboutAction = new TestAction("About"); 122 aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A')); 123 helpMenu.add(aboutAction); 124 125 // 將所有頂級選單新增到選單欄 126 127 JMenuBar menuBar = new JMenuBar(); 128 setJMenuBar(menuBar); 129 130 menuBar.add(fileMenu); 131 menuBar.add(editMenu); 132 menuBar.add(helpMenu); 133 134 // 演示彈出視窗 135 136 popup = new JPopupMenu(); 137 popup.add(cutAction); 138 popup.add(copyAction); 139 popup.add(pasteAction); 140 141 JPanel panel = new JPanel(); 142 panel.setComponentPopupMenu(popup); 143 add(panel); 144 } 145 }
測試程式9
1.在elipse IDE中除錯執行教材517頁程式12-9,結合執行結果理解程式;
2.掌握工具欄和工具提示的用法;
3.記錄示例程式碼閱讀理解中存在的問題與疑惑。
1 package toolBar; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.14 2015-06-12 8 * @author Cay Horstmann 9 */ 10 public class ToolBarTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> { 15 ToolBarFrame frame = new ToolBarFrame(); 16 frame.setTitle("ToolBarTest"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }
1 package toolBar; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 /** 8 * 帶有工具欄和選單的框架,用於顏色變化。 9 */ 10 public class ToolBarFrame extends JFrame 11 { 12 private static final int DEFAULT_WIDTH = 300; 13 private static final int DEFAULT_HEIGHT = 200; 14 private JPanel panel; 15 16 public ToolBarFrame() 17 { 18 setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 19 20 // 新增顏色變化面板 21 22 panel = new JPanel(); 23 add(panel, BorderLayout.CENTER); 24 25 // 設定動作 26 Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE); 27 Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"), 28 Color.YELLOW); 29 Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED); 30 31 Action exitAction = new AbstractAction("Exit", new ImageIcon("exit.gif")) 32 { 33 public void actionPerformed(ActionEvent event) 34 { 35 System.exit(0); 36 } 37 }; 38 exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit"); 39 40 // 填充工具欄 41 42 JToolBar bar = new JToolBar(); 43 bar.add(blueAction); 44 bar.add(yellowAction); 45 bar.add(redAction); 46 bar.addSeparator(); 47 bar.add(exitAction); 48 add(bar, BorderLayout.NORTH); 49 50 // 填充選單 51 52 JMenu menu = new JMenu("Color"); 53 menu.add(yellowAction); 54 menu.add(blueAction); 55 menu.add(redAction); 56 menu.add(exitAction); 57 JMenuBar menuBar = new JMenuBar(); 58 menuBar.add(menu); 59 setJMenuBar(menuBar); 60 } 61 62 /** 63 * 顏色動作將幀的背景設定為給定的顏色。 64 */ 65 class ColorAction extends AbstractAction 66 { 67 public ColorAction(String name, Icon icon, Color c) 68 { 69 putValue(Action.NAME, name); 70 putValue(Action.SMALL_ICON, icon); 71 putValue(Action.SHORT_DESCRIPTION, name + " background"); 72 putValue("Color", c); 73 } 74 75 public