201871010107-公海瑜《面向對象程序設計(java)》第十四周學習總結
201871010107-公海瑜《面向對象程序設計(java)》第十四周學習總結
| 項目 | 內容 |
| 這個作業屬于哪個課程 | http://www.rzrgm.cn/nwnu-daizh/ |
| 這個作業的要求在哪里 | http://www.rzrgm.cn/nwnu-daizh/p/11953993.html |
| 作業學習目標 |
(1)掌握GUI布局管理器用法; (2)掌握Java Swing文本輸入組件用途及常用API; (3)掌握Java Swing選擇輸入組件用途及常用API。 |
第一部分:總結第十二章本周理論知識
1.Swing和MVC設計模式
a.設計模式(Design pattern)是設計者一種流行的思考設計問題的方法,是一套被反復使用,多數人知曉的,經過分類編目的,代碼設計經驗的總結。使用設計模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性。
b.每一個模式描述了一個不斷重復發生的設計問題,以及該問題的核心解決方案
c.模型-視圖-控制器設計模式(Model –ViewController )是Java EE平臺下創建 Web 應用程序的重要設計模式。
MVC設計模式
– Model(模型):是程序中用于處理程序數據邏輯的部分,通常模型負責在數據庫中存取數據。
– View(視圖):是程序中處理數據顯示的部分,通常視圖依據模型存取的數據創建。
– Controller(控制器):是程序中處理用戶交互的部分。通常控制器負責從視圖讀取數據,控制用戶輸入,并向模型發送數據。
2.布局管理器
1.為了設計美觀合理的GUI界面,需要考慮組件在容器組件中的位置和相互關系,就需要學習布局設計的知識。
2.在java的GUI應用程序界面設計中,布局控制通過為容器設置布局管理器來實現的
5種布局管理器
(1)FlowLayout: 流布局(Applet和Panel的默認布局管理器)
JPanel對象的默認布局管理器為FlowLayout,組件加入JPanel中總是處于中央,一行可以排列多個組件,如果一行的空間容納不下所有的組件則換行。當頂層窗口縮放時,JPanel中組件的大小不會隨之縮放。
(2)BorderLayout:邊框布局( Window、Frame和Dialog的默認布局管理器)
是JFrame的內容窗格的默認布局管理器,可以選擇將空間放在內容窗格的東、南、西、北、中。 且將組件加入其中時,組件會充滿其對應的整個區域,如果在這個方位再加入一個組件,會覆蓋原本存在的組件。當頂層窗口縮放時,東南西北的組件不會隨之變化,中部的組件會等比例變化。
如果要在某方法并排加入幾個組件,則可以先將組件加入JPanel中,再放入邊框布局管理器。
BorderLayout的常量定義為字符串
frame.add(new JButton("Yes"),BorderLayout.SOUTH);
(3)GridLayout: 網格布局
①GridLayout():生成一個單行單列的網格布局
②GridLayout(int rows,int cols):生成一個設定行數和列數的網格布局
③GridLayout(int rows,int columns,int hgap,int vgap):可設置組件之間的水平和垂直間隔
(4)GridBagLayout: 網格組布局
(5)CardLayout :卡片布局 通過setLayout( )方法為容器設置新的布局。
容器組件名.setLayout( 布局類對象名)。
3.文本輸入
(1)擴展于JTextComponent的JTextField和JTextArea
JTextField和JTextArea都用于文本輸入,其中JTextField接收單行文本的輸入,而JTextArea可接收多行文本的輸入。
列數為文本域的寬度,如果希望文本域最多能輸入N個字符,則將寬度設置為N
JTextField text = new JTextField("Input Here",20);
第二個構造函數可以指定文本區顯示的行數和列數。如果需要設置滾動條,則需要將文本區加入JScrollPane中,再講JScrollPane插入容器。
JTextArea area = new TextArea(4,10);
JScrollPane pane = new JScrollPane(area);
panel.add(pane);
(2)擴展于JTextField的JPasswordField
接受單行輸入,輸入字符被特殊字符掩蓋
(3)JLabel
沒有任何修飾,不能響應用戶輸入,只是容納文本的組件??梢栽O置標簽的顯示文字、圖標以及對齊方式
其中對齊方式是SwingConstants里的常量,如LEFT/RIGHT/CENTER等
JLabel label = new JLabel("User Name:",SwingConstants.RIGHT);
4.選擇組件
(1)JCheckBox
復選框自動帶有標簽和圖標,在構造時可以提供,當用戶選中復選框時會觸發動作事件。
JCheckBox box = new JCheckBox("Bold");
自帶標簽和圖標。單選鈕只能多選其一,要打到這種效果需要把所有的單選鈕加入ButtonGroup的對象里,從而使得新按鈕被按下時,取消前一個選中的按鈕的狀態。ButtonGroup直接擴展于Object類,所以單選鈕需加入容器中進行布局,ButtonGroup和容器(如JPanel)是相互獨立的。 選中時觸發動作事件。
(3)邊框(Border)
任何繼承自JComponent的組件都可以使用邊框(void setBorder(Border b))。常用的方法是將組件放入容器中,然后容器使用邊框。是通過調用BorderFactory的靜態方法構建邊框。 同時可以為邊框設置標題:
Border etch = BorderFactory.createEtchedBorder();
Border title = BorderFactory.createTitleBorder(etch,"Title");
panel.setBorder(title);
(4)組合框
JComboBox< T>是泛型類,構建時需注意。
組合框不僅有下拉選擇的功能,還具有文本框編輯的功能。
獲得當前選中內容:
combo.getItemAt(combo.getSelectedIndex());
//Object getItemAt(int index)
當用戶從組合框中選中一個選項時,組合框就會產生一個動作事件。
(5)滑動條(JSlider)
滑動條在構造時默認是橫向,如果需要縱向滑動條:
JSlider s = new JSlider(SwingConstants.VERTICAL,min,max,initialValue);
當滑動條滑動時,會觸發ChangeEvent,需要調用addChangeListener()并且安裝一個實現了ChangeListener接口的對象。這個接口只有一個StateChanged方法
如果需要顯示滑動條的刻度,則setPaintTicks(true);
如果要將滑動條強制對準刻度,則setSnapToTicks(true);
如果要為滑動條設置標簽,則需要先構建一個Hashtable< Integer,Component>,將數字與標簽對應起來,再調用setLabelTable(Dictionary label);
5.菜單
分為JMenuBar/JMenu/JMenuItem,當選擇菜單項時會觸發一個動作事件,需要注冊監聽器監聽
6.對話框
對話框是一種大小不能變化、不能有菜單的容器窗口; 對話框不能作為一個應用程序的主框架,而必須包含在其他的容器中。
對話框分為模式對話框和無模對話框,模式對話框就是未處理此對話框之前不允許與其他窗口交互。
文件對話框(JFileChooser類)
顏色對話框(JColorChooser類)
第二部分:實驗部分
實驗1: 導入第12章示例程序,測試程序并進行組內討論。
測試程序1
l 在elipse IDE中運行教材479頁程序12-1,結合運行結果理解程序;
l 掌握布局管理器的用法;
l 理解GUI界面中事件處理技術的用途。
l 在布局管理應用代碼處添加注釋;
程序代碼:
package calculator; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class Calculator { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new CalculatorFrame(); frame.setTitle("Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package calculator; import javax.swing.*; /** * A frame with a calculator panel. */ public class CalculatorFrame extends JFrame { public CalculatorFrame() { add(new CalculatorPanel()); pack(); } }
package calculator; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 帶有計算器按鈕和結果顯示的面板。 */ public class CalculatorPanel extends JPanel { private JButton display; private JPanel panel; private double result; private String lastCommand; private boolean start; public CalculatorPanel() { //為容器設置布局管理器,為邊框布局管理器 setLayout(new BorderLayout()); result = 0; lastCommand = "="; start = true; // 添加頁面顯示 display = new JButton("0"); display.setEnabled(false); add(display, BorderLayout.NORTH); var insert = new InsertAction(); var command = new CommandAction(); // 在4*4的網格中添加按鈕 panel = new JPanel(); panel.setLayout(new GridLayout(4, 4)); //網格布局管理器:4行4列 addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command); addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command); addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command); addButton("0", insert); addButton(".", insert); addButton("=", command); addButton("+", command); add(panel, BorderLayout.CENTER); //顯示在窗口中心位置 } /** * 向中心面板添加一個按鈕。 * @param label the button label * @param listener the button listener */ private void addButton(String label, ActionListener listener) { var button = new JButton(label); button.addActionListener(listener); panel.add(button); } /** * 此操作將按鈕操作的字符串插入到顯示文本的末尾。 */ private class InsertAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start) { display.setText(""); start = false; } display.setText(display.getText() + input); } } /** * 該操作執行按鈕操作的字符串表示的命令。 */ private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (start) { if (command.equals("-")) { display.setText(command); start = false; } else lastCommand = command; } else { calculate(Double.parseDouble(display.getText())); lastCommand = command; start = true; } } } /** * 具體的計算 * @param x與先前結果累積的值。 */ public void calculate(double x) { if (lastCommand.equals("+")) result += x; else if (lastCommand.equals("-")) result -= x; else if (lastCommand.equals("*")) result *= x; else if (lastCommand.equals("/")) result /= x; else if (lastCommand.equals("=")) result = x; //將結果轉化成字符串顯示 display.setText("" + result); } }
運行結果:

測試程序2
l 在elipse IDE中調試運行教材486頁程序12-2,結合運行結果理解程序;
l 掌握文本組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
程序代碼:
package text; import java.awt.*; import javax.swing.*; /** * @version 1.42 2018-04-10 * @author Cay Horstmann */ public class TextComponentTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new TextComponentFrame(); frame.setTitle("TextComponentTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package text; import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingConstants; /** * 帶有輸入文本框組件的框架。 */ public class TextComponentFrame extends JFrame { public static final int TEXTAREA_ROWS = 8; public static final int TEXTAREA_COLUMNS = 20; public TextComponentFrame() { var textField = new JTextField(); var passwordField = new JPasswordField(); var northPanel = new JPanel(); northPanel.setLayout(new GridLayout(2, 2)); //SwingConstants通常用于在屏幕上定位或定向組件的常量的集合 northPanel.add(new JLabel("User name: ", SwingConstants.RIGHT)); northPanel.add(textField); northPanel.add(new JLabel("Password: ", SwingConstants.RIGHT)); northPanel.add(passwordField); add(northPanel, BorderLayout.NORTH); //構造具有指定行數和列數的新的空 TextArea。 var textArea = new JTextArea(TEXTAREA_ROWS, TEXTAREA_COLUMNS); //創建一個顯示指定組件內容的 JScrollPane對象,只要組件的內容超過視圖大小就會顯示水平和垂直滾動條。 var scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); // add button to append text into the text area var southPanel = new JPanel(); var insertButton = new JButton("Insert"); southPanel.add(insertButton); //將給定文本追加到文檔結尾。 insertButton.addActionListener(event -> textArea.append("User name: " + textField.getText() + " Password: " + new String(passwordField.getPassword()) + "\n")); add(southPanel, BorderLayout.SOUTH); pack(); } }
運行結果:

測試程序3
l 在elipse IDE中調試運行教材489頁程序12-3,結合運行結果理解程序;
l 掌握復選框組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
程序代碼:
package checkBox; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class CheckBoxTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new CheckBoxFrame(); frame.setTitle("CheckBoxTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package checkBox; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A frame with a sample text label and check boxes for selecting font * attributes. */ public class CheckBoxFrame extends JFrame { private JLabel label; private JCheckBox bold; private JCheckBox italic; private static final int FONTSIZE = 24; public CheckBoxFrame() //構造器 { // 添加示例文本標簽 label = new JLabel("The quick brown fox jumps over the lazy dog."); label.setFont(new Font("Serif", Font.BOLD, FONTSIZE)); add(label, BorderLayout.CENTER); // 字體屬性 // 復選框狀態的標簽 ActionListener listener = event -> { //設置字體為常規、加粗或斜體等 int mode = 0; if (bold.isSelected()) mode += Font.BOLD; if (italic.isSelected()) mode += Font.ITALIC; label.setFont(new Font("Serif", mode, FONTSIZE)); }; // 添加復選框 var buttonPanel = new JPanel(); bold = new JCheckBox("Bold"); bold.addActionListener(listener); bold.setSelected(true); buttonPanel.add(bold); italic = new JCheckBox("Italic"); italic.addActionListener(listener); buttonPanel.add(italic); add(buttonPanel, BorderLayout.SOUTH); pack(); } }
運行結果:


測試程序4
l 在elipse IDE中調試運行教材491頁程序12-4,運行結果理解程序;
l 掌握單選按鈕組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
程序代碼:
package radioButton; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class RadioButtonTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new RadioButtonFrame(); frame.setTitle("RadioButtonTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package radioButton; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 帶有示例文本標簽和用于選擇字體大小的單選按鈕的框架。 */ public class RadioButtonFrame extends JFrame { private JPanel buttonPanel; private ButtonGroup group; private JLabel label; private static final int DEFAULT_SIZE = 36; public RadioButtonFrame() { // add the sample text label label = new JLabel("The quick brown fox jumps over the lazy dog."); label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE)); add(label, BorderLayout.CENTER); // add the radio buttons buttonPanel = new JPanel(); group = new ButtonGroup(); addRadioButton("Small", 8); addRadioButton("Medium", 12); addRadioButton("Large", 18); addRadioButton("Extra large", 36); add(buttonPanel, BorderLayout.SOUTH); pack(); } /** * 添加一個單選按鈕,用于設置示例文本的字體大小。 * @param 大小的規格要出現在按鈕上的字符串 * @param 按鈕設置的字體大小 */ public void addRadioButton(String name, int size) { boolean selected = size == DEFAULT_SIZE; var button = new JRadioButton(name, selected); group.add(button); buttonPanel.add(button); // 此監聽器設置標簽字體大小 ActionListener listener = event -> label.setFont(new Font("Serif", Font.PLAIN, size)); button.addActionListener(listener); } }
運行結果:


測試程序5
l 在elipse IDE中調試運行教材494頁程序12-5,結合運行結果理解程序;
l 掌握邊框的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
程序代碼:
package border; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class BorderTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new BorderFrame(); frame.setTitle("BorderTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package border; import java.awt.*; import javax.swing.*; import javax.swing.border.*; /** * A frame with radio buttons to pick a border style. */ public class BorderFrame extends JFrame { private JPanel demoPanel; private JPanel buttonPanel; private ButtonGroup group; public BorderFrame() { demoPanel = new JPanel(); buttonPanel = new JPanel(); group = new ButtonGroup(); //設置不同的邊框類型按鈕,共六種(提供標準 Border 對象的工廠類) addRadioButton("Lowered bevel", BorderFactory.createLoweredBevelBorder()); addRadioButton("Raised bevel", BorderFactory.createRaisedBevelBorder()); addRadioButton("Etched", BorderFactory.createEtchedBorder()); addRadioButton("Line", BorderFactory.createLineBorder(Color.BLUE)); addRadioButton("Matte", BorderFactory.createMatteBorder(10, 10, 10, 10, Color.BLUE)); addRadioButton("Empty", BorderFactory.createEmptyBorder()); Border etched = BorderFactory.createEtchedBorder(); Border titled = BorderFactory.createTitledBorder(etched, "Border types"); buttonPanel.setBorder(titled); setLayout(new GridLayout(2, 1)); add(buttonPanel); add(demoPanel); pack(); } public void addRadioButton(String buttonName, Border b) { var button = new JRadioButton(buttonName); button.addActionListener(event -> demoPanel.setBorder(b)); group.add(button); buttonPanel.add(button); } }
運行結果:


測試程序6
l 在elipse IDE中調試運行教材498頁程序12-6,結合運行結果理解程序;
l 掌握組合框組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
程序代碼:
package comboBox; import java.awt.*; import javax.swing.*; /** * @version 1.36 2018-04-10 * @author Cay Horstmann */ public class ComboBoxTest { public static void main(String[] args) { //lambda表達式 EventQueue.invokeLater(() -> { //構造frame框架對象 var frame = new ComboBoxFrame(); //設置標題 frame.setTitle("ComboBoxTest"); //設置用戶在此窗體上發起 "close" 時默認執行的操作。 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置框架是否可見 frame.setVisible(true); }); } }
package comboBox; import java.awt.BorderLayout; import java.awt.Font; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * 具有示例文本標簽和用于選擇字體外觀的組合框的框架。 * 用戶可以從下拉列表中選擇值,下拉列表在用戶請求時顯示。 */ //ComboBoxFrame繼承于JFrame類 public class ComboBoxFrame extends JFrame { //設置ComboBoxFrame的私有屬性 private JComboBox<String> faceCombo; private JLabel label; private static final int DEFAULT_SIZE = 24; public ComboBoxFrame() { // 添加示例文本標簽 label = new JLabel("The quick brown fox jumps over the lazy dog."); //設置字體 label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE)); //添加到邊框布局管理器的中間 add(label, BorderLayout.CENTER); //創建一個組合框對象并添加項目名稱 faceCombo = new JComboBox<>(); //把一個選項添加到選項列表中,共五種選項 faceCombo.addItem("Serif"); faceCombo.addItem("SansSerif"); faceCombo.addItem("Monospaced"); faceCombo.addItem("Dialog"); faceCombo.addItem("DialogInput"); // 組合框監聽器將標簽字體更改為所選的名稱(添加監聽器,使用lambda表達式) faceCombo.addActionListener(event -> //設置標簽的字體 label.setFont( //getItemAt用于返回指定索引處的列表項;getSelectedIndex用于返回當前選擇的選項 new Font(faceCombo.getItemAt(faceCombo.getSelectedIndex()), Font.PLAIN, DEFAULT_SIZE))); // 將組合框添加到框架南部邊界的面板 var comboPanel = new JPanel(); comboPanel.add(faceCombo); add(comboPanel, BorderLayout.SOUTH); pack(); } }
運行結果:





實驗2:結對編程練習
利用所掌握的GUI技術,設計一個用戶信息采集程序,要求如下:
(1) 用戶信息輸入界面如下圖所示:

(2) 用戶點擊提交按鈕時,用戶輸入信息顯示在錄入信息顯示區,格式如下:

(3) 用戶點擊重置按鈕后,清空用戶已輸入信息;
(4) 點擊窗口關閉,程序退出。
程序代碼:
package message; import java.awt.EventQueue; import javax.swing.JFrame; public class Main { public static void main(String[] args) { EventQueue.invokeLater(() -> { demo page = new demo(); }); } }
package message; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.Window; public class center { public static void center(Window win){ Toolkit tkit = Toolkit.getDefaultToolkit(); Dimension sSize = tkit.getScreenSize(); Dimension wSize = win.getSize(); if(wSize.height > sSize.height){ wSize.height = sSize.height; } if(wSize.width > sSize.width) { wSize.width = sSize.width; } win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2); } }
package message; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class demo extends JFrame { public demo() { JPanel panel1 = new JPanel(); panel1.setPreferredSize(new Dimension(700, 45)); panel1.setLayout(new GridLayout(1, 4)); JLabel label1 = new JLabel("姓名:"); JTextField j1 = new JTextField(""); JLabel label2 = new JLabel("成年:"); JComboBox<Object> j2 = new JComboBox<>(); j2.addItem("是"); j2.addItem("否"); panel1.add(label1); panel1.add(j1); panel1.add(label2); panel1.add(j2); JPanel panel2 = new JPanel(); panel2.setPreferredSize(new Dimension(700, 50)); panel2.setLayout(new GridLayout(1, 4)); JLabel label3 = new JLabel("地址:"); JTextArea j3 = new JTextArea(); JLabel label4 = new JLabel("愛好:"); JPanel p = new JPanel(); p.setLayout(new GridLayout(3, 1)); p.setBorder(BorderFactory.createLineBorder(null)); JCheckBox c1 = new JCheckBox("閱讀"); JCheckBox c2 = new JCheckBox("唱歌"); JCheckBox c3 = new JCheckBox("跳舞"); p.add(c1); p.add(c2); p.add(c3); panel2.add(label3); panel2.add(j3); panel2.add(label4); panel2.add(p); JPanel panel3 = new JPanel(); panel3.setPreferredSize(new Dimension(700, 150)); FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT, 20, 40); panel3.setLayout(flowLayout1); JLabel label5 = new JLabel("性別:"); JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(2,1)); p1.setBorder(BorderFactory.createLineBorder(null)); ButtonGroup bu = new ButtonGroup(); JRadioButton jr1 = new JRadioButton("男"); JRadioButton jr2 = new JRadioButton("女"); bu.add(jr1); bu.add(jr2); p1.add(jr1); p1.add(jr2); panel3.add(label5); panel3.add(p1); add(panel1); add(panel2); add(panel3); JPanel panel4 = new JPanel(); panel4.setPreferredSize(new Dimension(700, 150)); JButton b1 = new JButton("提交"); panel4.add(b1); JButton b2 = new JButton("重置"); panel4.add(b2); add(panel4); FlowLayout flowLayout = new FlowLayout(); this.setLayout(flowLayout); this.setTitle("UserMessage"); this.setBounds(200, 200, 800, 400); this.setVisible(true); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO 自動生成的方法存根 String shifou = j2.getSelectedItem().toString(); System.out.println("姓名:" + j1.getText()); System.out.println("成年:" + shifou); String hobbystring = "愛好:"; if (c1.isSelected()) { hobbystring += "閱讀"; } if (c2.isSelected()) { hobbystring += "唱歌"; } if (c3.isSelected()) { hobbystring += "跳舞"; } System.out.println("地址:" + j3.getText()); if (jr1.isSelected()) { System.out.println("性別:男"); } if (jr2.isSelected()) { System.out.println("性別:女"); } System.out.println(hobbystring); } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO 自動生成的方法存根 j1.setText(null); j3.setText(null); j2.setSelectedIndex(0); c1.setSelected(false); c2.setSelected(false); c3.setSelected(false); bu.clearSelection(); } }); } public static void main(String args[]) { new demo(); } }
運行結果:


(5)討論、細化和編程時的結對照片

實驗總結:
這周的測試實驗比較簡單也比較有趣,在測試后,結合書上的知識,學習了程序GUI設計中應用的相關組件以及各類Java Swing組件用途及常用API;在學習過程中我發現對于之前學過的知識掌握還不夠好,在學習新知識的同時忘記了復習之前的知識,以后也要多注意知識的回顧,并且還是要繼續提高自身的編程能力。

浙公網安備 33010602011771號