1. 程式人生 > >學生資訊管理系統-GUI

學生資訊管理系統-GUI

本人java 初學者,使用了已經過時的GUI圖形使用者介面。

程式運了IO進行資料的讀寫,沒有涉及到資料庫的使用。

寫道最後發現有很多的缺點,面向物件的程式設計思想很多地方體現的不是很到位,還有頻繁的操作磁碟檔案。

也參考過許多網上許多大佬的方法。

算是給自己學java半年以來的一個說法吧。

最後將程式碼分享給大家,一起學習!

package MyStu;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


import javax.swing.*;


class Login extends JFrame implements ActionListener{




static ArrayList<Student> as = new ArrayList<Student>();

JButton b1,b2=null;
JPanel jp1,jp2,jp3=null;
JTextField jtf=null;
JLabel jlb1,jlb2=null;
JPasswordField pwf=null;
final String user="123";  
    final String psd="123";  

public Login(){
Image icon=Toolkit.getDefaultToolkit().getImage("E://1.gif");
this.setIconImage(icon);

b1=new JButton("登陸");

b1.setFont(new Font("華文中宋", Font.BOLD, 15));
b2=new JButton("清除");
b2.setFont(new Font("華文中宋", Font.BOLD, 15));
b1.setBorder(null);


b2.setBorder(null);
b1.addActionListener(this);
b2.addActionListener(this);

jp1=new JPanel();  
jp1.setBackground(Color.cyan);
    jp2=new JPanel();  
    jp2.setBackground(Color.YELLOW);
    jp3=new JPanel();  
    jp3.setBackground(Color.orange);
    
    jlb1=new JLabel("使用者名稱:");  
    jlb2=new JLabel("密    碼:");  
    jlb1.setFont(new Font("華文行楷", Font.BOLD, 20));
    jlb2.setFont(new Font("華文行楷", Font.BOLD, 20));
    
jtf=new JTextField(10);

pwf=new JPasswordField(10);

jp1.add(jlb1);
jp1.add(jtf);

jp2.add(jlb2);
jp2.add(pwf);

jp3.add(b1);
jp3.add(b2);

this.add(jp1);  
    this.add(jp2);  
    this.add(jp3);   
     
       this.setLayout(new GridLayout(3,1));  
        
     this.setTitle("學生資訊管理系統"); 
      
       this.setSize(300,200);  
        
       this.setLocation(200, 150);  
        
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
        
       this.setVisible(true);  
       this.setResizable(false);  
}
public void actionPerformed(ActionEvent e){

if(e.getActionCommand()=="登陸"){

stulogin();
}else if (e.getActionCommand()=="清除") {
clear();
}

}

                            


public void stulogin(){
if(user.equals(jtf.getText())&&psd.equals(pwf.getText())){
JOptionPane.showMessageDialog(null,"登入成功!","系統提示",JOptionPane.WARNING_MESSAGE);
clear();
 
dispose();
Menu1 ui=new Menu1();



}else if(jtf.getText().isEmpty()&&pwf.getText().isEmpty())  
        {  
            JOptionPane.showMessageDialog(null,"請輸入使用者名稱和密碼!","提示訊息",JOptionPane.WARNING_MESSAGE);  
        }else if(jtf.getText().isEmpty())  
        {  
            JOptionPane.showMessageDialog(null,"請輸入使用者名稱!","提示訊息",JOptionPane.WARNING_MESSAGE);  
        }else if(pwf.getText().isEmpty())  
        {  
            JOptionPane.showMessageDialog(null,"請輸入密碼!","提示訊息",JOptionPane.WARNING_MESSAGE);  
        }else  
        {  
            JOptionPane.showMessageDialog(null,"使用者名稱或者密碼錯誤!\n請重新輸入","提示訊息",JOptionPane.ERROR_MESSAGE);  
            //清空輸入框  
            clear();  
        }  

}

public void clear(){
jtf.setText("");
pwf.setText("");
}

}
//---------------------------------------------------------


//Memu功能選單欄
class Menu1 extends JFrame {
static ArrayList<Student> as = new ArrayList<Student>();
JButton jb1,jb2,jb3,jb4,jb5,jb6=null;  
 JPanel jp1,jp2,jp3=null;  
 JLabel jlb1=null; 
 public  Menu1(){
 
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
 
jb1=new JButton("新增資訊");
jb2=new JButton("刪除資訊");
jb3=new JButton("查詢資訊");
jb5=new JButton("檢視資訊");
jb6=new JButton("退出");
jb1.setFont(new Font("華文中宋", Font.BOLD, 18));
jb2.setFont(new Font("華文中宋", Font.BOLD, 18));
jb3.setFont(new Font("華文中宋", Font.BOLD, 18));
jb5.setFont(new Font("華文中宋", Font.BOLD, 18));
jb6.setFont(new Font("華文中宋", Font.BOLD, 18));
 
jb1.setBorder(null);
jb2.setBorder(null);
jb3.setBorder(null);


jb5.setBorder(null);
jb6.setBorder(null);
 
jlb1=new JLabel("歡迎使用學生資訊管理系統");
 jlb1.setHorizontalAlignment(SwingConstants.CENTER);
 jlb1.setFont(new Font("華文行楷", Font.BOLD, 40));
jp1. add(jlb1);
 
 
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
 


jp3.add(jb5);
jp3.add(jb6);
 
this.add(jp1);
this.add(jp2);
this.add(jp3);
 
this.setLayout(new GridLayout(3,5)); 
 
this.setTitle("學生資訊管理系統");  
 
this.setSize(500,400);  
       //設定窗體初始位置  
       this.setLocation(200, 150); 
       //設定當關閉視窗時,保證JVM也退出  
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
       //顯示窗體  
       this.setVisible(true);  
       this.setResizable(false);  
       jb1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="新增資訊"){
addinfo_Menu ai=new addinfo_Menu();
ArrayList<Student>as=new ArrayList<Student>();
ai.infor(as);

}
}
  });
       
       
       jb2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="刪除資訊"){
Delete_info di=new Delete_info();
di.delete_info();

}
}
       });
       
       jb3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="查詢資訊"){
scaninfo si=new scaninfo();
si.Scaninfo();
}
}
       
       
       });
       
      
       
       jb5.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="檢視資訊"){
readinfo ri=new readinfo();
ri.Readinfo();
}
}
       
       
       });
       
       jb6.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="退出"){
dispose();
Login lg=new Login();
}
}
       
       
       });
 }




public void addinfo(){

}

}








//主類--------------------------------------------------
public class Wenfan{
public   static void main(String[] args){
Login lg=new Login();



}
}




package MyStu;






import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;


import javax.swing.*;


 class addinfo_Menu extends JFrame implements ActionListener {
ArrayList<Student>as=new ArrayList<Student>();
JButton jb1,jb2,jb3,jb4=null;
JPanel  jp1,jp2,jp3,jp4,jp5,jp6,jp7=null;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5=null;
JLabel jlb1,jlb2,jlb3,jlb4,jlb5,jlb6=null;
 
public void infor(final ArrayList<Student>as){



jb1=new JButton("儲存");
jb2=new JButton("返回");
jb3=new JButton("繼續新增");
jb4=new JButton("清除檔案資料");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb1.setFont(new Font("華文中宋", Font.BOLD, 15));
jb2.setFont(new Font("華文中宋", Font.BOLD, 15));
jb3.setFont(new Font("華文中宋", Font.BOLD, 15));
jb4.setFont(new Font("華文中宋", Font.BOLD, 15));

jp1=new JPanel();  
    jp2=new JPanel();  
    jp3=new JPanel(); 
    jp4=new JPanel();
    jp5=new JPanel();
    jp6=new JPanel();
    jp7=new JPanel();
    jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp4.setBackground(Color.cyan);
jp5.setBackground(Color.cyan);
jp6.setBackground(Color.cyan);
jp7.setBackground(Color.cyan);


    
    
    
    jlb1=new JLabel("輸入學生資訊");  
    jlb1.setFont(new Font("華文行楷", Font.BOLD, 30));
    jlb2=new JLabel("姓名:");  
    jlb3=new JLabel("性別:");  
    jlb4=new JLabel("年齡:");  
    jlb5=new JLabel("學號:");  
    jlb6=new JLabel("成績:");  
    
    jlb1.setFont(new Font("華文行楷", Font.BOLD, 25));
    jlb2.setFont(new Font("華文行楷", Font.BOLD, 25));
    jlb3.setFont(new Font("華文行楷", Font.BOLD, 25));
    jlb4.setFont(new Font("華文行楷", Font.BOLD, 25));
    jlb5.setFont(new Font("華文行楷", Font.BOLD, 25));
    jlb6.setFont(new Font("華文行楷", Font.BOLD, 25));
    
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jtf4=new JTextField(10);
jtf5=new JTextField(10);



jp1.add(jlb1);

jp2.add(jlb2);
jp2.add(jtf1);

jp3.add(jlb3);
jp3.add(jtf2);

jp4.add(jlb4);
jp4.add(jtf3);

jp5.add(jlb5);
jp5.add(jtf4);

jp6.add(jlb6);
jp6.add(jtf5);

jp7.add(jb1);
jp7.add(jb3);
jp7.add(jb4);
jp7.add(jb2);

this.add(jp1);  
    this.add(jp2);  
    this.add(jp3);  
    this.add(jp4);  
    this.add(jp5);  
    this.add(jp6); 
    this.add(jp7);  
  
    //設定佈局管理器  
       this.setLayout(new GridLayout(7,1));  
       //給視窗設定標題
     this.setTitle("學生成績管理系統"); 
     //設定窗體大小  
       this.setSize(500,400);  
       //設定窗體初始位置  
       this.setLocation(200, 150);  
       //顯示窗體  
       this.setVisible(true);  
       this.setResizable(false);  
       
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

if(e.getActionCommand()=="儲存"){
try {
File stu_csv = new File("E://stu.txt");   
if(stu_csv.exists())    //
   stu_csv.delete();  
stu_csv.createNewFile();
   String stu_name,stu_gendar,stu_age,stu_number,stu_score;
   stu_name=jtf1.getText();
stu_gendar=jtf2.getText();
stu_age=jtf3.getText();
stu_number=jtf4.getText();
stu_score=jtf5.getText(); 
as.add(new Student(stu_name,stu_gendar,stu_age,stu_number,stu_score));
BufferedWriter bfw = new BufferedWriter(new FileWriter(stu_csv,true));
   //bfw.newLine();  
   for (Student stu : as) {  
   System.out.println(stu.name+","+stu.gendar+","+stu.age+","+stu.number+","+stu.score);
       bfw.write(stu.name+","+stu.gendar+","+stu.age+","+stu.number+","+stu.score+"\r\n");  
   }  
   bfw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}  
JOptionPane.showMessageDialog(null,"已儲存姓名為:"+jtf1.getText()+"的資訊","系統提示",JOptionPane.INFORMATION_MESSAGE); 
}else if(e.getActionCommand()=="繼續新增"){
reset();
}else if(e.getActionCommand()=="清除檔案資料"){
try {
File stu_csv = new File("E://stu.txt");  
if(stu_csv.exists())      
   stu_csv.delete();  
stu_csv.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}  

}else if(e.getActionCommand()=="返回"){
dispose();

}
}





public  void reset(){
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
jtf4.setText("");
}




}  


 
 class Student{
String  name;
String gendar;
String number;
String age;
String score;
public Student(String name,String gendar,String age,String number,String score){
this.name=name;
this.gendar=gendar;
this.age=age;
this.number=number;                      
this.score=score;
}
 

 
 }







