JAVA 作业:图形界面

来源:互联网 发布:手机怎么看淘宝直播 编辑:程序博客网 时间:2024/06/14 06:04

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

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

 

  1 import java.awt.*;  2 import java.awt.event.*;  3 import java.util.ArrayList;  4   5 import javax.swing.*;  6   7 class Demo {  8     public static void main(String[] args) {  9         new MyFrame(); 10     }                 11 } 12  13 class Student{ 14  15     private String _stuId; 16     private String _name; 17     private char[] _secret; 18     private String _sex; 19     private String _jiguan; 20     private String _xibie; 21     private String _course; 22     public String get_stuId() { 23         return _stuId; 24     } 25     public void set_stuId(String _stuId) { 26         this._stuId = _stuId; 27     } 28     public String get_name() { 29         return _name; 30     } 31     public void set_name(String _name) { 32         this._name = _name; 33     } 34     public String get_sex() { 35         return _sex; 36     } 37     public void set_sex(String _sex) { 38         this._sex = _sex; 39     } 40     public String get_jiguan() { 41         return _jiguan; 42     } 43     public void set_jiguan(String _jiguan) { 44         this._jiguan = _jiguan; 45     } 46     public String get_xibie() { 47         return _xibie; 48     } 49     public void set_xibie(String _xibie) { 50         this._xibie = _xibie; 51     } 52     public String get_course() { 53         return _course; 54     } 55     public void set_course(String _course) { 56         this._course = _course; 57     } 58     public char[] get_secret() { 59         return _secret; 60     } 61     public void set_secret(char[] dd_secret) { 62         this._secret = dd_secret; 63     } 64 } 65  66  67  68 class MyFrame extends JFrame { 69      70     ArrayList<Student> students=new ArrayList<Student>(); 71     public MyFrame() { 72         Box baseBox,box1,box2; 73         setTitle("学生信息录用"); 74         setSize(350, 650); 75         setVisible(true); 76         setResizable(false); 77         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 78          79         box1 = Box.createVerticalBox(); 80         box1.add(Box.createVerticalStrut(30)); 81         box1.add(new JLabel("学号")); 82         box1.add(Box.createVerticalStrut(30)); 83         box1.add(new JLabel("姓名")); 84         box1.add(Box.createVerticalStrut(30)); 85         box1.add(new JLabel("密码")); 86  87         box2 = Box.createVerticalBox(); 88         box2.add(Box.createVerticalStrut(30)); 89         final JTextField TF_stuid = new JTextField(10); 90         box2.add(TF_stuid); 91         box2.add(Box.createVerticalStrut(30)); 92         final JTextField TF_name = new JTextField(10); 93         box2.add(TF_name); 94         box2.add(Box.createVerticalStrut(30)); 95         final JPasswordField TF_secret = new JPasswordField(10); 96         box2.add(TF_secret); 97          98         Box box3 =  Box.createHorizontalBox(); 99         ButtonGroup g = new ButtonGroup();100         final String JRb1 = "男";101         final String JRb2 = "女";102         final JRadioButton b1 = new JRadioButton(JRb1);103         final JRadioButton b2 = new JRadioButton(JRb2);104 105         106         //final String sex ;107         g.add(b1);108         g.add(b2);109         box3.add(new JLabel("性别"));110         box3.add(Box.createHorizontalStrut(20));111         box3.add(b1);112         box3.add(Box.createHorizontalStrut(20));113         box3.add(b2);114         115         baseBox = Box.createHorizontalBox();116         baseBox.add(box1);117         baseBox.add(Box.createHorizontalStrut(30));118         baseBox.add(box2);119         //baseBox.add(box3);120         //add(baseBox);121         Box box5,box6,box7;122         box5 = Box.createVerticalBox();123         box5.add(Box.createVerticalStrut(30));124         box5.add(new JLabel("籍贯"));125         box5.add(Box.createVerticalStrut(30));126         box5.add(new JLabel("系别"));127 128         final String[] xibie = {"计算机与科学", "软件工程", "网络工程", "嵌入式系统开发", "信息工程"};129         final JComboBox jb = new JComboBox(xibie);130         box6 = Box.createVerticalBox();131         box6.add(Box.createVerticalStrut(30));132         final JTextField TF_jiguan = new JTextField(10);133         box6.add(TF_jiguan);134         box6.add(Box.createVerticalStrut(30));135         box6.add(jb);136 137         box7 = Box.createHorizontalBox();138         box7.add(box5);139         box7.add(Box.createHorizontalStrut(30));140         box7.add(box6);141         142         final JCheckBox[] ch = new JCheckBox[6];143         ch[0]=new JCheckBox("C++程序设计");144         ch[1]=new JCheckBox("计算机组成原理");145         ch[2]=new JCheckBox("汇编原理");146         ch[3]=new JCheckBox("数据结构");147         ch[4]=new JCheckBox("数据库设计原理");148         ch[5]=new JCheckBox("软件工程");149         150         151         Box box8,box9,box10;152         box8 = Box.createVerticalBox();153         box8.add(new JLabel("课程"));154         155         box9 = Box.createVerticalBox();156         box9.add(ch[0]);157         box9.add(ch[1]);158         box9.add(ch[2]);159         box9.add(ch[3]);160         box9.add(ch[4]);161         box9.add(ch[5]);162         163         box10 = Box.createHorizontalBox();164         box10.add(box8);165         box10.add(Box.createHorizontalStrut(100));166         box10.add(box9);167         168         JButton dd1 = new JButton("确认", new ImageIcon("3.JPG"));169         JButton dd2 = new JButton("取消");170         171         dd1.addActionListener(new ActionListener(){172             173             public void actionPerformed(ActionEvent e){174                 175                 String dd_stuid = TF_stuid.getText(); 176                 if(dd_stuid.equals("")) System.out.println(123);177                 String dd_name = TF_name.getText();178                 String dd_jiguan = TF_jiguan.getText();179                 char[] dd_secret = TF_secret.getPassword();180                 181                 String dd_sex = null;182                 if(b1.isSelected())183                     dd_sex = "男";184                 if(b2.isSelected())185                     dd_sex = "女";186                 String course = "";187                 String[] sz_course = {"C++程序设计 ", "计算机组成原理 ", "汇编原理 ", "数据结构 ", "数据库设计原理 ","软件工程 "};188         189                 for(int i=0;i<6;++i)190                     if(ch[i].isSelected())191                         course+=sz_course[i];192                 193                 int int_jb = jb.getSelectedIndex();194                 String dd_xibie = xibie[int_jb];195                 System.out.println(xibie[int_jb]);196                 Student st = new Student();197                 st.set_stuId(dd_stuid);198                 st.set_name(dd_name);199                 st.set_secret(dd_secret);200                 st.set_jiguan(dd_jiguan);201                 st.set_sex(dd_sex);202                 st.set_xibie(dd_xibie);203                 st.set_course(course);204         205                 students.add(st);206                 if(TF_stuid.getText().equals("")||TF_name.getText().equals("") ||TF_jiguan.getText().equals("") )207                     JOptionPane.showMessageDialog(null, "录入失败");208                 else JOptionPane.showMessageDialog(null, "录入成功");209                 210                 TF_stuid.setText("");211                 TF_name.setText("");212                 TF_jiguan.setText("");213                 TF_secret.setText("");214             }215         });216         217         dd2.addActionListener(new ActionListener(){218             public void actionPerformed(ActionEvent e){219                 dispose();220             }221         });222         223         224         Box box11 = Box.createHorizontalBox();225         box11.add(dd1);226         box11.add(Box.createHorizontalStrut(30));227         box11.add(dd2);228         229         Box box4 = Box.createVerticalBox();230         box4.add(baseBox);231         box4.add(Box.createVerticalStrut(10));232         box4.add(box3);233         box4.add(box7);234         box4.add(Box.createVerticalStrut(20));235         box4.add(box10);236         box4.add(Box.createVerticalStrut(20));237         box4.add(box11);238         add(box4);239             240         241 242         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();243         int x = (screenSize.width - getWidth()) / 2;244         int y = (screenSize.height - getHeight()) / 2;245         setLocation(x, y);246 247         setLayout(new FlowLayout());248         validate();249 250     }251 }

 

0 0