JAVA期末大做業之學生信息管理簡潔版系統

1、界面化登陸窗體,實現主要的界面佈局

  1 //package newpackage;
  2 import javax.swing.*;
  3 import java.awt.*;
  4 import java.awt.event.*;
  5 import java.util.Vector;
  6  
  7 public class StudentFrame extends JFrame {    //建立一個登陸界面
  8       Vector<Student> v = new Vector<Student>();           //建立一個動態數組的對象
  9      // Student student;         
 10     StudentLogin student = new StudentLogin();     //建立一個atm對象
 11     int num;
 12     String num1;    
 13     Container pane;          //content pane是container的一個對象
 14     CardLayout cc = new CardLayout();    //卡片佈局
 15     JLabel labTop = new JLabel("學生信息管理系統");     //建立了一個標籤寫着歡迎你
 16     JLabel labTop2 = new JLabel("  "); //建立了另一個空標籤
 17     JLabel labTop3 = new JLabel("  "); //建立了另一個空標籤
 18     JLabel labTop4 = new JLabel("  "); //建立了另一個空標籤
 19     JLabel labTop5 = new JLabel("  "); //建立了另一個空標籤
 20      
 21     JTextField txtName = new JTextField(15);           //名字輸入控制在15個字符之內
 22     JPasswordField txtPassword = new JPasswordField(15);   //密碼輸入控制在15個字符之內
 23     JButton butOK = new JButton("  登陸  ");                 //建立一個OK按鈕
 24     JButton butexit = new JButton("  退出  ");   
 25     public StudentFrame() {                                  //登陸窗體標題框內容
 26         super("用戶登陸");
 27         initalInterface();
 28     }   
 29     private void initalInterface() {    //初始化設置       
 30         //設置登陸按鈕的字體
 31         Font font = new Font("Dialog", Font.BOLD, 16);
 32         butOK.setFont(font);
 33        //設置退出按鈕的字體
 34         Font font2 = new Font("Dialog", Font.BOLD, 16);
 35         butexit.setFont(font2);        
 36        //設置"學生信息管理系統"的字體
 37         Font font1 = new Font("Dialog", Font.BOLD, 30);
 38         labTop.setFont(font1);              
 39         //開始構建菜單
 40         //******************************************************
 41         JMenuBar menuBar;
 42         JMenu menu;
 43         JMenuItem menuItem;      
 44         //建立菜單條
 45         menuBar = new JMenuBar();
 46         this.setJMenuBar(menuBar);       
 47         //建立File菜單
 48         menu = new JMenu("文件");
 49         menuBar.add(menu);
 50         menuItem=new JMenuItem("打開");
 51         menu.add(menuItem);
 52         menu.addSeparator();
 53         menuItem = new JMenuItem("退出");     
 54         //給菜單項Exit添加活動事件處理程序
 55         menuItem.addActionListener(new java.awt.event.ActionListener() {
 56  
 57         public void actionPerformed(java.awt.event.ActionEvent evt) {
 58             finish();
 59             System.exit(0);
 60         }
 61         });
 62         menu.add(menuItem);
 63         //建立Help菜單
 64         menu=new JMenu("幫助");
 65         menuBar.add(menu);
 66         menuItem=new JMenuItem("關於");
 67         menu.add(menuItem);
 68         //給菜單項About添加活動事件處理程序
 69         menuItem.addActionListener(new java.awt.event.ActionListener() {
 70  
 71             public void actionPerformed(java.awt.event.ActionEvent evt) {
 72                 aboutActionPerformed(evt);              
 73             }
 74         });
 75         //******************************************************
 76         //結束構建菜單
 77         
 78         //構建第一張卡片panLogin
 79         //*******************************************************
 80         JPanel panLogin = new JPanel();
 81         BoxLayout v = new BoxLayout(panLogin, BoxLayout.Y_AXIS);
 82         panLogin.setLayout(v);
 83         JPanel pt = new JPanel();
 84         panLogin.add(pt);
 85         pt = new JPanel();
 86         labTop.setForeground(Color.BLACK);
 87         pt.add(labTop);
 88         panLogin.add(pt);
 89         pt = new JPanel();
 90         pt.add(new JLabel("用戶名:"));
 91         pt.add(txtName);
 92         panLogin.add(pt);
 93         pt = new JPanel();
 94         pt.add(new JLabel("密碼:"));
 95         pt.add(txtPassword);
 96         panLogin.add(pt);
 97         //給OK按鈕添加活動事件處理程序
 98         butOK.addActionListener(new java.awt.event.ActionListener() {
 99  
100             public void actionPerformed(java.awt.event.ActionEvent evt) {
101                 butOKActionPerformed(evt);
102             }
103         });
104         pt = new JPanel();    
105         pt.add(butOK);
106         panLogin.add(pt);
107         panLogin.add(new JPanel());
108         //第一張卡片panLogin構建結束****************************************************
109         
110         //構建第二張卡片panOperation
111         //********************************************************************
112         JPanel panOperation = new JPanel();
113         panOperation.setLayout(new BorderLayout());
114         panOperation.add(labTop2, BorderLayout.NORTH);
115         panOperation.add(new JLabel("  "), BorderLayout.SOUTH);      
116         JLabel labTop7;   
117         JPanel p2 = new JPanel();
118         p2.setLayout(new GridLayout(5, 3));
119         JButton bPut = new JButton("添加學生信息");
120        JButton bGet = new JButton("");
121         JButton bSearch = new JButton("查詢學生信息");
122         JButton bQuit = new JButton("退出");
123         //給四個按鈕分別添加活動事件處理程序
124         bPut.addActionListener(new java.awt.event.ActionListener() {
125             public void actionPerformed(java.awt.event.ActionEvent evt) {
126                 bPutActionPerformed(evt);
127             }
128         });
129         bSearch.addActionListener(new java.awt.event.ActionListener() {
130  
131             public void actionPerformed(java.awt.event.ActionEvent evt) {
132                 bSearchActionPerformed(evt);
133             }
134         });
135         bQuit.addActionListener(new java.awt.event.ActionListener() {
136  
137             public void actionPerformed(java.awt.event.ActionEvent evt) {
138                 bQuitActionPerformed(evt);
139             }
140         });
141         p2.add(new JLabel(" "));
142         p2.add(new JLabel(" "));
143         p2.add(new JLabel(" "));
144  
145         p2.add(new JLabel(" "));
146         p2.add(bPut);
147         p2.add(new JLabel(" "));
148  
149         p2.add(new JLabel(" "));
150          p2.add(new JLabel(" "));
151        // p2.add(bGet);
152         p2.add(new JLabel(" "));
153  
154         p2.add(new JLabel(" "));
155         p2.add(bSearch);
156         p2.add(new JLabel(" "));
157  
158         p2.add(new JLabel(" "));
159        // p2.add(bQuit);
160         p2.add(new JLabel(" "));
161  
162         panOperation.add(p2, BorderLayout.CENTER);
163         //第二張卡片panOperation構建結束***************************************************
164  
165         //在主窗口中添加前面構建的兩張卡片panLogin和panOperation
166         pane = getContentPane();
167         pane.setLayout(cc);
168         pane.add(panLogin, "login");
169         pane.add(panOperation, "operation");
170         //添加窗口關閉事件
171         this.addWindowListener(new WindowAdapter() {
172  
173             //表示下面的方法是覆蓋父類的方法
174             @Override
175             public void windowClosing(WindowEvent e) {
176                 finish();
177             }
178         });
179         setSize(550, 550);
180         //讓起始窗口停在屏幕的中央
181         Toolkit kit = Toolkit.getDefaultToolkit();
182         Dimension dim = kit.getScreenSize();
183         setLocation(dim.width / 2 - 150, dim.height / 2 - 100);
184         //禁止用戶改變窗口的大小
185         this.setResizable(false);
186         setVisible(true);
187         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
188  
189     }  
190     private void finish(){
191         student.saveData();
192     }
193     //由菜單About調用的方法
194     private void aboutActionPerformed(java.awt.event.ActionEvent evt){
195          JOptionPane.showMessageDialog(this,"歡迎使用溫娉哲開發的學生信息管理系統!","歡迎使用",JOptionPane.INFORMATION_MESSAGE);
196     }         
197     //分別由五個按鈕活動事件處理程序調用的方法
198     private void butOKActionPerformed(java.awt.event.ActionEvent evt) {
199         String no = txtName.getText();
200         String pw = new String(txtPassword.getPassword());
201         if (student.check(no, pw)) {
202             labTop.setForeground(Color.black);
203             labTop.setText("歡迎使用");
204             txtName.setText("");
205             txtPassword.setText("");
206             cc.show(pane, "operation");
207         } else {
208             labTop.setForeground(Color.red);
209             labTop.setText("您的帳號或密碼錯誤,請從新輸入!");
210         }
211     }
212     public void bPutActionPerformed(java.awt.event.ActionEvent evt) {
213          num = Integer.parseInt(JOptionPane.showInputDialog(this, "請添加學生學號:"));
214         labTop2.setText("添加的學生學號爲:" + num);
215         
216          num1 = String.format(JOptionPane.showInputDialog(this, "請添加學生姓名:"));           
217         //num1 = student.getCurrent(). inputName(num1);
218         labTop2.setText("添加的學生姓名爲:" + num1);
219     }
220     public void bSearchActionPerformed(java.awt.event.ActionEvent evt) {
221        
222        num = Integer.parseInt(JOptionPane.showInputDialog(this, "請輸入要查詢的學生學號:"));
223       // num = student.getCurrent().inputID(num);
224         
225       // num1 = student.getCurrent().getinformation();
226          
227        labTop2.setText("學號爲" + num+"的學生姓名爲"+num1);
228     }
229     private void bQuitActionPerformed(java.awt.event.ActionEvent evt) {
230         labTop2.setText("");
231         cc.show(pane, "login");
232         txtName.requestFocusInWindow();
233     }
234     public static void main(String[] args) {
235         StudentFrame student = new StudentFrame();
236     }
237 }

