Java 可视学生成绩管理系统

systemv.java代码如下:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Student
{
	private long x;
	private String n;
	private double s1,s2;
	public Student(long x,String n,double s1,double s2)
	{
		this.x=x;
		this.n=n;
		this.s1=s1;
		this.s2=s2;
	}
	public long get_x()
	{
		return x;
	}
	public void set_x(long x)
	{
		this.x=x;
	}
	public String get_n()
	{
		return n;
	}
	public void set_n(String n)
	{
		this.n=n;
	}
	public double get_s1()
	{
		return s1;
	}
	public void set_s1(double s1)
	{
		this.s1=s1;
	}
	public double get_s2()
	{
		return s2;
	}
	public void set_s2(double s2)
	{
		this.s2=s2;
	}
	public String toString()
	{
		return "[ 'number':"+x+" 'name':"+n+" 'score1':"+s1+" 'score2':"+s2+" ]";
	}
}
public class systemv extends JFrame implements ActionListener
{
	static java.util.List<Student> l=new ArrayList<>();
	static JFrame f=new JFrame();
	static String[] h={"创建学生信息",
		"查找某学号的学生信息",
		"查找某姓名的学生信息",
		"删除某学号的学生信息",
		"插入新的学生信息",
		"显示所有学生的个人信息",
		"更改学生个人信息"};
	public systemv()
	{
		JLabel la=new JLabel("welcome to [JIT学生成绩管理系统]!!!");
		f.add(la,BorderLayout.NORTH);
		JPanel p=new JPanel(new GridLayout(7,1));
		JButton[] b=new JButton[7];
		for(int i=0;i<7;i++)
		{
			b[i]=new JButton(h[i]);
			b[i].addActionListener(this);
			p.add(b[i]);
		}
		f.add(p);
		f.pack();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
	public void actionPerformed(ActionEvent e)
	{
		String g=e.getActionCommand();
		if(g.equals(h[0]))
		{
			if(l.isEmpty())
			{
				create();
				show_all();
			}
			else
				already();
		}
		if(g.equals(h[1]))
		{
			if(l.isEmpty())
				none();
			else
				search_x();
		}
		if(g.equals(h[2]))
		{
			if(l.isEmpty())
				none();
			else
				search_n();
		}
		if(g.equals(h[3]))
		{
			if(l.isEmpty())
				none_r();
			else
			{
				remove();
				if(l.isEmpty())
					r_e();
				else
					show_all();
			}
		}
		if(g.equals(h[4]))
		{
			add();
			show_all();
		}
		if(g.equals(h[5]))
		{
			if(l.isEmpty())
				none();
			else
				show_all();
		}
		if(g.equals(h[6]))
		{
			if(l.isEmpty())
				none_m();
			else
			{
				modify();
				show_all();
			}
		}
	}
	static void already()
	{
		JDialog d=new JDialog(f,"Error",true);
		JLabel jl=new JLabel("已有学生信息");
		d.add(jl);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void none()
	{
		JDialog d=new JDialog(f,"Error",true);//模态对话框
		JLabel jl=new JLabel("没有学生的信息可显示");
		d.add(jl);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void none_s()
	{
		JDialog d=new JDialog(f,"Error",true);
		JLabel jl=new JLabel("没有学生的信息可查询");
		d.add(jl);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void none_r()
	{
		JDialog d=new JDialog(f,"Error",true);
		JLabel jl=new JLabel("没有学生的信息可删除");
		d.add(jl);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void none_m()
	{
		JDialog d=new JDialog(f,"Error",true);
		JLabel jl=new JLabel("没有学生的信息可修改");
		d.add(jl);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void r_e()
	{
		JDialog d=new JDialog(f,"Error",true);
		JLabel jl=new JLabel("学生信息已删空");
		d.add(jl);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static boolean ce=true;
	static void create()
	{
		while(ce)
			create_one();
	}
	static void create_one()	
	{ 
		JDialog d=new JDialog(f,"创建学生信息",true);
		JTextField[] tf=new JTextField[4];
		JLabel[] la=new JLabel[4];
	    	JLabel jl=new JLabel("请输入:[学号] [姓名] [分数1] [分数2]");
		d.setSize(300,200);
		JPanel jp=new JPanel(new GridLayout(4,2));
		String[] lb={"学号:","姓名:","分数1:","分数2:"};
		for(int i=0;i<4;i++)
		{
			la[i]=new JLabel(lb[i]);
			tf[i]=new JTextField(8);
			jp.add(la[i]);
			jp.add(tf[i]);			
		}
		JButton jb=new JButton("确认输入");
		jb.addActionListener(new ActionListener()
				{   
			       		public void actionPerformed(ActionEvent e) 
					{
						long x=Long.parseLong(tf[0].getText());
						String n=tf[1].getText();
						double s1=Double.parseDouble(tf[2].getText());
						double s2=Double.parseDouble(tf[3].getText());
						Student b=new Student(x,n,s1,s2);
						l.add(b);
						d.dispose();
			     		}
				});
		JButton jb1=new JButton("结束输入");
		jb1.addActionListener(new ActionListener()
				{   
			       		public void actionPerformed(ActionEvent e) 
					{
						ce=false;
						d.dispose();
			     		}
				});
	  	d.add(jp);
		JPanel p=new JPanel();
		p.add(jb);
		p.add(jb1);
		d.add(p,BorderLayout.SOUTH);
		d.setVisible(true);
     	}
	static void show_all()
	{
		JDialog d=new JDialog(f,"显示所有学生的个人信息",true);
		JTextArea ta=new JTextArea(l.size()+1,20);
		ta.setLineWrap(true);
		ta.setText("所有学生的信息如下:");
		for(int i=0;i<l.size();i++)
		{
			Student c=l.get(i);
			ta.append("\n"+c.toString());
		}
		d.add(ta);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void search_x()
	{
		JDialog d=new JDialog(f,"查找某学号的学生信息",true);
	    	JLabel jl=new JLabel("请输入要查询的学生信息的学生学号:");
	    	JLabel jl1=new JLabel("number: ");
		JTextField tf=new JTextField(10);
		tf.setText(null);
		JTextArea ta=new JTextArea(2,40);
		JButton jb=new JButton("确认查询");
		jb.addActionListener(new ActionListener()
				{   
			       		public void actionPerformed(ActionEvent e) 
					{
						long x=Long.parseLong(tf.getText());
						boolean m=true;
						for(int i=0;i<l.size();i++)
						{
							Student c=l.get(i);
							if(c.get_x()==x)
							{
								m=false;
								ta.setText("您所查询的学生信息如下:");
								ta.append("\n"+c.toString());
								break;
							}
						}
						if(m)
							ta.setText("Error! No such student !");
					}
				});
		JPanel p=new JPanel();
		p.add(jl);
		p.add(tf);
		d.add(jl,BorderLayout.NORTH);
		d.add(p,BorderLayout.WEST);
		d.add(jb,BorderLayout.EAST);
		d.add(ta,BorderLayout.SOUTH);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void search_n()
	{
		JDialog d=new JDialog(f,"查找某姓名的学生信息",true);
	    	JLabel jl=new JLabel("请输入要查询的学生信息的学生姓名:");
	    	JLabel jl1=new JLabel("name: ");
		JTextField tf=new JTextField(10);
		JTextArea ta=new JTextArea(2,40);
		JButton jb=new JButton("确认查询");
		jb.addActionListener(new ActionListener()
				{   
			       		public void actionPerformed(ActionEvent e) 
					{
						String n=tf.getText();
						boolean m=true;
						for(int i=0;i<l.size();i++)
						{
							Student c=l.get(i);
							if(n.equals(c.get_n()))
							{
								m=false;
								ta.setText("您所查询的学生信息如下:");
								ta.append("\n"+c.toString());
								break;
							}
						}
						if(m)
							ta.setText("Error! No such student !");
					}
				});
		JPanel p=new JPanel();
		p.add(jl1);
		p.add(tf);
		d.add(jl,BorderLayout.NORTH);
		d.add(p,BorderLayout.WEST);
		d.add(jb,BorderLayout.EAST);
		d.add(ta,BorderLayout.SOUTH);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void remove()
	{
		JDialog d=new JDialog(f,"删除学号的学生信息",true);
	    	JLabel jl=new JLabel("请输入要删除的学生信息的学生学号:");
	    	JLabel jl1=new JLabel("number: ");
		JTextField tf=new JTextField(10);
		JTextArea ta=new JTextArea(2,40);
		JButton jb=new JButton("确认删除");
		jb.addActionListener(new ActionListener()
				{   
					public void actionPerformed(ActionEvent e) 
					{
						long x=Long.parseLong(tf.getText());
						boolean m=true;
						int j;
						for(int i=0;i<l.size();i++)
						{
							Student c=l.get(i);
							if(c.get_x()==x)
							{
								m=false;
								ta.setText("您所删除的学生信息如下:");
								ta.append("\n"+c.toString());
								j=i;
								l.remove(j);
								break;
							}
						}
						if(m)
							ta.setText("Error! No such student !");
					}
				});				
		JPanel p=new JPanel();
		p.add(jl1);
		p.add(tf);
		d.add(jl,BorderLayout.NORTH);
		d.add(p,BorderLayout.WEST);
		d.add(jb,BorderLayout.EAST);
		d.add(ta,BorderLayout.SOUTH);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void add() 
	{
		JDialog d=new JDialog(f,"插入新的学生信息",true);
		d.setSize(300,200);
		JTextField[] tf=new JTextField[4];
		JLabel[] la=new JLabel[4];
	    	JLabel jl=new JLabel("请输入要插入的学生信息的学生[学号] [姓名] [分数1] [分数2] :");
		JPanel jp=new JPanel(new GridLayout(4,2));
		String[] lb={"学号:","姓名:","分数1:","分数2:"};
		for(int i=0;i<4;i++)
		{
			la[i]=new JLabel(lb[i]);
			tf[i]=new JTextField(8);
			jp.add(la[i]);
			jp.add(tf[i]);			
		}
		JButton jb=new JButton("确认插入");
		jb.addActionListener(new ActionListener()
				{   
			       		public void actionPerformed(ActionEvent e) 
					{
						boolean m=true;
						long x=Long.parseLong(tf[0].getText());
						for(int i=0;i<l.size();i++)
						{
							Student c=l.get(i);
							if(x==c.get_x())
							{
								m=false;
								JDialog d1=new JDialog(d,"Error",true);
								JLabel jl1=new JLabel("这个学号已经存在了!!!");
								d1.add(jl1);
								d.pack();
								d1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
								d1.setVisible(true);
								break;
							}
						}
						if(m)
						{
							String n=tf[1].getText();
							double s1=Double.parseDouble(tf[2].getText());
							double s2=Double.parseDouble(tf[3].getText());
							Student b=new Student(x,n,s1,s2);
							l.add(b);
							d.dispose();
						}
					}
				});
	  	d.add(jp);
		d.add(jb,BorderLayout.SOUTH);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	static void modify()
	{
		JDialog d=new JDialog(f,"更改某学号的学生信息",true);
		boolean m=true;
	    	JLabel jl=new JLabel("请输入要更改的学生信息的学生学号:");
	    	JLabel jl1=new JLabel("number: ");
		JTextField tf=new JTextField(10);
		JTextArea ta=new JTextArea(2,20);
		JTextField tf1=new JTextField(20);
		tf1.setVisible(false);
		JButton jb1=new JButton("确认更改");
		jb1.setVisible(false);
		JButton jb=new JButton("确认查询");
		jb.addActionListener(new ActionListener()
				{   
			       		public void actionPerformed(ActionEvent e) 
					{
						long x=Long.parseLong(tf.getText());
						boolean m=true;
						for(int i=0;i<l.size();i++)
						{
							Student c=l.get(i);
							if(c.get_x()==x)
							{
								m=false;
								ta.setText(c.toString()+"\n");
								ta.append("请输入修改后的学生信息的学生[学号] [姓名] [分数1] [分数2] :");
								jb1.setVisible(true);
								tf1.setVisible(true);
								jb1.addActionListener(new ActionListener()
										{   
											public void actionPerformed(ActionEvent e) 
											{
												String[] k=tf1.getText().split(" ");
												c.set_x(Long.parseLong(k[0]));
												c.set_n(k[1]);
												c.set_s1(Double.parseDouble(k[2]));
												c.set_s2(Double.parseDouble(k[3]));
												d.dispose();
											}
										});
								break;	
							}
						}
						if(m)
							ta.setText("Error! No such student !");
					}
				});				
		JPanel p=new JPanel();
		p.add(jl1);
		p.add(tf);
		d.add(p,BorderLayout.WEST);
		d.add(jl,BorderLayout.NORTH);
		d.add(jb,BorderLayout.EAST);
		JPanel p1=new JPanel(new BorderLayout());
		p1.add(ta,BorderLayout.NORTH);
		p1.add(tf1,BorderLayout.WEST);
		p1.add(jb1,BorderLayout.EAST);
		d.add(p1,BorderLayout.SOUTH);
		d.pack();
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		d.setVisible(true);
	}
	public static void main(String[] args)
	{
		new systemv();
	}
}

你可能感兴趣的:(java)