package MyStu;


import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;


 class Delete_info extends JFrame implements ActionListener{
JButton jb1,jb2,jb3,jb4,jb5;
JLabel jlb1,jlb2,jlb3,jlb4;
JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
JTextField jtf1;
public void delete_info(){
jp1=new JPanel();
jp2=new JPanel();
jp7=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp7.setBackground(Color.cyan);


 
jb1=new JButton("按學號刪除");
jb2=new JButton("按姓名刪除");
jb5=new JButton("返回");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb5.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);
jb5.setBorder(null);
jb1.setFont(new Font("華文中宋", Font.BOLD, 20));
jb2.setFont(new Font("華文中宋", Font.BOLD, 20));
jb5.setFont(new Font("華文中宋", Font.BOLD, 15));


jlb1=new JLabel("刪除資訊");  
jlb1.setFont(new Font("華文行楷", Font.BOLD, 30));

     jp1.add(jlb1);
     
     jp2.add(jb1);
     jp2.add(jb2);
     
     jp7.add(jb5);
     
     this.add(jp1);
     this.add(jp2);
     this.add(jp7);
     
     this.setLayout(new GridLayout(3,1));
     this.setTitle("刪除");
     this.setSize(500,400);
     this.setLocation(200,150);
     this.setVisible(true);
     this.setResizable(false);


}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="按學號刪除"){
Delete_Number dn=new Delete_Number();
dn.delete_number();
}else if(e.getActionCommand()=="按姓名刪除"){
Delete_Name dne=new Delete_Name();
dne.delete_name();
}else if(e.getActionCommand()=="返回"){

dispose();
}
}




}
 
 class Delete_Number extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5;
