📄 mystu.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.io.*;
class Savestu implements Serializable{ //保存数据
Label nn=new Label("",Label.CENTER);
Label ii=new Label("");
Label yy=new Label("",Label.CENTER);
Label ss=new Label("",Label.CENTER);
Label ee=new Label("",Label.CENTER);
Label pp=new Label("",Label.CENTER);
Label zz=new Label("",Label.CENTER);
}
public class Mystu extends JFrame {
static int xn=0;
Panel p1=new Panel();
Panel p2=new Panel();
Button zj=new Button("增加记录");
Button del=new Button("删除记录");
Button sg=new Button("修改记录");
Button find=new Button("查询记录");
Button open=new Button("打开文件");
Button save=new Button("保存文件");
Savestu[] stu=new Savestu[10];
zjwindow zj1=new zjwindow(); //必须放在外面,否则其它类找不到此符号
Dialog alarm=new Dialog(zj1,"警告!",true);
myfile mf=new myfile(this);
public Mystu(){
super("学生成绩管理系统---JIN");
p1.setLayout(new GridLayout(6,1,5,8));
p1.add(zj);
p1.add(del);
p1.add(sg);
p1.add(find);
p1.add(open);
p1.add(save);
p2.setLayout(new GridLayout(11,7,2,2));
Button name=new Button("姓名");
Button id=new Button("学号");
Button chinese=new Button("语文");
Button math=new Button("数学");
Button en=new Button("英语");
Button avg=new Button("平均分");
Button sum=new Button("总分");
p2.add(id);
p2.add(name);
p2.add(chinese);
p2.add(math);
p2.add(en);
p2.add(avg);
p2.add(sum);
for(int x=0;x<10;x++)
stu[x]=new Savestu();
for(int x=0;x<10;x++)
{p2.add(stu[x].ii);
p2.add(stu[x].nn);
p2.add(stu[x].yy);
p2.add(stu[x].ss);
p2.add(stu[x].ee);
p2.add(stu[x].pp);
p2.add(stu[x].zz);
}
add(p1,BorderLayout.WEST);
add(p2,BorderLayout.CENTER);
//注册监听
zj.addActionListener(new ActionHandler());
sum.addActionListener(new sort());
chinese.addActionListener(new sort());
math.addActionListener(new sort());
en.addActionListener(new sort());
avg.addActionListener(new sort());
open.addActionListener(new filehander());
save.addActionListener(new filehander());
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main (String[] args) {
Mystu jin=new Mystu();
jin.setBounds(200,100,700,500);
jin.setVisible(true);}
class zjwindow extends JFrame{ //增加数据窗口
TextField zji=new TextField(10);
TextField zjn=new TextField(10);
TextField zjy=new TextField(10);
TextField zjs=new TextField(10);
TextField zje=new TextField(10);
public zjwindow()
{super("增加学生数据");
Panel zjw=new Panel();
Button zjsure=new Button("确定");
Button zjcancel=new Button("取消");
zjw.setLayout(new GridLayout(6,2));
zjw.add(new Label("学号",Label.CENTER));
zjw.add(zji);
zjw.add(new Label("姓名",Label.CENTER));
zjw.add(zjn);
zjw.add(new Label("语文",Label.CENTER));
zjw.add(zjy);
zjw.add(new Label("数学",Label.CENTER));
zjw.add(zjs);
zjw.add(new Label("英语",Label.CENTER));
zjw.add(zje);
zjw.add(zjsure);
zjw.add(zjcancel);
add(zjw);
pack();
zjsure.addActionListener(new zjAction());
zjcancel.addActionListener(new zjAction());
}
class zjAction implements ActionListener{ //监听
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="确定")
{
if((zji.getText().equals(""))||(zjn.getText().equals(""))||(zjy.getText().equals(""))||(zjs.getText().equals(""))||(zje.getText().equals("")))
{ Button alasure=new Button("确定继续");
alasure.addActionListener(new alahander());
alarm.add(new Label("请输入完整数据,再继续。。。"),BorderLayout.CENTER);
alarm.add(alasure,BorderLayout.SOUTH);
alarm.setBounds(330,200,200,150);
alarm.setVisible(true);
}
else{
float temp;
stu[xn].ii.setText(zji.getText());
stu[xn].nn.setText(zjn.getText());
stu[xn].yy.setText(zjy.getText());
stu[xn].ss.setText(zjs.getText());
stu[xn].ee.setText(zje.getText());
temp=Float.parseFloat(zjy.getText())+Float.parseFloat(zjs.getText())+Float.parseFloat(zje.getText());
stu[xn].zz.setText(""+temp+"");
stu[xn].pp.setText(""+temp/3+"");
xn++;
zji.setText("");
zjn.setText("");
zjy.setText("");
zjs.setText("");
zje.setText("");
zj1.setVisible(false);
} }
if(e.getActionCommand()=="取消")
{ zji.setText("");
zjn.setText("");
zjy.setText("");
zjs.setText("");
zje.setText("");
zj1.setVisible(false);
}
}
}
}
class myfile{
FileDialog fdlg;
public myfile(JFrame parent){
fdlg=new FileDialog(parent,"",FileDialog.LOAD);
}
String getpath(){
return fdlg.getDirectory()+"\\"+fdlg.getFile();
}
void save()throws IOException{
fdlg.setTitle("保存学生信息");
fdlg.setMode(FileDialog.SAVE);
fdlg.setVisible(true);
FileOutputStream fsave=new FileOutputStream(getpath());
ObjectOutputStream fse=new ObjectOutputStream(fsave);
fse.writeObject(stu);
System.out.print("jin");
fse.close();
}
void open()throws ClassNotFoundException,IOException{
fdlg.setTitle("打开学生信息");
fdlg.setMode(FileDialog.LOAD);
fdlg.setVisible(true);
FileInputStream fopen=new FileInputStream(getpath());
ObjectInputStream fon=new ObjectInputStream(fopen);
Savestu[] rsstu=(Savestu[])fon.readObject();
fon.close();
//stu[1].ii.setText(stu[0].ii.getText());
//stu[3].ii.setText("3545");
for(int k=0;k<rsstu.length;k++){
stu[k].ii.setText(rsstu[k].ii.getText());
stu[k].nn.setText(rsstu[k].nn.getText());
stu[k].yy.setText(rsstu[k].yy.getText());
stu[k].ss.setText(rsstu[k].ss.getText());
stu[k].ee.setText(rsstu[k].ee.getText());
stu[k].pp.setText(rsstu[k].pp.getText());
stu[k].zz.setText(rsstu[k].zz.getText());
}
System.out.print("lai");
}
}
class alahander implements ActionListener{
public void actionPerformed(ActionEvent e){
alarm.setVisible(false);}
}
class ActionHandler implements ActionListener{ //监听增加等功能实现
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="增加记录")
{
zj1.setBounds(460,200,250,250);
zj1.setVisible(true);
} }
}
class sort implements ActionListener{ //很笨拙的方法,主要是刚开始结构就不好。
public void actionPerformed(ActionEvent e){
Savestu temp=new Savestu();
int power=0;
for(int j=0;j<xn;j++)
{ for(int k=j+1;k<xn;k++)
{
if(e.getActionCommand()=="平均分"||e.getActionCommand()=="总分")
{if(Float.parseFloat(stu[j].pp.getText())<Float.parseFloat(stu[k].pp.getText()))
power=1;
}else if(e.getActionCommand()=="语文")
{if(Float.parseFloat(stu[j].yy.getText())<Float.parseFloat(stu[k].yy.getText()))
power=1;}
else if(e.getActionCommand()=="数学")
{if(Float.parseFloat(stu[j].ss.getText())<Float.parseFloat(stu[k].ss.getText()))
power=1;}
else
{if(Float.parseFloat(stu[j].ee.getText())<Float.parseFloat(stu[k].ee.getText()))
power=1;}
if(power==1)
{ temp.ii.setText(stu[k].ii.getText());
temp.nn.setText(stu[k].nn.getText());
temp.yy.setText(stu[k].yy.getText());
temp.ss.setText(stu[k].ss.getText());
temp.ee.setText(stu[k].ee.getText());
temp.pp.setText(stu[k].pp.getText());
temp.zz.setText(stu[k].zz.getText());
stu[k].ii.setText(stu[j].ii.getText());
stu[k].nn.setText(stu[j].nn.getText());
stu[k].yy.setText(stu[j].yy.getText());
stu[k].ss.setText(stu[j].ss.getText());
stu[k].ee.setText(stu[j].ee.getText());
stu[k].pp.setText(stu[j].pp.getText());
stu[k].zz.setText(stu[j].zz.getText());
stu[j].ii.setText(temp.ii.getText());
stu[j].nn.setText(temp.nn.getText());
stu[j].yy.setText(temp.yy.getText());
stu[j].ss.setText(temp.ss.getText());
stu[j].ee.setText(temp.ee.getText());
stu[j].pp.setText(temp.pp.getText());
stu[j].zz.setText(temp.zz.getText());
power=0;
}
}
}
}
}
class filehander implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="打开文件")
{try{ mf.open();
}catch(IOException ie) {System.out.print("打开出错");}
catch(ClassNotFoundException ie) {System.out.print("打开出错");}
}else{
try{mf.save();
}catch(IOException ie) {System.out.print("保存出错");}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -