姓名:周强
学号:201771010141
第十八周作业
1、实验目的与要求
(1) 综合掌握java基本程序结构;
(2) 综合掌握java面向对象程序设计特点;
(3) 综合掌握java GUI 程序设计结构;
(4) 综合掌握java多线程编程模型;
(5) 综合编程练习。
2、实验内容和步骤
任务1:填写课程课后调查问卷,网址:https://www.wjx.cn/jq/33108969.aspx。
任务2:综合编程练习
练习1:设计一个用户信息采集程序,要求如下:
(1) 用户信息输入界面如下图所示:
(1)用户点击提交按钮时,用户输入信息显示控制台界面;
(2)用户点击重置按钮后,清空用户已输入信息;
(3)点击窗口关闭,程序退出。
Main:
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Mian {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
demo page = new demo();
});
}
}
WinCenter:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
public class WinCenter {
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);
}
}
Demo:
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("Name:");
JTextField j1 = new JTextField("");
JLabel label2 = new JLabel("Qualification:");
JComboBox<Object> j2 = new JComboBox<>();
j2.addItem("Graduate");
j2.addItem("Not Graduate");
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("Address:");
JTextArea j3 = new JTextArea();
JLabel label4 = new JLabel("Hobby:");
JPanel p = new JPanel();
p.setLayout(new GridLayout(3, 1));
p.setBorder(BorderFactory.createLineBorder(null));
JCheckBox c1 = new JCheckBox("Reading");
JCheckBox c2 = new JCheckBox("Singing");
JCheckBox c3 = new JCheckBox("Dancing");
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("Sex:");
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2,1));
p1.setBorder(BorderFactory.createLineBorder(null));
ButtonGroup bu = new ButtonGroup();
JRadioButton jr1 = new JRadioButton("Male");
JRadioButton jr2 = new JRadioButton("Female");
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("Validate");
panel4.add(b1);
JButton b2 = new JButton("Reset");
panel4.add(b2);
add(panel4);
FlowLayout flowLayout = new FlowLayout();
this.setLayout(flowLayout);
this.setTitle("Students Detail");
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 xueli = j2.getSelectedItem().toString();
System.out.println("Name:" + j1.getText());
System.out.println("Qualification:" + xueli);
String hobbystring = "Hobby:";
if (c1.isSelected()) {
hobbystring += "Reading";
}
if (c2.isSelected()) {
hobbystring += "Singing";
}
if (c3.isSelected()) {
hobbystring += "Dancing";
}
System.out.println("Address:" + j3.getText());
if (jr1.isSelected()) {
System.out.println("Sex:Male");
}
if (jr2.isSelected()) {
System.out.println("Sex:Female");
}
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