JTextField jtf1;
JLabel jlb1,jlb2,jlb3;
ArrayList<Student3> as3 = new ArrayList<Student3>();
BufferedWriter bfw;
public void delete_number(){
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp4.setBackground(Color.cyan);
 
jb1=new JButton("刪除");
jb2=new JButton("返回");
jb1.addActionListener(this);
jb2.addActionListener(this);
 
jb1.setBorder(null);
jb2.setBorder(null);
jb1.setFont(new Font("華文中宋", Font.BOLD, 20));
jb2.setFont(new Font("華文中宋", Font.BOLD, 20));
 
 
jtf1=new JTextField(10);


jlb1=new JLabel("刪除資訊");  
jlb1.setFont(new Font("華文行楷", Font.BOLD, 30));

    
    jlb2=new JLabel("請輸入學號:");
    jlb2.setFont(new Font("華文行楷", Font.BOLD, 20));
    
    jp1.add(jlb1);
    
    jp2.add(jlb2);
    jp2.add(jtf1);
    jp2.add(jb1);
      
    jp3.add(jb2);
    
    this.add(jp1);
    this.add(jp2);
    this.add(jp3);


    
    this.setLayout(new GridLayout(3,1));
    this.setTitle("刪除");
    this.setSize(500,400);
    this.setLocation(200,150);
    this.setVisible(true);
    this.setResizable(false);
    
    try {
File csv = new File("E://stu.txt"); // CSV檔案  
BufferedReader br = new BufferedReader(new FileReader(csv)); 
// 讀取直到最後一行   
String line = "";
while ((line = br.readLine()) != null) {   
    // 把一行資料分割成多個欄位   
    StringTokenizer st1 = new StringTokenizer(line, ",");  
    Student3 st2= new Student3(st1.nextToken(),st1.nextToken(),st1.nextToken(),st1.nextToken(),st1.nextToken());  
    as3.add(st2);  
   
}   
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
boolean flag=false;
if(e.getActionCommand()=="刪除"){
if(jtf1.getText().isEmpty())  
       {  
           JOptionPane.showMessageDialog(null,"請輸入使用者名稱!","提示訊息",JOptionPane.WARNING_MESSAGE);  
       }
for(Student3 student :as3){
if(student.number.equals(jtf1.getText())){
as3.remove(student);
File txt = new File("E://stu.txt"); // CSV檔案  
txt.delete();  
try {
txt.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

flag=true;
break;
}
}


if(flag){
try {
File txt = new File("E://stu.txt");
bfw = new BufferedWriter(new FileWriter(txt,true));
for(Student3 student1 :as3){
bfw.write(student1.name+","+student1.gendar+","+student1.age+","+student1.number+","+student1.score+"\r\n");
System.out.println(student1.name+","+student1.gendar+","+student1.age+","+student1.number+","+student1.score);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null,"已刪除學號為:"+jtf1.getText()+"的學生資訊","系統提示",JOptionPane.INFORMATION_MESSAGE);


try {
bfw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
JOptionPane.showMessageDialog(null,"無學號為:"+jtf1.getText()+"的資訊!","提示訊息",JOptionPane.WARNING_MESSAGE);
}




}else if(e.getActionCommand()=="返回"){
dispose();
}

}
 
 
 
 }
 
 
 
 
 //--------------------------按姓名刪除選單----------------、
 
 
 class Delete_Name extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5;
JTextField jtf1;
JLabel jlb1,jlb2,jlb3;
ArrayList<Student3> as3 = new ArrayList<Student3>();
BufferedWriter bfw;
public void delete_name(){
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
 
jb1=new JButton("刪除");
jb2=new JButton("返回");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb1.setFont(new Font("華文中宋", Font.BOLD, 20));
jb2.setFont(new Font("華文中宋", Font.BOLD, 20));
 
 
jb1.setBorder(null);
jb2.setBorder(null);


 
jtf1=new JTextField(10);


jlb1=new JLabel("刪除資訊");  
    jlb1.setFont(new Font("華文行楷", Font.BOLD, 30));

    
    jlb2=new JLabel("請輸入姓名:");
    jlb2.setFont(new Font("華文行楷", Font.BOLD, 20));
    
    jp1.add(jlb1);
    
    jp2.add(jlb2);
    jp2.add(jtf1);
    jp2.add(jb1);
      
    jp3.add(jb2);
    
    this.add(jp1);
    this.add(jp2);
    this.add(jp3);


    
    this.setLayout(new GridLayout(3,1));
    this.setTitle("刪除");
    this.setSize(500,400);
    this.setLocation(200,150);
    this.setVisible(true);
    this.setResizable(false);
    
    try {
File csv = new File("E://stu.txt"); // CSV檔案  
BufferedReader br = new BufferedReader(new FileReader(csv)); 
// 讀取直到最後一行   
String line = "";
while ((line = br.readLine()) != null) {   
    // 把一行資料分割成多個欄位   
    StringTokenizer st1 = new StringTokenizer(line, ",");  
    Student3 st2= new Student3(st1.nextToken(),st1.nextToken(),st1.nextToken(),st1.nextToken(),st1.nextToken());  
    as3.add(st2);  
   
}   
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
boolean flag=false;
if(e.getActionCommand()=="刪除"){
if(jtf1.getText().isEmpty())  
       {  
           JOptionPane.showMessageDialog(null,"請輸入姓名!","提示訊息",JOptionPane.WARNING_MESSAGE);  
       }
for(Student3 student :as3){
if(student.name.equals(jtf1.getText())){
as3.remove(student);
File txt = new File("E://stu.txt"); // CSV檔案  
txt.delete();  
try {
txt.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jtf1.setText("");
flag=true;
break;
}
}


if(flag){
try {
File txt = new File("E://stu.txt");
bfw = new BufferedWriter(new FileWriter(txt,true));
for(Student3 student1 :as3){
bfw.write(student1.name+","+student1.gendar+","+student1.age+","+student1.number+","+student1.score+"\r\n");
System.out.println(student1.name+","+student1.gendar+","+student1.age+","+student1.number+","+student1.score);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null,"已刪除姓名為:"+jtf1.getText()+"的學生資訊","系統提示",JOptionPane.INFORMATION_MESSAGE);


try {
bfw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
JOptionPane.showMessageDialog(null,"無姓名為:"+jtf1.getText()+"的資訊!","提示訊息",JOptionPane.WARNING_MESSAGE);
}




}else if(e.getActionCommand()=="返回"){
dispose();
}

}
 
 
 
 }
 
 
 
 
 
 class Student3{
String  name;
String gendar;
String number;
String age;
String score;
public Student3(String name,String gendar,String age,String number,String score){
this.name=name;
this.gendar=gendar;
this.age=age;
this.number=number;                      
this.score=score;
}
}
 
 


 package MyStu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;


import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;








 class readinfo extends JFrame implements ActionListener {
JLabel jlb;
JButton jb1;
JPanel jp1,jp2;
public void Readinfo(){
jp1=new JPanel();
jp2=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);

jb1=new JButton("返回");
jb1.addActionListener(this);
jb1.setFont(new Font("華文中宋", Font.BOLD, 18));
jb1.setBorder(null);

jlb=new JLabel("所有資訊如下");
jlb.setFont(new Font("華文行楷", Font.BOLD, 30));

JTableHeader tableH; 
String col[]={"姓名","性別","年齡","學號","成績"};  
       final DefaultTableModel tableModel = new DefaultTableModel(col, 0);  
     JTable table = new JTable(tableModel);  
     table.setEnabled(false);  
       JScrollPane pane = new JScrollPane(table); 
       pane.setBackground(Color.cyan);
       
       jp1.add(jlb);
      jp2.add(jb1);

ArrayList<Student1> as1 = new ArrayList<Student1>();
try {
File csv = new File("E://stu.txt"); // CSV檔案  
 
BufferedReader br = new BufferedReader(new FileReader(csv));  


  
String line = "";   
while ((line = br.readLine()) != null) {   
       
    StringTokenizer st = new StringTokenizer(line, ",");  
    Student1 st1= new Student1(st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken());  
    as1.add(st1);  
   
}   
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
this.add(jp1);
this.add(pane);
this.add(jp2);
 

   this.setBackground(Color.cyan);
   this.setLayout(new GridLayout(3,1));
     this.setTitle("顯示資訊"); 
      
      this.setSize(500,400);  
        
      this.setLocation(200, 150);  
      this.setVisible(true); 
     this.setResizable(false);  
     
       table.setBackground(new Color(144,238,144));  
          table.setForeground(new Color(100,100,100)) ;  
          table.setGridColor(new Color(105 ,105, 105));  
          tableH = table.getTableHeader();  
        
          tableH.setBackground(new Color(200, 200, 200));  

          
          tableH.setForeground(new Color(0,0,205));  
       for (Student1 stu : as1) {  
           Object [] data ={stu.name,stu.gendar,stu.age,stu.number,stu.score};  
           tableModel.addRow(data);  
       }  
       tableModel.fireTableDataChanged();  
        RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);  
        table.setRowSorter(sorter);  
        this.getContentPane().add(pane, BorderLayout.CENTER);  
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="返回"){
dispose();
}
}  
 }

 
 class Student1{
String  name;
String gendar;
String number;
String age;
String score;
public Student1(String name,String gendar,String age,String number,String score){
this.name=name;
this.gendar=gendar;
this.age=age;
this.number=number;                      
this.score=score;
}
}
 
 
                 package MyStu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;


import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;










class scaninfo extends JFrame implements ActionListener {
JButton jb1,jb2,jb5;
JLabel jlb1,jlb2,jlb3,jlb4;
JPanel jp1,jp2,jp7;


public void  Scaninfo(){
jp1=new JPanel();
jp2=new JPanel();
jp7=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp7.setBackground(Color.cyan);
 
jb1=new JButton("按學號查詢");
jb2=new JButton("按姓名查詢");
jb5=new JButton("返回");
jb1.setFont(new Font("華文中宋", Font.BOLD, 20));
jb2.setFont(new Font("華文中宋", Font.BOLD, 20));
jb5.setFont(new Font("華文中宋", Font.BOLD, 15));
jb1.addActionListener(this);
jb2.addActionListener(this);
jb5.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);
jb5.setBorder(null);

jlb1=new JLabel("查詢資訊");  
jlb1.setFont(new Font("華文行楷", Font.BOLD, 30));

    jp1.add(jlb1);
    
    jp2.add(jb1);
    jp2.add(jb2);
    
    jp7.add(jb5);
    
    this.add(jp1);
    this.add(jp2);
    this.add(jp7);
    
    this.setLayout(new GridLayout(3,1));
    this.setTitle("查詢");
    this.setSize(500,400);
    this.setLocation(200,150);
    this.setVisible(true);
    this.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="按學號查詢"){
Lookup_number look_n=new Lookup_number();
look_n.lookup_number();
}else if(e.getActionCommand()=="按姓名查詢"){
Lookup_name look_ne=new Lookup_name();
look_ne.lookup_name();

}else if(e.getActionCommand()=="返回"){
dispose();
}

}
//---------------按學號查詢介面----------------
}


//---------------按學號查詢介面----------------
class Lookup_number extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5;
JTextField jtf1;
JLabel jlb1,jlb2,jlb3;
JTable jtb;
ArrayList<Student2> as2 = new ArrayList<Student2>();


String col[]={"姓名","性別","年齡","學號","成績"}; 
final DefaultTableModel tableModel = new DefaultTableModel(col, 0);
public void lookup_number(){

jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp4.setBackground(Color.cyan);
 
JTableHeader tableH; 
JTable table = new JTable(tableModel);  
 table.setEnabled(false);  
 table.setBackground(new Color(144,238,144));  
          table.setForeground(new Color(100,100,100)) ;  
          table.setGridColor(new Color(105 ,105, 105));  
          tableH = table.getTableHeader();  
       
          tableH.setBackground(new Color(200, 200, 200));  
          
      
          tableH.setForeground(new Color(0,0,205));  
JScrollPane pane = new JScrollPane(table); 

 
jb1=new JButton("查詢");
jb2=new JButton("返回");
jb1.setFont(new Font("華文中宋", Font.BOLD, 18));
jb2.setFont(new Font("華文中宋", Font.BOLD, 18));
jb1.addActionListener(this);
jb2.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);

jtf1=new JTextField(10);

jlb1=new JLabel("按學號查詢資訊");
jlb1.setFont(new Font("華文行楷", Font.BOLD, 30));

jlb2=new JLabel("請輸入學號:");
jlb2.setFont(new Font("華文行楷", Font.BOLD, 20));
jlb3=new JLabel("查詢資訊如下");
jlb2.setFont(new Font("華文行楷", Font.BOLD, 25));

jp1.add(jlb1);

jp2.add(jlb2);
jp2.add(jtf1);
jp2.add(jb1);

jp3.add(jlb3);
jp4.add(jb2);

this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(pane);
this.add(jp4);




this.setLayout(new GridLayout(5,1));  
       //給視窗設定標題
     this.setTitle("查詢結果"); 
     //設定窗體大小  
       this.setSize(500,400);  
       //設定窗體初始位置  
       this.setLocation(200, 150);           
       //顯示窗體  
       this.setVisible(true);  
       this.setResizable(false);  
       
       try {
File csv = new File("E://stu.txt"); // CSV檔案  
BufferedReader br = new BufferedReader(new FileReader(csv)); 
// 讀取直到最後一行   
String line = "";
while ((line = br.readLine()) != null) {   
    // 把一行資料分割成多個欄位   
    StringTokenizer st = new StringTokenizer(line, ",");  
    Student2 st1= new Student2(st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken());  
    as2.add(st1);  
   
}   
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
       

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="查詢"){
boolean flag=true;
tableModel.setRowCount(0); // 之前的table一定要清零!  
    
             for (Student2 student : as2) {  
               
                 System.out.println(student.name+student.gendar+student.age+student.number+student.score);  
                 if(student.number.equals(jtf1.getText())){
                 Object [] data1 ={student.name,student.gendar,student.age,student.number,student.score};  
                 tableModel.addRow(data1);  
                 flag=false;
                 JOptionPane.showMessageDialog(null,"已查詢到學號為:"+jtf1.getText()+"的學生資訊","系統提示",JOptionPane.INFORMATION_MESSAGE);
                 }
                 
             }  
             if(flag){
            JOptionPane.showMessageDialog(null,"無學號為:"+jtf1.getText()+"的學生資訊","系統提示",JOptionPane.INFORMATION_MESSAGE);
             }
}else if(e.getActionCommand()=="返回"){
dispose();

}
}


}




