JAVA 作业:图形界面

时间:2021-12-11 15:42:39

自己动手写的一个小JAVA 程序:

一个学生管理小系统,虽然很挫,但是这我学JAVA的第一步。学了2天JAVA没有白费!

 import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList; import javax.swing.*; class Demo {
public static void main(String[] args) {
new MyFrame();
}
} class Student{ private String _stuId;
private String _name;
private char[] _secret;
private String _sex;
private String _jiguan;
private String _xibie;
private String _course;
public String get_stuId() {
return _stuId;
}
public void set_stuId(String _stuId) {
this._stuId = _stuId;
}
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
public String get_sex() {
return _sex;
}
public void set_sex(String _sex) {
this._sex = _sex;
}
public String get_jiguan() {
return _jiguan;
}
public void set_jiguan(String _jiguan) {
this._jiguan = _jiguan;
}
public String get_xibie() {
return _xibie;
}
public void set_xibie(String _xibie) {
this._xibie = _xibie;
}
public String get_course() {
return _course;
}
public void set_course(String _course) {
this._course = _course;
}
public char[] get_secret() {
return _secret;
}
public void set_secret(char[] dd_secret) {
this._secret = dd_secret;
}
} class MyFrame extends JFrame { ArrayList<Student> students=new ArrayList<Student>();
public MyFrame() {
Box baseBox,box1,box2;
setTitle("学生信息录用");
setSize(350, 650);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); box1 = Box.createVerticalBox();
box1.add(Box.createVerticalStrut(30));
box1.add(new JLabel("学号"));
box1.add(Box.createVerticalStrut(30));
box1.add(new JLabel("姓名"));
box1.add(Box.createVerticalStrut(30));
box1.add(new JLabel("密码")); box2 = Box.createVerticalBox();
box2.add(Box.createVerticalStrut(30));
final JTextField TF_stuid = new JTextField(10);
box2.add(TF_stuid);
box2.add(Box.createVerticalStrut(30));
final JTextField TF_name = new JTextField(10);
box2.add(TF_name);
box2.add(Box.createVerticalStrut(30));
final JPasswordField TF_secret = new JPasswordField(10);
box2.add(TF_secret); Box box3 = Box.createHorizontalBox();
ButtonGroup g = new ButtonGroup();
final String JRb1 = "男";
final String JRb2 = "女";
final JRadioButton b1 = new JRadioButton(JRb1);
final JRadioButton b2 = new JRadioButton(JRb2); //final String sex ;
g.add(b1);
g.add(b2);
box3.add(new JLabel("性别"));
box3.add(Box.createHorizontalStrut(20));
box3.add(b1);
box3.add(Box.createHorizontalStrut(20));
box3.add(b2); baseBox = Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(30));
baseBox.add(box2);
//baseBox.add(box3);
//add(baseBox);
Box box5,box6,box7;
box5 = Box.createVerticalBox();
box5.add(Box.createVerticalStrut(30));
box5.add(new JLabel("籍贯"));
box5.add(Box.createVerticalStrut(30));
box5.add(new JLabel("系别")); final String[] xibie = {"计算机与科学", "软件工程", "网络工程", "嵌入式系统开发", "信息工程"};
final JComboBox jb = new JComboBox(xibie);
box6 = Box.createVerticalBox();
box6.add(Box.createVerticalStrut(30));
final JTextField TF_jiguan = new JTextField(10);
box6.add(TF_jiguan);
box6.add(Box.createVerticalStrut(30));
box6.add(jb); box7 = Box.createHorizontalBox();
box7.add(box5);
box7.add(Box.createHorizontalStrut(30));
box7.add(box6); final JCheckBox[] ch = new JCheckBox[6];
ch[0]=new JCheckBox("C++程序设计");
ch[1]=new JCheckBox("计算机组成原理");
ch[2]=new JCheckBox("汇编原理");
ch[3]=new JCheckBox("数据结构");
ch[4]=new JCheckBox("数据库设计原理");
ch[5]=new JCheckBox("软件工程"); Box box8,box9,box10;
box8 = Box.createVerticalBox();
box8.add(new JLabel("课程")); box9 = Box.createVerticalBox();
box9.add(ch[0]);
box9.add(ch[1]);
box9.add(ch[2]);
box9.add(ch[3]);
box9.add(ch[4]);
box9.add(ch[5]); box10 = Box.createHorizontalBox();
box10.add(box8);
box10.add(Box.createHorizontalStrut(100));
box10.add(box9); JButton dd1 = new JButton("确认", new ImageIcon("3.JPG"));
JButton dd2 = new JButton("取消"); dd1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String dd_stuid = TF_stuid.getText();
if(dd_stuid.equals("")) System.out.println(123);
String dd_name = TF_name.getText();
String dd_jiguan = TF_jiguan.getText();
char[] dd_secret = TF_secret.getPassword(); String dd_sex = null;
if(b1.isSelected())
dd_sex = "男";
if(b2.isSelected())
dd_sex = "女";
String course = "";
String[] sz_course = {"C++程序设计 ", "计算机组成原理 ", "汇编原理 ", "数据结构 ", "数据库设计原理 ","软件工程 "}; for(int i=0;i<6;++i)
if(ch[i].isSelected())
course+=sz_course[i]; int int_jb = jb.getSelectedIndex();
String dd_xibie = xibie[int_jb];
System.out.println(xibie[int_jb]);
Student st = new Student();
st.set_stuId(dd_stuid);
st.set_name(dd_name);
st.set_secret(dd_secret);
st.set_jiguan(dd_jiguan);
st.set_sex(dd_sex);
st.set_xibie(dd_xibie);
st.set_course(course); students.add(st);
if(TF_stuid.getText().equals("")||TF_name.getText().equals("") ||TF_jiguan.getText().equals("") )
JOptionPane.showMessageDialog(null, "录入失败");
else JOptionPane.showMessageDialog(null, "录入成功"); TF_stuid.setText("");
TF_name.setText("");
TF_jiguan.setText("");
TF_secret.setText("");
}
}); dd2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
}); Box box11 = Box.createHorizontalBox();
box11.add(dd1);
box11.add(Box.createHorizontalStrut(30));
box11.add(dd2); Box box4 = Box.createVerticalBox();
box4.add(baseBox);
box4.add(Box.createVerticalStrut(10));
box4.add(box3);
box4.add(box7);
box4.add(Box.createVerticalStrut(20));
box4.add(box10);
box4.add(Box.createVerticalStrut(20));
box4.add(box11);
add(box4); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width - getWidth()) / 2;
int y = (screenSize.height - getHeight()) / 2;
setLocation(x, y); setLayout(new FlowLayout());
validate(); }
}