2、添加登陸系統的帳號密碼信息

 1 //package newpackage;
 2 import java.io.*;
 3 import java.util.*;
 4  
 5 public class StudentLogin {
 6     Scanner sc = new Scanner(System.in);     //獲取控制檯輸入
 7     Vector<Student> v = new Vector<Student>();           //建立一個動態數組的對象
 8     private Student atm;  
 9     //current是Account的對象,是Account類的實例化
10       
11     public StudentLogin() {
12         try {     //異常捕獲
13             FileInputStream fi = new FileInputStream("accoun1t.txt");
14             
15             ObjectInputStream oi = new ObjectInputStream(fi);
16             v = (Vector<Student>) oi.readObject();
17         } catch (FileNotFoundException e) {
18             v.add(new Student("admin", "1", 100));
19             v.add(new Student("user", "2", 100));
20             v.add(new Student("windy", "3", 100));
21             v.add(new Student("jason", "4", 100));
22         } catch (Exception e) {
23             System.out.println("Error - " + e.toString());
24         }
25     }
26  
27     public Student getCurrent() {    //獲取當前餘額
28         return atm;
29     }
30  
31     public boolean check(String account, String pw) {     //檢查帳號,密碼是否正確
32         int i;    //定義了一個整型變量
33         Student a;     
34         for (i = 0; i < v.size(); i++) {
35             a = v.get(i);
36             if (a.check(account, pw)) {
37                 atm = a;
38                 return true;
39             }
40         }
41        atm = null;
42         return false;
43     }
44     public void saveData() {      //保存數據
45         try {
46             FileOutputStream fo = new FileOutputStream("account.txt");
47             ObjectOutputStream oo = new ObjectOutputStream(fo);
48             oo.writeObject(v);
49             oo.close();
50         } catch (IOException e) {
51             System.out.println("Error -" + e.toString());
52         }
53  
54     }
55 }