//------------------按姓名查詢介面------------------------


class Lookup_name extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5;
JTextField jtf1;
JLabel jlb1,jlb2,jlb3;
JTable jtb;
ArrayList<Student2> as2 = new ArrayList<Student2>();


String col[]={"姓名","性別","年齡","學號","成績"}; 
final DefaultTableModel tableModel = new DefaultTableModel(col, 0);
public void lookup_name(){

jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp4.setBackground(Color.cyan);
JTableHeader tableH; 
JTable table = new JTable(tableModel);  
 table.setEnabled(false);  

 
 table.setEnabled(false);  
 table.setBackground(new Color(144,238,144));  
          table.setForeground(new Color(100,100,100)) ;  
          table.setGridColor(new Color(105 ,105, 105));  
          tableH = table.getTableHeader();  
          //設定表頭的背景色  
          tableH.setBackground(new Color(200, 200, 200));  
          //設定表頭的文字顏色  
          
          tableH.setForeground(new Color(0,0,205));  
JScrollPane pane = new JScrollPane(table); 
 
jb1=new JButton("查詢");
jb2=new JButton("返回");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);

jtf1=new JTextField(10);

jlb1=new JLabel("按姓名查詢資訊");
jlb1.setFont(new Font("華文行楷", Font.BOLD, 30));

