这星期尝试用Java做了一个学生信息管理界面,实现了其中一些功能。解决问题的一个方法就是将它划分成很多小问题,再一步步解决这些小问题。在编写界面程序时,遇到一些问题,解决一部分然后运行程序,看着界面一点点充实起来,也有了信心。
这次接触到了AWT和Swing组件。首先需要解决的是将学生信息管理界面需要基本组件放在合适的位置。JFrame设置窗口的框架,需要设置它的大小setSize,可见setVisible,在屏幕上的位置setLocation,如果关闭窗口要正常退出用到方法setDefaultCloseOperation。其次所有的元素要放在容器中,需要类Container。依此设置的控件需要的类有JLabel、JButton、JComboBox、JCheckBox、JList、JRadioButton等。在函数initFrame() 完成初始化操作。
private void initFrame() { int i; Dimension dim; Font normalFont; normalFont = new Font("宋体", Font.PLAIN, 16); jfrmMain = new JFrame("学生信息管理系统"); con = jfrmMain.getContentPane(); con.setLayout(null); jfrmMain.setSize(8130/15, 6135/15); dim = Toolkit.getDefaultToolkit().getScreenSize(); jfrmMain.setLocation((dim.width - jfrmMain.getWidth())/2, (dim.height - jfrmMain.getHeight())/2); jlblTopic = new JLabel("学生信息管理"); jlblTopic.setFont(new Font("微软雅黑", Font.PLAIN, 35)); jlblTopic.setForeground(Color.blue); jlblTopic.setBounds(2430/15, 0, 3150/15, 690/15); con.add(jlblTopic); jlblStudentIdTopic = new JLabel("学 号"); jlblStudentIdTopic.setFont(normalFont); jlblStudentIdTopic.setBounds(3450/15, 1050/15, 960/15, 240/15); con.add(jlblStudentIdTopic); jlblStudentId = new JLabel(); jlblStudentId.setFont(normalFont); jlblStudentId.setBounds(4500/15, 1050/15, 3345/15, 240/15); con.add(jlblStudentId); jlblName = new JLabel("姓 名"); jlblName.setFont(normalFont); jlblName.setBounds(3450/15, 1575/15, 960/15, 240/15); con.add(jlblName); jtxtName = new JTextField(); jtxtName.setFont(normalFont); jtxtName.setBounds(4500/15, 1515/15, 3315/15, 360/15); con.add(jtxtName); jlblSex = new JLabel("性 别"); jlblSex.setFont(normalFont); jlblSex.setBounds(3450/15, 2145/15, 960/15, 240/15); con.add(jlblSex); jrdbSex[0] = new JRadioButton("男"); jrdbSex[1] = new JRadioButton("女"); jrdbSex[0].setFont(normalFont); jrdbSex[1].setFont(normalFont); jrdbSex[0].setBounds(4725/15, 2115/15, 690/15, 315/15); jrdbSex[1].setBounds(6900/15, 2115/15, 690/15, 315/15); con.add(jrdbSex[0]); con.add(jrdbSex[1]); bgSex = new ButtonGroup(); bgSex.add(jrdbSex[0]); bgSex.add(jrdbSex[1]); jlblId = new JLabel("身份证号"); jlblId.setFont(normalFont); jlblId.setBounds(3450/15, 2715/15, 960/15, 240/15); con.add(jlblId); jtxtId = new JTextField(); jtxtId.setFont(normalFont); jtxtId.setBounds(4500/15, 2655/15, 3315/15, 360/15); con.add(jtxtId);
jlblBirth = new JLabel("出生日期"); jlblBirth.setFont(normalFont); jlblBirth.setBounds(3450/15, 3285/15, 960/15, 240/15); con.add(jlblBirth);
jlblBirthYear = new JLabel("年"); jlblBirthYear.setFont(normalFont); jlblBirthYear.setBounds(5475/15, 3285/15, 240/15, 240/15); con.add(jlblBirthYear);
jlblBirthMonth = new JLabel("月"); jlblBirthMonth.setFont(normalFont); jlblBirthMonth.setBounds(6525/15, 3285/15, 240/15, 240/15); con.add(jlblBirthMonth);
jlblBirthDay = new JLabel("日"); jlblBirthDay.setFont(normalFont); jlblBirthDay.setBounds(7575/15, 3285/15, 240/15, 240/15); con.add(jlblBirthDay); jcmbBirthYear = new JComboBox(); jcmbBirthYear.setFont(normalFont); jcmbBirthYear.setBounds(4500/15, 3225/15, 915/15, 360/15); con.add(jcmbBirthYear); jcmbBirthMonth = new JComboBox(); jcmbBirthMonth.setFont(normalFont); jcmbBirthMonth.setBounds(5775/15, 3225/15, 690/15, 360/15); con.add(jcmbBirthMonth); jcmbBirthDay = new JComboBox(); jcmbBirthDay.setFont(normalFont); jcmbBirthDay.setBounds(6825/15, 3225/15, 690/15, 360/15); con.add(jcmbBirthDay);
jlblHobby = new JLabel("个人嗜好"); jlblHobby.setFont(normalFont); jlblHobby.setBounds(3450/15, 3855/15, 960/15, 240/15); con.add(jlblHobby); for(i = 0; i < jckbHobby.length; i++) { jckbHobby[i] = new JCheckBox(strHobby[i]); jckbHobby[i].setFont(normalFont); jckbHobby[i].setBounds(hobbyBounds[i][0], hobbyBounds[i][1], hobbyBounds[i][2], hobbyBounds[i][3]); con.add(jckbHobby[i]); } jbtnAllSelected = new JButton("全选"); jbtnAllSelected.setFont(new Font("宋体", Font.PLAIN, 11)); jbtnAllSelected.setBounds(6975/15, 3675/15, 840/15, 390/15); con.add(jbtnAllSelected); jbtnRevSelected = new JButton("反选"); jbtnRevSelected.setFont(new Font("宋体", Font.PLAIN, 11)); jbtnRevSelected.setBounds(6975/15, 4050/15, 840/15, 390/15); con.add(jbtnRevSelected); jbtnNonSelected = new JButton("清空"); jbtnNonSelected.setFont(new Font("宋体", Font.PLAIN, 11)); jbtnNonSelected.setBounds(6975/15, 4425/15, 840/15, 390/15); con.add(jbtnNonSelected);
jlblStudentList = new JLabel("学生列表"); jlblStudentList.setFont(normalFont); jlblStudentList.setBounds(1170/15, 825/15, 960/15, 240/15); con.add(jlblStudentList);
dlmStudentList = new DefaultListModel(); jlstStudentList = new JList(dlmStudentList); jlstStudentList.setFont(normalFont); jscpStudentList = new JScrollPane(jlstStudentList); jscpStudentList.setBounds(225/15, 1125/15, 2865/15, 3900/15); con.add(jscpStudentList); jlblStudentAccount = new JLabel("共0名学生"); jlblStudentAccount.setFont(normalFont); jlblStudentAccount.setBounds(225/15, 5025/15, 2880/15, 240/15); con.add(jlblStudentAccount);
jbtnAdd = new JButton("添加"); jbtnAdd.setFont(normalFont); jbtnAdd.setBounds(3525/15, 4950/15, 990/15, 465/15); con.add(jbtnAdd);
jbtnModify = new JButton("修改"); jbtnModify.setFont(normalFont); jbtnModify.setBounds(4500/15, 4950/15, 990/15, 465/15); con.add(jbtnModify);
jbtnRemove = new JButton("删除"); jbtnRemove.setFont(normalFont); jbtnRemove.setBounds(5475/15, 4950/15, 990/15, 465/15); con.add(jbtnRemove);
jbtnExit = new JButton("退出"); jbtnExit.setFont(normalFont); jbtnExit.setBounds(6825/15, 4950/15, 990/15, 465/15); con.add(jbtnExit); jfrmMain.setVisible(true); }
加入组件之后,对进行的操作进行处理,首先对“年”“月”“日”进行处理。这里的MAX_AGE指65,MIN_AGE指12。如下程序:
private void initBirthDay() { String birthYear; String birthMonth; birthYear = jcmbBirthYear.getSelectedItem().toString(); birthMonth = jcmbBirthMonth.getSelectedItem().toString(); if(birthYear != null && birthMonth != null) { Calendar today = Calendar.getInstance(); today.set(Integer.valueOf(birthYear), Integer.valueOf(birthMonth) - 1, 1); int maxDay = today.getActualMaximum(Calendar.DATE); jcmbBirthDay.removeAllItems(); for(int i = 0; i < maxDay; i++) jcmbBirthDay.addItem((i + 101 + "").substring(1)); jcmbBirthDay.setSelectedIndex(0); } } private void initBirthMonth() { jcmbBirthMonth.removeAllItems(); int i; for(i = 1; i < 13; i++) jcmbBirthMonth.addItem(((i+100)+"").substring(1)); jcmbBirthMonth.setSelectedIndex(2); } private void initBirthYear() { Calendar today = Calendar.getInstance(); int thisYear = today.get(Calendar.YEAR); int i; jcmbBirthYear.removeAllItems(); for(i = thisYear - MAX_AGE; i <= thisYear - MIN_AGE; i++) jcmbBirthYear.addItem(i); jcmbBirthYear.setSelectedIndex(MAX_AGE - DEFAULT_AGE); }
当年份变了时,判断月份的值是否为二月,如果是则对天数进行初始化;月份的值变化,也需要对天数初始化,如下处理:
jbtnExit.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent arg0) { myTool.showMessage(jfrmMain, "确定要退出吗?"); jfrmMain.dispose(); } } ); jcmbBirthYear.addItemListener ( new ItemListener() { public void itemStateChanged(ItemEvent arg0) { if(jcmbBirthMonth.getSelectedIndex() == 1) initBirthDay(); } } ); jcmbBirthMonth.addItemListener ( new ItemListener() { public void itemStateChanged(ItemEvent arg0) { initBirthDay(); } } );
对“退出”按钮操作:
jbtnExit.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent arg0) { myTool.showMessage(jfrmMain, "确定要退出吗?"); jfrmMain.dispose(); } } );
对“全选”“反选”“清空”按钮操作:
jbtnAllSelected.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent arg0) { setHobbySelected(true); } } ); jbtnRevSelected.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent arg0) { int i; for(i = 0; i < jckbHobby.length; i++) jckbHobby[i].setSelected(!jckbHobby[i].isSelected()); } } ); jbtnNonSelected.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent arg0) { setHobbySelected(false); } } );
最终界面为