if(al.get(i).getNum()==n){
returnal.get(i);
}
}
returnnull;
}
}
③
登陆界面及成绩管理的所有界面,同过创建StuC的对象,调用其方法实现成绩管理
代码:
packagekeshe;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjavax.swing.*;
publicclassLoginextendsJFrame{
privateTextFieldf1;
privateTextFieldf2;
privateJButtonb1;
privateJButtonb2;
privateJButtonb3;
StuCscs=newStuC();
//登陆界面
publicLogin(){
Containercp=getContentPane();//容器
cp.setLayout(newGridLayout(3,1));//三行一列布局
Labell1=newLabel("用户名");
Labell2=newLabel("密码");
Panelp1=newPanel();
Panelp2=newPanel();
Panelp3=newPanel();
f1=newTextField(10);
f2=newTextField(10);
f2.setEchoChar('*');//回显字符为*
b1=newJButton("登录");
b2=newJButton("重置");
b3=newJButton("退出");
p1.add(l1);//第一行添加label1
p1.add(f1);
p2.add(l2);
p2.add(f2);
p3.add(b1);
p3.add(b2);
p3.add(b3);
cp.add(p1);
cp.add(p2);
cp.add(p3);
b1.addActionListener(newEnter());
b2.addActionListener(newReWrite());
b3.addActionListener(newClose());
}
classEnterimplementsActionListener{
publicvoidactionPerformed(ActionEvente)
{
if((f1.getText()).equals("yazhou")&&(f2.getText()).equals("123456"))
{
scs.read();//初始化,从文件读入信息
XueShengframe1=newXueSheng();
frame1.setBounds(200,200,300,300);
frame1.setVisible(true);
}
elseJOptionPane.showMessageDialog(null,"用户名或密码错误,请重新登录!
");
}
}
classReWriteimplementsActionListener{
publicvoidactionPerformed(ActionEvente)
{
f1.setText("");
f2.setText("");
f1.requestFocus();
}
}
classCloseimplementsActionListener{
publicvoidactionPerformed(ActionEvente)
{
JButtonbt=(JButton)e.getSource();
if(bt==b3){
System.exit(0);
}
}
}
//主函数程序开始
publicstaticvoidmain(String[]args){
Loginlog=newLogin();
log.setTitle("系统登录");
log.setBounds(200,200,300,300);
log.setBackground(Color.blue);
log.setVisible(true);
}
//信息管理界面内部类进行初始化和保存
classXueShengextendsJFrameimplementsActionListener{
privateJButtoncx,zj,tc,sc,xg,tj;
publicXueSheng()
{
Containerc=this.getContentPane();
c.setLayout(newGridLayout(3,1));
c.setFont(newFont("true",Font.TRUETYPE_FONT,13));
JPanelpanel2=newJPanel();
JPanelpanel1=newJPanel();
JLabellabel1=newJLabel("欢迎进入成绩管理",SwingConstants.CENTER);
label1.setFont(newFont("true",Font.TRUETYPE_FONT,13));
label1.setForeground(Color.blue);
c.add(label1);
//添加按钮
cx=newJButton("查询");
panel2.add(cx);
zj=newJButton("增加");
panel2.add(zj);
sc=newJButton("删除");
panel2.add(sc);
tc=newJButton("退出");
panel2.add(tc);
xg=newJButton("修改");
panel1.add(xg);
tj=newJButton("统计");
panel1.add(tj);
c.add(panel2);
c.add(panel1);
cx.addActionListener(this);
zj.addActionListener(this);
sc.addActionListener(this);
xg.addActionListener(this);
tc.addActionListener(this);
tj.addActionListener(this);
this.setVisible(true);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==cx){
Findf=newFind();
}
if(e.getSource()==zj){
AddFIf=newAddFI();
}
if(e.getSource()==sc){
Deletd=newDelet();
}
if(e.getSource()==xg){
XiuGaix=newXiuGai();
}
if(e.getSource()==tc){
shutDown();
}
if(e.getSource()==tj){
Tongjit=newTongji();
}
}
privatevoidshutDown()
{
scs.stor();
JOptionPane.showMessageDialog(null,"信息已保存");
this.dispose();
}
}
//增加信息界面内部类,捕获文本框中信息创建Student对象,添加到Arraylist中,如果已存在该学号//给出提示信息,并重新添加。
classAddFIextendsJFrameimplementsActionListener{
privateJTextFieldSTNOText,SNAMEText,MAText,CHIText,JAVAText;
privateJButtonb1,b2,b3;
privateStringSTNO,SNAME,MAT,CHI,JAVA;
publicAddFI(){
super("添加学生信息");
Containerc2=this.getContentPane();
c2.setLayout(newGridLayout(3,1));
JPanelcenter=newJPanel(newGridLayout(5,2));
JPanellow=newJPanel(newFlowLayout());
JLabellabel1=newJLabel("添加学生信息",SwingConstants.CENTER);
label1.setFont(newFont("TRUE",Font.TRUETYPE_FONT,20));
c2.add(label1);
STNOText=newJTextField(30);//30列文本框
SNAMEText=newJTextField(30);
CHIText=newJTextField(30);
MAText=newJTextField(30);
JAVAText=newJTextField(30);
center.add(newJLabel("学号",SwingConstants.CENTER));//添加标签学号写在标签中间
center.add(STNOText);//添加文本框
center.add(newJLabel("姓名",SwingConstants.CENTER));
center.add(SNAMEText);
center.add(newJLabel("语文",SwingConstants.CENTER));
center.add(CHIText);
center.add(newJLabel("数学",SwingConstants.CENTER));
center.add(MAText);
center.add(newJLabel("java",SwingConstants.CENTER));
center.add(JAVAText);
c2.add(center);
b1=newJButton("添加");
b2=newJButton("清除");
b3=newJButton("退出");
low.add(b1);
low.add(b2);
low.add(b3);
c2.add(low);
//添加监听
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
this.setBounds(200,200,600,400);
this.setVisible(true);
this.setTitle("添加学生信息");
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==b1){
try{
addFI();
}catch(FileNotFoundExceptione1){
e1.printStackTrace();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
if(e.getSource()==b2){
clearForm();
}
if(e.getSource()==b3){
this.dispose();
}
}
privatevoidaddFI()throwsFileNotFoundException,IOException{
STNO=STNOText.getText();
SNAME=SNAMEText.getText();
CHI=CHIText.getText();
MAT=MAText.getText();
JAVA=JAVAText.getText();
if(STNO.length()==0||SNAME.length()==0||MAT.length()==0
||JAVA.length()==0||CHI.length()==0)
JOptionPane.showMessageDialog(this,"请添加完全信息");
else{
Studenta=newStudent