jlb2=new JLabel("請輸入姓名:");
jlb2.setFont(new Font("華文行楷", Font.BOLD, 20));
jlb3=new JLabel("查詢資訊如下");
jlb3.setFont(new Font("華文行楷", Font.BOLD, 25));
jp1.add(jlb1);

jp2.add(jlb2);
jp2.add(jtf1);
jp2.add(jb1);

jp3.add(jlb3);
jp4.add(jb2);

this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(pane);
this.add(jp4);




this.setLayout(new GridLayout(5,1));  
        
     this.setTitle("查詢結果"); 
    
       this.setSize(500,400);  
       
       this.setLocation(200, 150);           
     
       this.setVisible(true);  
       this.setResizable(false);  
       
       try {
File txt = new File("E://stu.txt");   
BufferedReader br = new BufferedReader(new FileReader(txt)); 
  
String line = "";
while ((line = br.readLine()) != null) {   
  
    StringTokenizer st = new StringTokenizer(line, ",");  
    Student2 st1= new Student2(st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken());  
    as2.add(st1);  
   
}   
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
       

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="查詢"){

tableModel.setRowCount(0);   
    boolean flag=true;
             for (Student2 student : as2) {  
               
                 System.out.println(student.name+student.gendar+student.age+student.number+student.score);  
                 if(student.name.equals(jtf1.getText())){
                 Object [] data1 ={student.name,student.gendar,student.age,student.number,student.score};  
                 tableModel.addRow(data1);  
                 JOptionPane.showMessageDialog(null,"已查詢到姓名為:"+jtf1.getText()+"的學生資訊","系統提示",JOptionPane.INFORMATION_MESSAGE);
                 flag=false;
                 }
                
             }  
             if(flag){
            JOptionPane.showMessageDialog(null,"無姓名為:"+jtf1.getText()+"的學生資訊","系統提示",JOptionPane.INFORMATION_MESSAGE);
             }
            
}else if(e.getActionCommand()=="返回"){
dispose();

}
}


}








