第二次了,见到管理系统这东西就恶心,为什么每一本编程教材都要玩这个?不过用java再写一次,倒是蛮有新意。留作纪念,看看到大学毕业,还会写几次这种破管理系统。。。。。。粗糙的可视化,鸡肋再现啊。
后果(代码看了估计都会吐血):
代码:
/*
author:YuGuichun
fuction:student information manage
time:2011.5.28
*/
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
//import java.io.*;
import java.util.*;
class Student
{
private String name;
private int number;
private String sex;
private int brithdate;
//---------------------定义一个学生类------------------------------
Student()
{
this.name="";
this.number=21;
this.sex="";
this.brithdate=0;
}
Student(String name,int number,String sex,int brithdate)
{
this.name=name;
this.number=number;
this.sex=sex;
this.brithdate=brithdate;
}
public void setName(String name)
{
this.name=name;
}
public void setNumber(int number)
{
this.number=number;
}
public void setSex(String sex)
{
this.sex=sex;
}
public void setBrithdate(int brithdate)
{
this.brithdate=brithdate;
}
public String getName()
{
return this.name;
}
public int getNumber()
{
return this.number;
}
public String getSex()
{
return this.sex;
}
public int getBrithdate()
{
return this.brithdate;
}
public void setAll(String name1,int number1,String sex1,int brithdate1)
{
this.name=name1;
this.number=number1;
this.sex=sex1;
this.brithdate=brithdate1;
}
public String getAll()
{
String output="";
output+=getName()+" "+getNumber()+" "+getSex()+" "+getBrithdate();
return output;
}
}
class allVector
{
public static Vector<Student> vector=new Vector<Student>();
public void addVector(Student s)
{
vector.add(s);
}
}
//---------------------------------------主类--------------------------------------------
public class Work7_8 extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = -4378344080017000098L;
private Container pane;
private GridBagConstraints constraints;
private GridBagLayout layout;
private JTextField textdisplay;
//public static Vector<Student> vector=new Vector<Student>();
public Work7_8()//构造函数
{
super("又是学生管理系统");
pane=getContentPane();
layout=new GridBagLayout();
pane.setLayout(layout);
constraints=new GridBa
gConstraints();
textdisplay=new JTextField(10);
textdisplay.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=2;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=1;
constraints.weighty=1;
layout.setConstraints(textdisplay,constraints);
pane.add(textdisplay);
addButton("添加学生",0,2,1,1);
addButton("修改信息",1,2,1,1);
addButton("删除学生",0,3,1,1);
addButton("浏览学生",1,3,1,1);
addButton("统计学生人数",0,4,4,1);
setSize(300,230);
setVisible(true);
}
//---------------------添加按钮的方法----------------------------
public void addButton(String label,int gridx,int gridy,int gridwidth,int gridheight)
{
JButton button=new JButton(label);
constraints.gridx=gridx;
constraints.gridy=gridy;
constraints.gridwidth=gridwidth;
constraints.gridheight=gridheight;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(this);
layout.setConstraints(button,constraints);
pane.add(button);
}
//------------------按钮产生的动作------------------------------
public void actionPerformed(ActionEvent e)
{
String input=(e.getActionCommand());
allVector V=new allVector();
//--------------------添加学生----------------------------
if(input.equals("添加学生"))
{
SmainFrame B = new SmainFrame();
}
//--------------------浏览学生----------------------------
else if(input.equals("浏览学生"))
{
String retult="";
if(V.vector.isEmpty())
JOptionPane.showMessageDialog(null, "管理系统中的学生为空");
else
{
String out="姓名 学号 性别 出生年月日"+"\n";
Iterator<Student> items=V.vector.iterator();
while(items.hasNext())
{
Student s1=items.next();
out+=s1.getAll();
out+="\n";
}
JOptionPane.showMessageDialog(null,out);
}
}
//--------------------修改学生----------------------------
else if(input.equals("修改信息"))
{
Enumeration items=V.vector.elements();
String num1=JOptionPane.showInputDialog("你想修改的学生的名字是:");
if(V.vector.isEmpty())
JOptionPane.showMessageDialog(null,"系统中的学生为空");
else
{
while(items.hasMoreElements())
{
String op1=JOptionPane.showInputDialog("请选择下你想修改的内容\n1:名字\n2:学号\n3:性别\n4:出生年月日\n");
int op=Integer.parseInt(op1);
Student s1=(Student) items.nextElement();
if(s1.getName().equals(
num1))
{
//String nname34=JOptionPane.showInputDialog("更改名字为:");
if(op==1)
{
String nname=JOptionPane.showInputDialog("更改名字为:");
s1.setName(nname);
JOptionPane.showMessageDialog(null,"修改成功,玩得愉快哈");
}
else if(op==2)
{
String nnum1=JOptionPane.showInputDialog("更改学号为:");
int nnum=Integer.parseInt(nnum1);
s1.setNumber(nnum);
JOptionPane.showMessageDialog(null,"修改成功,玩得愉快哈");
}
else if(op==3)
{
String sex1=JOptionPane.showInputDialog("更改性别为:");
s1.setSex(sex1);
JOptionPane.showMessageDialog(null,"修改成功,玩得愉快哈");
}
else if(op==4)
{
String brith1=JOptionPane.showInputDialog("更改出生日期为:");
int brith=Integer.parseInt(brith1);
s1.setBrithdate(brith);
JOptionPane.showMessageDialog(null,"修改成功,玩得愉快哈");
}
break;
}
}
}
}
//--------------------删除学生----------------------------
else if(input.equals("删除学生"))
{
String name1=JOptionPane.showInputDialog("请输入你要删除的学生的名字:");
Enumeration items=V.vector.elements();
if(V.vector.isEmpty())
{
JOptionPane.showMessageDialog(null,"系统中的学生为空,没得改啦");
}
else
{
int flag=0;
while(items.hasMoreElements())
{
Student s1=(Student) items.nextElement();
if(s1.getName().equals(name1))
{
V.vector.remove(s1);
JOptionPane.showMessageDialog(null,"删除成功");
flag=1;
break;
}
}
if(flag==0)
JOptionPane.showMessageDialog(null,"系统中没有你想要删除的那个学生,再想想,是不是打错名字了");
}
}
else if(input.equals("统计学生人数"))
{
/*if(V.vector.isEmpty())
{
JOptionPane.showMessageDialog(null,"系统有0个学生");
}
else*/
{
String out="系统中总共的人数为:";
int size=V.vector.size();
out+=String.valueOf(size);
textdisplay.setText(out);
JOptionPane.showMessageDialog(null,out);
}
}
}
//---------------------主方法-------------------------------
public static void main(String []args)
{
Work7_8 A=new Work7_8();
}
}