import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StudentManager{
public static void main(String[] args){
StudentFrame frame=new StudentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class StudentFrame extends JFrame{
public StudentFrame(){
setTitle("学生学籍管理系统");
setSize(WIDTH,HEIGHT);
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
JMenu menu=new JMenu("菜单");
menuBar.add(menu);
JMenuItem item1=new JMenuItem("登记");
menu.add(item1);
item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
item1.addActionListener(new insertListener());
JMenuItem item2=new JMenuItem("修改");
menu.add(item2);
item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
item2.addActionListener(new changeListener());
JMenuItem item3=new JMenuItem("查询");
menu.add(item3);
item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
item3.addActionListener(new inquestListener());
JMenuItem item4=new JMenuItem("删除");
menu.add(item4);
item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
item4.addActionListener(new deleteListener());
menu.addSeparator();
JMenuItem item5=new JMenuItem("退出");
menu.add(item5);
item5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
item5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
panel=new JPanel();
add(panel);
box=Box.createHorizontalBox();
box.add(new JLabel("<html><h1><i><b>welcome to student manager</b></i></h1></html>"),BorderLayout.CENTER);
panel.add(box);
}
private final static int WIDTH=600;
private final static int HEIGHT=400;
private final Box box;
private final JPanel panel;
private class insertListener implements ActionListener{
public void actionPerformed(ActionEvent e){
setTitle("记录学生信息");
remove(panel);
JPanel panel=new JPanel();
add(panel);
Box box=Box.createVerticalBox();
panel.add(box);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("学号:"));
box1.add(new JTextField(10));repaint();
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("姓名:"));
box2.add(new JTextField(10));repaint();
Box box3=Box.createHorizontalBox();
JRadioButton button1=new JRadioButton("男",true);
JRadioButton button2=new JRadioButton("女");
ButtonGroup group=new ButtonGroup();
group.add(button1);
group.add(button2);
box3.add(new JLabel("性别:"));
box3.add(button1);
box3.add(button2);repaint();
box.add(box1);
box.add(box2);
box.add(box3);
repaint();
}
}
private class changeListener implements ActionListener{
public void actionPerformed(ActionEvent e){
setTitle("修改学生信息");
remove(panel);
repaint();
}
}
private class inquestListener implements ActionListener{
public void actionPerformed(ActionEvent e){
setTitle("查询学生信息");
remove(panel);
repaint();
}
}
private class deleteListener implements ActionListener{
public void actionPerformed(ActionEvent e){
setTitle("删除学生信息");
remove(panel);
repaint();
}
}
}
问题1 我选择了“登记”,原来的内容消失了,可是新的内容没出来,只有把对话框拉大或者拉小,才能出来
问题2 当我先选择了“登记”,后来又要选择“删除”时,如何使界面只显示“删除”的相关内容,“登记”的内容消失。
要是还有那些可以优化的地方,请大家指出来,谢谢!!!
----------------解决方案--------------------------------------------------------
......
JRadioButton button2=new JRadioButton("女");
ButtonGroup group=new ButtonGroup();
group.add(button1);
group.add(button2);
box3.add(new JLabel("性别:"));
box3.add(button1);
box3.add(button2);repaint();
box.add(box1);
box.add(box2);
box.add(box3);
vaildate() // <--加上这一句看一下。
repaint();
}
}
..........
----------------解决方案--------------------------------------------------------