class Student2{
String  name;
String gendar;
String number;
String age;
String score;
public Student2(String name,String gendar,String age,String number,String score){
this.name=name;
this.gendar=gendar;
this.age=age;
this.number=number;                      
this.score=score;
}
}


相關推薦

學生資訊管理系統-GUI

本人java 初學者,使用了已經過時的GUI圖形使用者介面。 程式運了IO進行資料的讀寫,沒有涉及到資料庫的使用。 寫道最後發現有很多的缺點,面向物件的程式設計思想很多地方體現的不是很到位,還有頻繁的操作磁碟檔案。 也參考過許多網上許多大佬的方法。 算是給自己學java半年

GUI JDBC 學生資訊管理系統

1.Driver.java public class Driver { public static void main(String[] args) { Denglu dl = new Denglu(); dl.view(); } } 2.Denglu.java publi

學生資訊管理系統GUI

資料庫的內容:GUI介面:程式碼:StudentManagerment.javapackage 學生基本資訊; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.Actio

Android核心技術-day05-03-學生資訊管理系統小練習

package com.gaozewen.studentsystem.db; import android.content.ContentValues; import android.content.Context; import android.database.Curso

程式閱讀 簡單C 學生資訊管理系統

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

學生資訊管理系統資料庫課程設計

一.概述 學生資訊管理系統是學校管理的重要工具,是學校不可或缺的一部分。隨著在校人數的不斷增加,教務系統的數量也不斷的上漲。學校工作繁雜,資料眾多,人工管理資訊的難度也越來越大,顯然是不能滿足實際的需要,效率也是很低的。並且這種傳統的方式存在著眾多的弊端,如:保密性差.查詢不便.效率低,很難維

