import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class GTest3 extends JFrame{
ArrayList<Student> students = new ArrayList<Student>();
private JLabel label1,label2,label3,label4,label5,label6,label7;
private JTextField field1,field2,field3,field4,field5,field6;
private JRadioButton manButton,womenButton;
private ButtonGroup radioGroup;
private JButton button1,button2,button3,button4;
public GTest3() {
super();
Container container = getContentPane();
container.setLayout(new FlowLayout());
label1 = new JLabel("学号");
label2 = new JLabel("姓名");
label3 = new JLabel("性别");
label4 = new JLabel("数学");
label5 = new JLabel("英语");
label6 = new JLabel("政治");
label7 = new JLabel("总成绩");
field1 = new JTextField(10);
field2 = new JTextField(10);
field3 = new JTextField(10);
field4 = new JTextField(10);
field5 = new JTextField(10);
field6 = new JTextField(10);
manButton = new JRadioButton("男",true);
womenButton = new JRadioButton("女",false);
radioGroup = new ButtonGroup();
radioGroup.add(manButton);
radioGroup.add(womenButton);
button1 = new JButton("计算总成绩");
button2 = new JButton("添加");
button3 = new JButton("删除");
button4 = new JButton("关闭");
Box horiziontallBox1 = Box.createHorizontalBox();
Box horiziontallBox2 = Box.createHorizontalBox();
Box horiziontallBox3 = Box.createHorizontalBox();
Box horiziontallBox4 = Box.createHorizontalBox();
Box horiziontallBox5 = Box.createHorizontalBox();
Box verticalBox = Box.createVerticalBox();
horiziontallBox1.add(label1);
horiziontallBox1.add(field1);
horiziontallBox1.add(label2);
horiziontallBox1.add(field2);
horiziontallBox2.add(label3);
horiziontallBox2.add(manButton);
horiziontallBox2.add(womenButton);
horiziontallBox3.add(label4);
horiziontallBox3.add(field3);
horiziontallBox3.add(label5);
horiziontallBox3.add(field4);
horiziontallBox4.add(label6);
horiziontallBox4.add(field5);
horiziontallBox4.add(label7);
horiziontallBox4.add(field6);
horiziontallBox5.add(button1);
horiziontallBox5.add(Box.createHorizontalStrut(5));
horiziontallBox5.add(button2);
horiziontallBox5.add(Box.createHorizontalStrut(5));
horiziontallBox5.add(button3);
horiziontallBox5.add(Box.createHorizontalStrut(5));
horiziontallBox5.add(button4);
verticalBox.add(Box.createVerticalStrut(25));
verticalBox.add(horiziontallBox1);
verticalBox.add(Box.createVerticalStrut(25));
verticalBox.add(horiziontallBox2);
verticalBox.add(Box.createVerticalStrut(25));
verticalBox.add(horiziontallBox3);
verticalBox.add(Box.createVerticalStrut(25));
verticalBox.add(horiziontallBox4);
verticalBox.add(Box.createVerticalStrut(25));
verticalBox.add(horiziontallBox5);
container.add(verticalBox);
ButtonHandle BH = new ButtonHandle();
button1.addActionListener(BH);
button2.addActionListener(BH);
button3.addActionListener(BH);
button4.addActionListener(BH);
setSize(400,400);
setVisible(true);
}
public static void main(String args[]) {
GTest3 a = new GTest3();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class ButtonHandle extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "计算总成绩") {
int math = Integer.parseInt(field3.getText());
int english = Integer.parseInt(field4.getText());
int politics = Integer.parseInt(field5.getText());
String sum = Integer.toString(math+english+politics);
field6.setText(sum);
}
if(e.getActionCommand() == "添加") {
String stuno = field1.getText();
String stuname = field2.getText();
String stusex;
if(manButton.isSelected()) {
stusex ="男";
}
else {
stusex="女";
}
String stumath = field3.getText();
String stuenglish = field4.getText();
String stupolitics = field5.getText();
if(students.size()==0) {
Student s1 = new Student(stuno, stuname, stusex, stumath, stuenglish, stupolitics);
students.add(s1);
}
else {
for(Student s:students) {
if(s.getStuno().equals(stuno)) {
JOptionPane.showMessageDialog(null, "该学生学号已存在");
break;
}
else {
Student s1 = new Student(stuno, stuname, stusex, stumath, stuenglish, stupolitics);
students.add(s1);
break;
}
}
}
}
if(e.getActionCommand() == "删除") {
String stuno = field1.getText();
Student student = null;
for(Student s:students) {
if(s.getStuno().equals(stuno)) {
student = s;
}
}
students.remove(student);
}
if(e.getActionCommand() == "关闭") {
System.exit(0);
}
System.out.println(students);
}
}
}
public class Student {
private String stuno;
private String stuname;
private String stusex;
private String stumath;
private String stuenglish;
private String stupolitics;
public String getStuno() {
return stuno;
}
public void setStuno(String stuno) {
this.stuno = stuno;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
public String getStumath() {
return stumath;
}
public void setStumath(String stumath) {
this.stumath = stumath;
}
public String getStuenglish() {
return stuenglish;
}
public void setStuenglish(String stuenglish) {
this.stuenglish = stuenglish;
}
public String getStupolitics() {
return stupolitics;
}
public void setStupolitics(String stupolitics) {
this.stupolitics = stupolitics;
}
public Student(String stuno,String stuname,String stusex,String stumath,String stuenglish, String stupolitics) {
this.stuno = stuno;
this.stuname = stuname;
this.stusex = stusex;
this.stumath = stumath;
this.stuenglish = stuenglish;
this.stupolitics = stupolitics;
}
public String toString() {
return "学号:"+getStuno()+"姓名:"+getStuname()+"性别:"+getStusex()+"数学"+getStumath()+"英语"+getStuenglish()+"政治"+getStupolitics();
}
}
网友评论