import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
//定义学生类的基本信息
class student
{
//私有定义学生信息
private String name;
private String id;
private String className;
private int score_1;
private int score_2;
public String score1;
public String score2;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setId(String id)
{
this.id=id;
}
public String getId()
{
return id;
}
public void setClassName(String className)
{
this.className=className;
}
public String getClassName()
{
return className;
}
public void setScore_1(int score_1)
{
this.score_1=score_1;
}
public int getScore_1()
{
return score_1;
}
public void setScore_2(int score_2)
{
this.score_2=score_2;
}
public int getScore_2()
{
return score_2;
}
}
//定义gui类框图显示
class gui{
private Frame frame;//主界面
private Button addButton,delectButton,changeButton,inspectButton,inspectAllButton,showButton;
//主界面上的六个按键
static student []stu = new student[100];//
static int number=0;
static int flag =0;//addButton里面的标志
static int flag1=0;//changeButton里面的标志
private MenuBar menuBar;
private Menu menu;
private MenuItem openItem,saveItem;
private FileDialog openDialog,saveDialog;//打开和保存窗口
private File file;
public gui()
{
init();
}
public void init()
{
frame = new JFrame("学生管理系统");
frame.setBounds(500, 200, 300, 400);
frame.setLayout(new FlowLayout());
menuBar = new MenuBar();
menu = new Menu("菜单");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
menu.add(openItem);
menu.add(saveItem);
menuBar.add(menu);
frame.setMenuBar(menuBar);
addButton = new Button("添加学生信息");
delectButton = new Button("删除学生信息");
changeButton = new Button("修改学生信息");
inspectButton = new Button("查看某个学生信息");
inspectAllButton = new Button("查看所有学生的信息");
showButton = new Button("排名榜");
frame.setLayout(new GridLayout(3,2,10,10));
frame.add(addButton);
frame.add(delectButton);
frame.add(changeButton);
frame.add(inspectButton);
frame.add(inspectAllButton);
frame.add(showButton);
myEvent();//事件函数
frame.setVisible(true);
}
public void myEvent()//事件函数内容
{
//关闭窗体事件
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//保存文本事件
saveItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
saveDialog = new FileDialog(frame,"保存",FileDialog.SAVE);
if(file==null)
{
saveDialog.setVisible(true);
String fileName = saveDialog.getFile();
String directionName = saveDialog.getDirectory();
if(fileName == null || directionName == null)
return ;
file = new File(directionName,fileName);
}
try {
BufferedWriter bwr = new BufferedWriter(new FileWriter(file));
String text = "班级 姓名 学号 java成绩 电路设计成绩\r\n";
for(int i=0;i<number;i++)
{
text +=stu[i].getClassName()+" "+stu[i].getName()+" "+stu[i].getId()
+" "+stu[i].score1+" "+stu[i].score2+"\r\n";
}
bwr.write(text);//写入
bwr.flush();
bwr.close();
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
});
//读取文本事件
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openDialog = new FileDialog(frame,"读取文本",FileDialog.LOAD);
openDialog.setVisible(true);
String fileName = openDialog.getFile();
String directionName = openDialog.getDirectory();
if(fileName == null || directionName == null)
return ;
file = new File(directionName,fileName);
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
String line = null;
String str="";
while((line=buff.readLine())!=null)
{
str +=line+" ";
}
String[] s = str.split("\\s+");//很厉害的一种方法 以空格分割
buff.close();
for(int i=5;i<=s.length-1;i++)
if(i%5==0)
{
//将读到的值赋给变量
stu[i/5-1]=new student();
stu[i/5-1].setClassName(s[i]);
stu[i/5-1].setName(s[i+1]);
stu[i/5-1].setId(s[i+2]);
//stu[i/5].setScore_1(Integer.parseInt(s[i+3]));
//stu[i/5].setScore_2(Integer.parseInt(s[i+4]));
stu[i/5-1].score1=s[i+3];
stu[i/5-1].score2=s[i+4];
if(stu[i/5-1].score1.equals("未选"))//为了免除影响冠军榜的排名
{
stu[i/5-1].setScore_1(0);
}
else
{
stu[i/5-1].setScore_1(Integer.parseInt(stu[i/5-1].score1));
}
if(stu[i/5-1].score2.equals("未选"))
{
stu[i/5-1].setScore_2(0);
}
else
{
stu[i/5-1].setScore_2(Integer.parseInt(stu[i/5-1].score2));
}
number++;
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
//添加学生信息事件
addButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
flag =0;
JFrame dialog = new JFrame("添加学生信息");
JPanel jp1, jp2 ,jp3,jp4,jp5;
dialog.setBounds(500, 200, 300, 400);
dialog.setVisible(true);
dialog.setLayout(new GridLayout(5,1));
Label labelClassName = new Label("班级:");
TextField text_1 = new TextField(20);
jp1=new JPanel();
jp1.add(labelClassName); jp1.add(text_1);
Label labelName = new Label("姓名:");
TextField text_2 = new TextField(20);
jp2=new JPanel();
jp2.add(labelName); jp2.add(text_2);
Label labelId = new Label("学号:");
TextField text_3 = new TextField(20);
jp3=new JPanel();
jp3.add(labelId); jp3.add(text_3);
dialog.add(jp1); dialog.add(jp2); dialog.add(jp3);
JButton button1 = new JButton("java语言程序设计成绩输入");
JButton button2 = new JButton("电路设计成绩输入");
jp4=new JPanel();
jp4.add(button1); jp4.add(button2);
dialog.add(jp4);
JButton button3 = new JButton("继续输入");
JButton button4 = new JButton("关闭");
jp5=new JPanel();
jp5.add(button3); jp5.add(button4);
dialog.add(jp5);
//填写java成绩事件
button1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
//建立一个窗口填写java成绩
Dialog dia = new Dialog(dialog,"填写java成绩");
dia.setBounds(550, 250, 200, 300);
dia.setVisible(true);
//防止未填写学生基本信息
if((text_1.getText().isEmpty())||(text_2.getText().isEmpty())||(text_3.getText().isEmpty()))
{
dia.setVisible(false);
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"学生基本信息未填写完整!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
dia.setLayout(new FlowLayout());
Label la1 = new Label("平时成绩");
TextField tf1 =new TextField(20);
TextField tf2 =new TextField(20);
TextField tf3 =new TextField(20);
TextField tf4 =new TextField(20);
TextField tf5 =new TextField(20);
Label la2 = new Label("期末成绩");
TextField tf6 =new TextField(20);
dia.add(la1);
dia.add(tf1);
dia.add(tf2);
dia.add(tf3);
dia.add(tf4);
dia.add(tf5);
dia.add(la2);
dia.add(tf6);
JButton btn = new JButton("保存");
dia.add(btn);
//保存之后事件
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
if((tf1.getText()!=null)&&(tf2.getText()!=null)&&(tf3.getText()!=null)&&(tf4.getText()!=null)
&&(tf5.getText()!=null)&&(tf6.getText()!=null))
{
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
int c = Integer.parseInt(tf3.getText());
int d = Integer.parseInt(tf4.getText());
int f = Integer.parseInt(tf5.getText());
int g = Integer.parseInt(tf6.getText());
int h= (int)((a+b+c+d+f)/5*0.8+g*0.2);
//计算学生java成绩
if(flag==0)
{
flag=1;
stu[number] = new student();
stu[number].setClassName(text_1.getText());
stu[number].setName(text_2.getText());
stu[number].setId(text_3.getText());
stu[number].setScore_1(h);
stu[number].score1=stu[number].getScore_1()+"";
stu[number].score2="未选";//为防止没有电路设计课
number++;
}
else
{
stu[number-1].setScore_1(h);
stu[number-1].score1=stu[number-1].getScore_1()+"";
}
tf1.setText("");tf2.setText("");tf3.setText("");
tf4.setText("");tf5.setText("");tf6.setText("");//清空
}
dia.dispose();//关闭窗口
}
});
dia.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dia.dispose();
}
});
}
});
//填写电路设计成绩事件
button2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
Dialog dia = new Dialog(dialog,"填写电路设计成绩");
dia.setBounds(550, 250, 200, 300);
dia.setVisible(true);
//防止未填写学生基本信息
if((text_1.getText().isEmpty())||(text_2.getText().isEmpty())||(text_3.getText().isEmpty()))
{
dia.setVisible(false);
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"学生基本信息未填写完整!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
dia.setLayout(new FlowLayout());
Label la1 = new Label("平时成绩");
TextField tf1 =new TextField(20);
Label la2 = new Label("期末成绩");
TextField tf2 =new TextField(20);
dia.add(la1);
dia.add(tf1);
dia.add(la2);
dia.add(tf2);
JButton btn = new JButton("保存");
dia.add(btn);
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
if((tf1.getText()!=null)&&(tf2.getText()!=null))
{
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
//得到学生成绩
if(flag==0)
{
flag=1;
stu[number] = new student();
stu[number].setClassName(text_1.getText());
stu[number].setName(text_2.getText());
stu[number].setId(text_3.getText());
stu[number].setScore_2((a+b)/2);
stu[number].score2=stu[number].getScore_2()+"";
stu[number].score1="未选";
number++;
}
else
{
stu[number-1].setScore_2((a+b)/2);
stu[number-1].score2=stu[number-1].getScore_2()+"";
}
tf1.setText("");
tf2.setText("");
}
dia.dispose();
}
});
dia.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dia.dispose();
}
});
}
});
button3.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
text_1.setText("");
text_2.setText("");
text_3.setText("");//清空学生基本信息
flag=0;//清除标志位
}
});
button4.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
dialog.dispose();
}
});
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dialog.dispose();
}
});
}
});
//删除学生信息事件
delectButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
String studentid=JOptionPane.showInputDialog("请输入你要删除的学生的学号:");
int i,j=0;
for(i=0;i<number;i++)
{
if(studentid.equals(stu[i].getId()))
{
for(;i<number-1;i++)
stu[i]=stu[i+1];
number -= 1;
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"学生信息已删除!", "提示", JOptionPane.INFORMATION_MESSAGE);
j=1;
break;
}
}
if(i==number&&j==0)
{
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"该学生未找到!", "提示", JOptionPane.INFORMATION_MESSAGE);
j=0;
}
}
});
//改变学生信息事件
changeButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
String studentid=JOptionPane.showInputDialog("请输入你要修改学生的学号:");
int i;
for( i=0;i<number;i++)
{
if(studentid.equals(stu[i].getId()))
{
final int n =i;
flag1=0;
JFrame dialog = new JFrame("修改学生信息");
JPanel jp1, jp2 ,jp3,jp4;
dialog.setBounds(500, 200, 300, 400);
dialog.setVisible(true);
dialog.setLayout(new GridLayout(5,1));
Label labelClassName = new Label("班级:");
TextField text_1 = new TextField(20);
jp1=new JPanel();
jp1.add(labelClassName); jp1.add(text_1);
Label labelName = new Label("姓名:");
TextField text_2 = new TextField(20);
jp2=new JPanel();
jp2.add(labelName); jp2.add(text_2);
Label labelId = new Label("学号:");
TextField text_3 = new TextField(20);
jp3=new JPanel();
jp3.add(labelId); jp3.add(text_3);
dialog.add(jp1); dialog.add(jp2); dialog.add(jp3);
JButton button1 = new JButton("java语言程序设计成绩输入");
JButton button2 = new JButton("电路设计成绩输入");
jp4=new JPanel();
jp4.add(button1); jp4.add(button2);
dialog.add(jp4);
button1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
Dialog dia = new Dialog(dialog,"填写java成绩");
dia.setBounds(550, 250, 200, 300);
dia.setVisible(true);
if((text_1.getText().isEmpty())||(text_2.getText().isEmpty())||(text_3.getText().isEmpty()))
{
dia.setVisible(false);
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"学生基本信息未填写完整!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
dia.setLayout(new FlowLayout());
Label la1 = new Label("平时成绩");
TextField tf1 =new TextField(20);
TextField tf2 =new TextField(20);
TextField tf3 =new TextField(20);
TextField tf4 =new TextField(20);
TextField tf5 =new TextField(20);
Label la2 = new Label("期末成绩");
TextField tf6 =new TextField(20);
dia.add(la1);
dia.add(tf1);
dia.add(tf2);
dia.add(tf3);
dia.add(tf4);
dia.add(tf5);
dia.add(la2);
dia.add(tf6);
JButton btn = new JButton("保存");
dia.add(btn);
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
if((tf1.getText()!=null)&&(tf2.getText()!=null)&&(tf3.getText()!=null)&&(tf4.getText()!=null)
&&(tf5.getText()!=null)&&(tf6.getText()!=null))
{
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
int c = Integer.parseInt(tf3.getText());
int d = Integer.parseInt(tf4.getText());
int f = Integer.parseInt(tf5.getText());
int g = Integer.parseInt(tf6.getText());
int h= (int)((a+b+c+d+f)/5*0.8+g*0.2);
if(flag1==0) {
flag1=1;
stu[n].setClassName(text_1.getText());
stu[n].setName(text_2.getText());
stu[n].setId(text_3.getText());
stu[n].score2="未选";
stu[n].setScore_1(h);
stu[n].score1=stu[n].getScore_1()+"";
}
else
{
stu[n].setScore_1(h);
stu[n].score1=stu[n].getScore_1()+"";
}
}
dia.dispose();
}
});
dia.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dia.dispose();
}
});
}
});
button2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
Dialog dia = new Dialog(dialog,"填写电路设计成绩");
dia.setBounds(550, 250, 200, 300);
dia.setVisible(true);
if((text_1.getText().isEmpty())||(text_2.getText().isEmpty())||(text_3.getText().isEmpty()))
{
dia.setVisible(false);
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"学生基本信息未填写完整!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
dia.setLayout(new FlowLayout());
Label la1 = new Label("平时成绩");
TextField tf1 =new TextField(20);
Label la2 = new Label("期末成绩");
TextField tf2 =new TextField(20);
dia.add(la1);
dia.add(tf1);
dia.add(la2);
dia.add(tf2);
JButton btn = new JButton("保存");
dia.add(btn);
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
if((tf1.getText()!=null)&&(tf2.getText()!=null))
{
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
if(flag1==0)
{
flag1=1;
stu[n] = new student();
stu[n].setClassName(text_1.getText());
stu[n].setName(text_2.getText());
stu[n].setId(text_3.getText());
stu[n].setScore_2((a+b)/2);
stu[n].score2=stu[n].getScore_2()+"";
stu[n].score2="未选";
}
else
{
stu[n].setScore_2((a+b)/2);
stu[n].score2=stu[n].getScore_2()+"";
}
}
dia.dispose();
}
});
dia.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dia.dispose();
}
});
}
});
break;
}
}
if(i==number)
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"该学生未找到!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
});
//查看某个学生信息事件
inspectButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
String studentid=JOptionPane.showInputDialog("请输入你要查看的学生的学号:");
int i;
for( i=0;i<number;i++)
{
if(studentid.equals(stu[i].getId()))
{
String in = stu[i].getClassName()+" "+stu[i].getName()+" "+stu[i].getId()+" "+stu[i].score1+" "+stu[i].score2;
String out ="班级 姓名 学号 java成绩 电路成绩\n"+in;
JOptionPane.showMessageDialog(null,out);
break;
}
}
if(i==number)
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"该学生未找到!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
});
//查看所有学生信息事件
inspectAllButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
Dialog dialog = new Dialog(frame,"查看所有学生的信息");
dialog.setBounds(500, 200, 450, 400);
dialog.setVisible(true);
if(number==0)
{
dialog.dispose();
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"没有学生信息!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
TextArea area = new TextArea();
dialog.add(area);
String in="";
for(int i=0;i<number;i++)
{
in += stu[i].getClassName()+" "+stu[i].getName()+" "+stu[i].getId()+" "+stu[i].score1+" "+stu[i].score2+"\n";
}
String out ="班级 姓名 学号 java成绩 电路成绩\n"+in;
area.setText(out);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dialog.dispose();
}
});
}
});
//查看冠军榜事件
showButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
Dialog dialog = new Dialog(frame,"排名榜");
dialog.setBounds(500, 200, 350, 400);
dialog.setVisible(true);
if(number==0)
{
dialog.dispose();
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"没有学生信息!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
TextArea area = new TextArea();
student s1 = new student();
student s2 = new student();
s1=stu[0];s2=stu[0];
for(int i=1;i<number;i++)
{
for(int j=0;j<number-i;j++)
{
if(stu[j].getScore_1()<stu[j+1].getScore_1())
{
s1=stu[j];
stu[j]=stu[j+1];
stu[j+1]=s1;
}
}
}
String s="java语言程序设计排名\r\n";
for(int i=0;i<number;i++)
{
s +=stu[i].getName()+" "+stu[i].getId()+" "+stu[i].score1+"\r\n";
}
for(int i=1;i<number;i++)
{
for(int j=0;j<number-i;j++)
{
if(stu[j].getScore_2()<stu[j+1].getScore_2())
{
s2=stu[j];
stu[j]=stu[j+1];
stu[j+1]=s2;
}
}
}
String stri = "电子设计排名\r\n";
for(int i=0;i<number;i++)
{
stri +=stu[i].getName()+" "+stu[i].getId()+" "+stu[i].score2+"\r\n";
}
area.setText(s+stri);
dialog.add(area);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dialog.dispose();
}
});
}
});
}
}
//主函数
public class work {
public static void main(String[] args) {
// TODO Auto-generated method stub
new gui();
}
}
学生管理系统
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...
- 一、复习 第1、2节: 没有目标和目标过于单一的人生,是充满低效、无序、不专注的线下人生的状态. 第3节: 进行时...
- 怎样反思自己的教学——学习杜威《我们怎样思维》有感 2015-01-13 14:56:52 来源:普宁市第三中学网...