一個Python編寫的小程式(學生資訊管理系統

def printInfo(StudentData,StudentName): """ :param StudentData: :param StudentName: :return: """ student = StudentData[Stude

C++課設案例 學生資訊管理系統

#include <iostream> #include <string> #include <windows.h> #include <iostream> #include <fstream> #include <conio.h&g

c#簡易學生資訊管理系統

在近期的學習中,我們學習了泛型及泛型集合的概念和使用,泛型是c#中的一個重要概念,為了鞏固我們學習的成果,我們可以使用一個例項來進行練習 題目及要求 要求使用Windows窗體應用程式,製作出如上圖的介面,並實現增刪改查的功能 StuInfo類的編寫 同往常一樣,在編寫窗體的程式碼前,我們需要先編

學生資訊管理系統總結(三)——優化篇

enter,esc鍵設定 確定按鈕屬性default------→true 取消按鈕屬性cancel------→true 窗體中心位置展現 首先將窗體介面通過滑鼠拖拽,達到介面大小適中 接著檢視屬性中的height,width屬性數值,將該數值填入下面程式碼中 from中新增以

學生資訊管理系統總結(二)

限制文字框輸入的必須是數字 Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < 48 Or KeyAscii > 57) Then keyAscii = 0 End

學生資訊管理系統總結(一)

Option Explicit ——強制變數宣告,若不宣告,則會顯示為variant(變體)型別,即未被宣告變數數 據型別。 Count ——用於計數 ADODB ——動態資料鏈接物件(Active Data Objects Data Base),它是一種 PHP 存取資

學生資訊管理系統:實現對學生資訊增刪改查操作

                                    小白成長記,不喜勿噴,請多多指教 &nb

java集合類實現簡單的學生資訊管理系統

package jihe; import java.util.Scanner; public class Student { private String sno; private String sname; private int grade; private int age; private S

Java——Web開發之簡單的學生資訊管理系統

這僅僅是一個跟著視訊學習的小系統,大牛就繞道啦~ 系統實現的功能: 與mysql資料庫連線,實現登陸功能 管理員登陸成功後能檢視學生資訊 擴充套件功能(完成刪除與更新學生資訊的操作) 系統實現過程: 寫一個簡單的登陸

Java——Web開發之MVC設計模式的學生資訊管理系統(二)

為什麼這個標題為“(二)”,其實是對於上一個特別簡單學生資訊管理系統裡功能的完善。 所謂的“(一)”在這:學生資訊管理系統(一) 系統實現的功能: 實現新增學生 顯示查詢到的學生 刪除學生 更新學生資訊 模糊查詢符合相關資訊的學生

基於express的學生資訊管理系統

基於express的學生資訊管理系統 專案地址 專案簡介 簡單的學生資訊管理系統,後臺基於express,資料庫mysql,前端用到了一點vue,這次的團隊專案我主要是負責後端的部分,所以這裡對於前端程式碼不再贅述,只介紹後端部分以及一點點前端的js程式碼。 後端部分 //server.js var

JSP牛刀小試:學生資訊管理系統

    小弟這學期的JSP課程上完了,課程作業是學生資訊管理系統,在開發過程遇到了許多問題,記錄下來,作為自己學習的總結,也可以給大家作為參考。如果有錯誤的地方,多多包涵。 github地址如下: https://github.com/XiaoZhong233/Stude

學生資訊管理系統錯誤總結

學生在暑假的時候就已經結束了,自己悄悄的進行了資料庫視訊,開學兩週後才開始驗專案,過了兩週,發現自己通過看資料庫視訊,其實在學生有的地方是可以用的到的,從而鞏固了自己之前的知識。因為資料庫視訊裡面講解了sql 語句,而學生裡面也有sql語句,先自己操作,最後在看

做完學生資訊管理系統之後的感想

專案引數        頁面:主要由bootstrap框架、layer彈框外掛、jquery以及jstl標準標籤庫組成        後臺:過濾器、javaBean小工具、mysql架包、加密架包、連線池、封裝好的 分頁工具以及封裝好的jdbc增刪查改工具包 技術引數