201771010124 王海珍 《面向物件設計 java》第十八週總結
阿新 • • 發佈:2018-12-30
1、實驗目的與要求
(1) 綜合掌握java基本程式結構;
(2) 綜合掌握java面向物件程式設計特點;
(3) 綜合掌握java GUI 程式設計結構;
(4) 綜合掌握java多執行緒程式設計模型;
(5) 綜合程式設計練習。
2、實驗內容和步驟
任務1:填寫課程課後調查問卷,網址:https://www.wjx.cn/jq/33108969.aspx。
任務2:綜合程式設計練習
練習1:設計一個使用者資訊採集程式,要求如下:
1 package project1;
2
3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.border.*; 7 8 public class test extends JFrame { 9 public test() { 10 JPanel panel1 = new JPanel(); 11 panel1.setPreferredSize(new Dimension(700, 45)); 12 panel1.setLayout(new GridLayout(1, 4)); 13 JLabel label1 = new JLabel("Name:"); 14 JTextField j1 = new JTextField(""); 15 JLabel label2 = new JLabel("Qualification:"); 16 JComboBox<Object> j2 = new JComboBox<>(); 17 j2.addItem("Graduate"); 18 j2.addItem("Not Graduate"); 19 panel1.add(label1); 20 panel1.add(j1); 21 panel1.add(label2); 22 panel1.add(j2); 23 24 JPanel panel2 = new JPanel(); 25 panel2.setPreferredSize(new Dimension(700, 50)); 26 panel2.setLayout(new GridLayout(1, 4)); 27 JLabel label3 = new JLabel("Address:"); 28 JTextArea j3 = new JTextArea(); 29 JLabel label4 = new JLabel("Hobby:"); 30 JPanel p = new JPanel(); 31 p.setLayout(new GridLayout(3, 1)); 32 p.setBorder(BorderFactory.createLineBorder(null)); 33 JCheckBox c1 = new JCheckBox("Reading"); 34 JCheckBox c2 = new JCheckBox("Singing"); 35 JCheckBox c3 = new JCheckBox("Dancing"); 36 p.add(c1); 37 p.add(c2); 38 p.add(c3); 39 panel2.add(label3); 40 panel2.add(j3); 41 panel2.add(label4); 42 panel2.add(p); 43 44 JPanel panel3 = new JPanel(); 45 panel3.setPreferredSize(new Dimension(700, 150)); 46 FlowLayout flowLayout1 = new FlowLayout(FlowLayout.CENTER, 50, 10); 47 panel3.setLayout(flowLayout1); 48 JLabel label5 = new JLabel("Sex:"); 49 JPanel p1 = new JPanel(); 50 p1.setLayout(new GridLayout(2, 1)); 51 p1.setBorder(BorderFactory.createLineBorder(null)); 52 ButtonGroup bu = new ButtonGroup(); 53 JRadioButton jr1 = new JRadioButton("Male"); 54 JRadioButton jr2 = new JRadioButton("Female"); 55 bu.add(jr1); 56 bu.add(jr2); 57 p1.add(jr1); 58 p1.add(jr2); 59 panel3.add(label5); 60 panel3.add(p1); 61 add(panel1); 62 add(panel2); 63 add(panel3); 64 65 JPanel panel4 = new JPanel(); 66 panel4.setPreferredSize(new Dimension(700, 150)); 67 JButton b1 = new JButton("Validate"); 68 panel4.add(b1); 69 JButton b2 = new JButton("Reset"); 70 panel4.add(b2); 71 add(panel4); 72 73 FlowLayout flowLayout = new FlowLayout(); 74 this.setLayout(flowLayout); 75 this.setTitle("Students Detail"); 76 this.setBounds(200, 200, 800, 400); 77 this.setVisible(true); 78 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 79 80 b1.addActionListener(new ActionListener() { 81 82 @Override 83 public void actionPerformed(ActionEvent e) { 84 String xueli = j2.getSelectedItem().toString(); 85 System.out.println("Name:" + j1.getText()); 86 System.out.println("Qualification:" + xueli); 87 String hobbystring = "Hobby:"; 88 if (c1.isSelected()) { 89 hobbystring += "Reading"; 90 } 91 if (c2.isSelected()) { 92 hobbystring += "Singing"; 93 } 94 if (c3.isSelected()) { 95 hobbystring += "Dancing"; 96 } 97 System.out.println("Address:" + j3.getText()); 98 if (jr1.isSelected()) { 99 System.out.println("Sex:Male");