1. 程式人生 > >Java版本 學生管理系統

Java版本 學生管理系統

package BookDemo_1;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Test {
    public static void main(String[] args) {
		StudentSys stuSys=new StudentSys("學生管理系統");
		stuSys.initWin();
    }  
}
class StudentSys extends JFrame{
	
	private JPanel p1,p2,p3,combop;
	private JTabbedPane tab;
	private Container container;
	private JButton b1,b2;
	private Listener listener;
	private Label nameLabel;
	private Label gradeLabel;
	private Label showLabel;
	private JTextField textName;
	private JTextField textGrade;
	private TextArea showGradeArea;
	/*
	* 查詢
	* */
	private Label searchLabel;
	private JTextField searchText;
	private JButton sBut;
	private JTextField resultText;
	private String[] name;
	private String[] grade;
	
	/*
	* 排序
	* */
	private TextArea showTextArea;
	private JButton sortBut;
	private int countNum=0;
	private JButton clearBut;
	public StudentSys(String str){
		super(str);
		
		this.name=new String[100];
		this.grade=new String[100];
		listener = new Listener();
		tab = new JTabbedPane(JTabbedPane.TOP); 
		//容器
		container = this.getLayeredPane();
		//物件化面板
		combop = new JPanel();
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		
		b1 =new JButton("確認新增");
		b2 =new JButton("撤回新增");
		nameLabel =new Label("姓名");
		gradeLabel =new Label("成績");
		
		showLabel=new Label("當前記錄為零!                          ");
		
		textName =new JTextField(15);
		textGrade =new JTextField(15);
		showGradeArea=new TextArea();
		
		/*
		* 查詢
		* */
		searchLabel=new Label("請輸入姓名:");
		searchText=new JTextField(15);
		sBut=new JButton("確認查詢");
		resultText=new JTextField(15);
		/*
		* 排序
		* */
		showTextArea=new TextArea();
		sortBut=new JButton("成績排序");
		clearBut=new JButton("清空資料");
	}
	public void initWin(){
		this.setBounds(300, 300, 500, 400);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				System.exit(0);
			}});
			layoutWin();
			this.setVisible(true);
	}
	private void layoutWin(){
		
		tab.add(p1,"成績輸入");
		tab.add(p2,"成績查詢");
		tab.add(p3,"成績排序");
		combop.add(new JLabel("學生資訊管理系統"));
		container.setLayout(new BorderLayout());
		container.add(combop,BorderLayout.NORTH);
		container.add(tab,BorderLayout.CENTER);
		
		Container con1=new Container();
		con1.setLayout(new FlowLayout());
		con1.add(nameLabel);
		con1.add(textName);
		
		con1.add(gradeLabel);
		con1.add(textGrade);
		p1.add(con1,BorderLayout.NORTH);
		p1.add(con1);
		p1.add(showGradeArea);
		
		Container con2=new Container();
		con2.setLayout(new FlowLayout());
		con2.add(b1);
		con2.add(b2);
		con2.add(showLabel);
		p1.add(con2);
		b1.addActionListener(listener);
		b2.addActionListener(listener);
		/*
		* 查詢佈局
		* */
		
		Container con3=new Container();
		con3.setLayout(new FlowLayout());
		con3.add(searchLabel);
		con3.add(searchText);
		con3.add(sBut);
		p2.add(con3,BorderLayout.NORTH);
		sBut.addActionListener(listener);
		p2.add(resultText);
		/*
		* 排序佈局
		* */
		p3.add(showTextArea);
		p3.add(sortBut);
		p3.add(clearBut);
		sortBut.addActionListener(listener);
		clearBut.addActionListener(listener);
	}
	/*
	* java內部類實現ActionListener介面
	* */
	class Listener implements ActionListener{
		@Override
			public void actionPerformed(ActionEvent e) {
			
			if(e.getSource()==b1){
				
				if((textName.getText().equals(""))||(textGrade.getText().equals(""))){
					showLabel.setText("新增失敗(姓名,成績不能有空)!");
				}
				else{
					name[countNum]=textName.getText();
					grade[countNum]=textGrade.getText();
					countNum++;
					String area="新增成功,當前有"+countNum+"條記錄";
					showLabel.setText(area);
					sortMess(false);
					textName.setText("");
					textGrade.setText("");
				}
				
			}
			if(e.getSource()==b2){
				if(countNum>0){
					countNum--;
					String area="撤回成功,當前有"+countNum+"條記錄";
					showLabel.setText(area);
					sortMess(false);
				}
			}
			if(e.getSource()==sBut){
				if(!searchText.getText().equals("")){
					searchMess(searchText.getText());
				}
			}
			if(e.getSource()==sortBut){
				sortMess(true);
			}
			if(e.getSource()==clearBut){
				if(!showTextArea.getText().equals("")){
					showTextArea.setText("");
				 }
			}
		}
		
		public void sortMess(boolean sign) {
			// TODO Auto-generated method stub
			if(sign){
				for(int i=0;i<countNum;i++){
					for(int j=i+1;j<countNum;j++){
						if(Integer.parseInt(grade[i])<Integer.parseInt(grade[j])){
							String s1,s2;
							s1=name[i];
							s2=grade[i];
							
							name[i]=name[j];
							grade[i]=grade[j];
							
							name[j]=s1;
							grade[j]=s2;
						}
					}
				}
			}else{ 
			
				if(!showGradeArea.getText().equals("")){
					showGradeArea.setText("");
				}
			}
			for(int i=0;i<countNum;i++){
				String content="姓名:"+name[i]+"\t"+"成績"+grade[i];
				if(sign)showTextArea.append(content+"\n");
				else showGradeArea.append(content+"\n");
			}
		}
		
		public void searchMess(String n) {
			// TODO Auto-generated method stub
			
			for(int i=0;i<countNum;i++){
				if(name[i].equals(n)){
					String content="姓名:"+name[i]+","+"成績"+grade[i];
					resultText.setText(content);
					return;
				}
			}
			resultText.setText("未找到該學生!");
		}
	}
}