學生管理系統簡單的實現,供初學Java Swing同學學習使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; //主類,程序的入口 public class begin { public static void main(String[] args) { new begindemo( "這是我的管理系統" ); } } class begindemo extends JFrame { //登錄的用戶名和密碼 private final String userName = "123" ; private final String password = "123" ; //聲明屏幕的寬高,程序窗口的寬高 private int windowWidth; private int windowHeight; private int screenSizeWidth; private int screenSizeHeight; //構造函數, public begindemo(String title) { super (title); //設置標題 this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置能關閉窗口 this .setSize( 600 , 600 ); //設置窗口的大小 this .setLayout( null ); //設置程序默認布局格式為空,以便于后期自己簡單的設置布局 this .setResizable( false ); //設置不可縮放 init(); //執行初始化函數(將用戶名密碼等組件加入到面板中) this .setVisible( true ); //使程序可見 } public void init() { //給屏幕的寬度高度,程序窗口的寬度高度賦值 Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); screenSizeWidth = ( int ) dimension.getWidth(); screenSizeHeight = ( int ) dimension.getHeight(); windowWidth = this .getWidth(); windowHeight = this .getHeight(); //設置程序窗口的位置為屏幕的正中央 this .setLocation(screenSizeWidth / 2 - windowWidth / 2 , screenSizeHeight / 2 - windowHeight / 2 ); // 聲明姓名,密碼的標簽 JLabel username_label = new JLabel( "姓名" ); JLabel password_label = new JLabel( "密碼" ); // 聲明姓名輸入框和密碼輸入框 final JTextField user_field = new JTextField(); final JPasswordField password_field = new JPasswordField(); //聲明登錄按鈕 JButton login_btn = new JButton( "登錄" ); //設置各個標簽和輸入框的大小和位置 username_label.setBounds( 150 , 100 , 100 , 50 ); password_label.setBounds( 150 , 200 , 100 , 50 ); user_field.setBounds( 200 , 100 , 300 , 50 ); password_field.setBounds( 200 , 200 , 300 , 50 ); login_btn.setBounds( 300 , 300 , 100 , 50 ); this .add(username_label); this .add(password_label); this .add(user_field); this .add(password_field); this .add(login_btn); //登錄按鈕的監聽器 login_btn.addActionListener( new ActionListener() { @SuppressWarnings ( "deprecation" ) @Override //當按鈕被單擊時自動調動這個方法 public void actionPerformed(ActionEvent event) { //如果用戶名和密碼都是123,那么彈出對話框顯示登錄成功,并且開啟另一個主框架(主頁) if (user_field.getText().equals(userName) && password_field.getText().equals(password)) { JOptionPane.showMessageDialog( null , "登錄成功" , "Login" , JOptionPane.INFORMATION_MESSAGE); //聲明主頁 JFrame home_page = new JFrame( "主頁" ); //給主頁設置位置 home_page.setLocation(screenSizeWidth / 2 - windowWidth / 2 + 50 , screenSizeHeight / 2 - windowHeight / 2 + 50 ); //給主頁設置大小 home_page.setSize(windowWidth, windowHeight); //設置主頁能夠關閉,并且登錄成功后將登錄頁面隱藏 home_page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); home_page.setVisible( true ); setVisible( false ); //登錄頁面隱藏 } else //反之,登錄不成功,重新登錄 { JOptionPane.showMessageDialog( null , "登錄失敗,請重新登錄" , "Login" , JOptionPane.INFORMATION_MESSAGE); //設置輸入框的內容為空,讓用戶重新輸入 user_field.setText( "" ); password_field.setText( "" ); } } }); } } |
添加了一個學生的類,方便以后使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
package demo; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; //主類,程序的入口 public class begin { public static void main(String[] args) { new begindemo( "這是我的管理系統" ); new student(); } } class begindemo extends JFrame { //登錄的用戶名和密碼 private final String userName = "123" ; private final String password = "123" ; //聲明屏幕的寬高,程序窗口的寬高 private int windowWidth; private int windowHeight; private int screenSizeWidth; private int screenSizeHeight; //構造函數, public begindemo(String title) { super (title); //設置標題 this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置能關閉窗口 this .setSize( 600 , 600 ); //設置窗口的大小 this .setLayout( null ); //設置程序默認布局格式為空,以便于后期自己簡單的設置布局 this .setResizable( false ); //設置不可縮放 init(); //執行初始化函數(將用戶名密碼等組件加入到面板中) this .setVisible( true ); //使程序可見 } public void init() { //給屏幕的寬度高度,程序窗口的寬度高度賦值 Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); screenSizeWidth = ( int ) dimension.getWidth(); screenSizeHeight = ( int ) dimension.getHeight(); windowWidth = this .getWidth(); windowHeight = this .getHeight(); //設置程序窗口的位置為屏幕的正中央 this .setLocation(screenSizeWidth / 2 - windowWidth / 2 , screenSizeHeight / 2 - windowHeight / 2 ); // 聲明姓名,密碼的標簽 JLabel username_label = new JLabel( "姓名" ); JLabel password_label = new JLabel( "密碼" ); // 聲明姓名輸入框和密碼輸入框 final JTextField user_field = new JTextField(); final JPasswordField password_field = new JPasswordField(); //聲明登錄按鈕 JButton login_btn = new JButton( "登錄" ); //設置各個標簽和輸入框的大小和位置 username_label.setBounds( 150 , 100 , 100 , 50 ); password_label.setBounds( 150 , 200 , 100 , 50 ); user_field.setBounds( 200 , 100 , 300 , 50 ); password_field.setBounds( 200 , 200 , 300 , 50 ); login_btn.setBounds( 300 , 300 , 100 , 50 ); this .add(username_label); this .add(password_label); this .add(user_field); this .add(password_field); this .add(login_btn); //登錄按鈕的監聽器 login_btn.addActionListener( new ActionListener() { @SuppressWarnings ( "deprecation" ) @Override //當按鈕被單擊時自動調動這個方法 public void actionPerformed(ActionEvent event) { //如果用戶名和密碼都是123,那么彈出對話框顯示登錄成功,并且開啟另一個主框架(主頁) if (user_field.getText().equals(userName) && password_field.getText().equals(password)) { JOptionPane.showMessageDialog( null , "登錄成功" , "Login" , JOptionPane.INFORMATION_MESSAGE); //聲明主頁 JFrame home_page = new JFrame( "主頁" ); //給主頁設置位置 home_page.setLocation(screenSizeWidth / 2 - windowWidth / 2 + 50 , screenSizeHeight / 2 - windowHeight / 2 + 50 ); //給主頁設置大小 home_page.setSize(windowWidth, windowHeight); //設置主頁能夠關閉,并且登錄成功后將登錄頁面隱藏 home_page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); home_page.setVisible( true ); setVisible( false ); //登錄頁面隱藏 } else //反之,登錄不成功,重新登錄 { JOptionPane.showMessageDialog( null , "登錄失敗,請重新登錄" , "Login" , JOptionPane.INFORMATION_MESSAGE); //設置輸入框的內容為空,讓用戶重新輸入 user_field.setText( "" ); password_field.setText( "" ); } } }); } } //聲明一個學生類,方便以后添加學生信息用 class student { private String name; private String sex; private int number; //學號 private String class_; //班級 private double grade; //默認構造函數,new一個對象的時候會自動調用 public student() { this .name = "" ; this .number = 0 ; this .class_ = "" ; this .grade = 0 ; System.out.println( "這是一個學生" ); } //重載的構造函數 public student(String name, int number, String class_, double grade) { this .name = name; this .number = number; this .class_ = class_; this .grade = grade; } //下面是設置名字性別學號等的函數,以后在輸入學生信息存儲的時候會調用,現在先寫出來方便以后調用 public void setName(String name) { this .name = name; } public void setSex(String sex) { this .sex = sex; } public void setNumber( int number) { this .number = number; } public void setClass(String class_) { this .class_ = class_; } public void setGrade( double grade) { this .grade = grade; } //下面是幾個得到學生姓名性別等的函數,在以后顯示學生信息的時候調用它來顯示學生的信息到窗口上。 public String getName() { return this .name; } public String getSex() { return this .sex; } public int getNumber() { return this .number; } public String getClass_() { return this .class_; } public double getGrade() { return this .grade; } //和上面的函數差不多用來一下設置一個學生的所有個人信息 public void setAll(String name, String sex, int number, String class_, double grade) { this .name=name; this .number=number; this .sex=sex; this .class_ = class_; this .grade = grade; } //一下得到一個學生的所有信息,就不用一個一個getName或者getSex了 public String getAll() { String output= "" ; output+=getName()+ " " +getSex()+ " " +getNumber()+ " " +getClass_()+ " " +getGrade(); return output; } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/sunlanchang/article/details/53202416