www.pudn.com > student.rar > StudentSituation.java
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.regex.Pattern;
class StudentSituation
{//学生信息录入类
private JFrame frame = new JFrame();
private SourcePanel sourcePanel = new SourcePanel();
private JPanel buttonPanel = new JPanel();
//状态显示区域
private JLabel state = new JLabel("录入学生信息",JLabel.CENTER);
//按钮
private JButton ok = new JButton();
private JButton next = new JButton();
private JButton reset = new JButton();
private JButton cancel = new JButton();
private Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
private ActionListener listener = new Listener();
private static int WIDTH = 280;
private static int HEIGH = 320;
private void addButton(JButton button,String title)
{//添加按钮方法
button.setText(title);
button.addActionListener(listener);
buttonPanel.add(button);
}
private class Listener implements ActionListener
{//监听器
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("确定"))
{
addRecord("确定");
}
if(e.getActionCommand().equals("继续"))
{
addRecord("继续");
}
if(e.getActionCommand().equals("重置"))
{
sourcePanel.reset();
}
if(e.getActionCommand().equals("取消"))
{
frame.setVisible(false);
}
}
private void addRecord(String command)
{//录入信息
if(!Pattern.matches("\\d+",sourcePanel.getId()))
{//学号必须是数字
failWrite();
sourcePanel.setId("");
}
else if(!Pattern.matches("[\\S&&\\D]+",sourcePanel.getName()))
{//姓名不能为数字
failWrite();
sourcePanel.setNames("");
}
else if(isIdDup())
{//如果学号重复
failWrite();
state.setText("学号重复!");
}
else
{//如果都正确
if(command.equals("确定"))
{
StudentManager.getModel().addRow(getValue());
JOptionPane.showMessageDialog(frame,"录入成功!");
sourcePanel.reset();
frame.setVisible(false);
}
if(command.equals("继续"))
{//如果输入的学号和姓名的格式正确,则继续录入下一个
int num;
StudentManager.getModel().addRow(getValue());
state.setForeground(Color.getColor(""));
state.setText("录入成功!");
sourcePanel.reset();
num = Integer.parseInt(StudentManager.getData().lastElement().elementAt(0));
sourcePanel.setId(String.valueOf(num+1));//学号设置为下一号
}
}
}
private boolean isIdDup()
{//学号是否重复
for(Vector dataLine:StudentManager.getData())
if(sourcePanel.getId().equals(dataLine.elementAt(0)))
return true;
else
return false;
return false;
}
private void failWrite()
{//录入失败
state.setForeground(Color.red);
state.setText("格式错误,录入失败!");
}
private Vector getValue()
{//获取输入的信息
return new Student(
sourcePanel.getId(),
sourcePanel.getName(),
sourcePanel.getSex(),
sourcePanel.getAge(),
sourcePanel.getMajor(),
sourcePanel.getHome()).getInformation();
}
}
public StudentSituation(String title)
{/*构造方法*/
//添加按钮
addButton(ok,"确定");
addButton(next,"继续");
addButton(reset,"重置");
addButton(cancel,"取消");
//添加面板
frame.add(state,"North");
frame.add(sourcePanel,"Center");
frame.add(buttonPanel,"South");
//框架属性
frame.setBounds((dim.width-WIDTH)/2,(dim.height-HEIGH)/2,WIDTH,HEIGH);
frame.setResizable(false);
frame.setTitle(title);
}
public void show()
{//显示方法
frame.setVisible(true);
}
}