3、主要實現獲取學生的學號和姓名,調用方法

 1 import java.io.*;
 2 public class Student implements Serializable{
 3     private String no;   //帳號
 4     private String password;   //密碼
 5     private int information;       //餘額
 6         private int ID;       //餘額     
 7         private String name;//姓名
 8     public Student(String accounts,String pw,int num) {   //
 9         no=new String(accounts);
10         password=new String(pw);
11         information=(num>0?num:0);
12     }
13 //     public String getNo(){      //獲取學號
14 //         return no;
15 //     }
16     public int getinformation() {  //獲取學生信息
17         return information;
18     }
19     public int inputID(int num) {   //獲取學號
20         
21         return ID;
22     }       
23         public String inputName(String num1) {   //獲取學生姓名
24         return name;
25     }
26     public boolean setPassword(String pw) {     //修改密碼
27         password=new String(pw);
28         return true;
29     }
30     public boolean check(String accounts,String pw) {     //覈對密碼
31         if(no.equals(accounts)&&password.equals(pw))
32             return true;
33         else
34             return false;    
35     }    
36     int Add(int num) {
37         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
38     }
39 }

4、添加學生學號信息,經過查詢學生學號檢索出學生的姓名(沒有使用數據庫,你們懂,嘿嘿)

 1 package newpackage;
 2  
 3 import java.io.*;
 4 import java.util.*;
 5  
 6 public class FunctionFrame {
 7     StudentLogin student = new StudentLogin();     //建立一個student對象
 8     Scanner sc = new Scanner(System.in);    //建立一個sc對象    scanner獲取控制檯輸入,控制檯會一直等待輸入,直到敲回車鍵結束,把所輸入的內容傳給scanner,做爲掃描對象        system.in  是輸入的意思
 9  
10     public void inputNoPassword() {     //定義了一個無返回值的輸入密碼的方法
11         String account, pw;      //定義了倆個字符串類型的變量
12         boolean b;               //定義了一個布爾類型的變量
13         do {                    //循環語句,
14             System.out.print("Please enter account:");   //提示輸入帳號
15             account = sc.next();        //sc是scanner類中的一個對象,此時表示從結果集中連續取值
16             System.out.print("Please enter password:");   //提示輸入密碼
17             pw = sc.next();               //查找並返回來自此掃描器的下一個完整標記
18             b = student.check(account, pw);      //檢查帳號,密碼是否正確
19             if (b == false) {    //若是帳號,密碼不相等,提示請從新輸入
20                 System.out.println("您的帳號或密碼錯誤,請從新輸入!");
21             }
22         } while (b == false);    
23     }
24  
25     public void operate() {     //定義了一個返回值爲空的操做方法
26         int i;   //定義了一個整型變量
27         do {                //設置了四個不一樣的功能
28             System.out.println("please choose:");
29             System.out.println("添加學生信息");
30             System.out.println("");
31             System.out.println("查詢學生信息");
32             System.out.println("");
33             try {
34                 i = sc.nextInt();
35                 if (i == 1) {
36                      inputID();
37                   //  getMoney();
38                 } else if (i == 2) {
39                    // putID();
40                 } else if (i == 3) {
41                     search();
42                 } else if (i == 4) {
43                     break;
44                 } else {
45                     System.out.println("輸入有誤,請從新輸入!");
46                 }
47             } catch (InputMismatchException e) {
48                 System.out.println("輸入有誤\nPlease choose1-4:");
49                 sc.nextLine();
50             }
51         } while (true);
52     }
53     private void inputID() {
54         int i, num;
55         System.out.print("Please enter ID : ");
56         i = sc.nextInt();
57         num = student.getCurrent().inputID(i);
58         System.out.println("You have put into: " + num);
59     } 
60  
61     private void search() {
62         int num;
63         num = student.getCurrent().getBalance();
64         System.out.println("學生信息爲: " + num);
65     }
66  
67     public void start() {
68         do {
69             inputNoPassword();
70             operate();
71             System.out.print("你肯定要退出嗎? y/n");
72             if (sc.next().equals("y")) {
73                student.saveData();
74                 break;
75             }
76         } while (true);
77     }
78  
79     public static void main(String[] args) {
80         FunctionFrame atmNoFrame = new FunctionFrame();
81         atmNoFrame.start();
82     }
83 }
相關文章
相關標籤/搜索