-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentGUI.java
More file actions
270 lines (229 loc) · 8.08 KB
/
StudentGUI.java
File metadata and controls
270 lines (229 loc) · 8.08 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package students;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class StudentGUI extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
//定义窗口中的7个标签,并向右对其
private final JLabel labStuNo=new JLabel("学号",JLabel.RIGHT);
private final JLabel labStuName=new JLabel("姓名",JLabel.RIGHT);
private final JLabel labStuAge=new JLabel("年龄",JLabel.RIGHT);
private final JLabel labStuSex=new JLabel("性别",JLabel.RIGHT);
private final JLabel labStuDepartment=new JLabel("系别",JLabel.RIGHT);
private final JLabel labStuPhone=new JLabel("电话",JLabel.RIGHT);
private final JLabel labStuEmail=new JLabel("e-mail",JLabel.RIGHT);//History
//7个文本框
private final JTextField txtStuNo=new JTextField(12);
private final JTextField txtStuName=new JTextField(12);
private final JTextField txtStuAge=new JTextField(12);
private final JTextField txtStuSex=new JTextField(12);
private final JTextField txtStuDepartment=new JTextField(12);
private final JTextField txtStuPhone=new JTextField(12);
private final JTextField txtStuEmail=new JTextField(12);
//定义窗口中显示学生简历信息的文本区
private final JTextArea txtArea=new JTextArea(13,34);
//滚动窗口
private final JScrollPane scrollPane=new JScrollPane(txtArea);
//显示学生信息的4个按钮
private final JButton btnFirst=new JButton("<<",new ImageIcon("students/images/first.gif"));
private final JButton btnPrior=new JButton("<",new ImageIcon("students/images/pre.gif"));
private final JButton btnNext=new JButton(">",new ImageIcon("students/images/next.gif"));
private final JButton btnLast=new JButton(">>",new ImageIcon("students/images/last.gif"));
//定义工具栏
JToolBar toolBar=new JToolBar("文本工具条");
private final JButton btnUpdate=new JButton("修改",new ImageIcon("students/images/update.gif"));
private final JButton btnDelete=new JButton("删除",new ImageIcon("students/images/Delete.gif"));
private final JButton btnInsert=new JButton("添加",new ImageIcon("students/images/Insert.gif"));
private final JButton btnExit=new JButton("退出",new ImageIcon("students/images/Exit.gif"));
//选项卡
private final JTabbedPane tabbedPane=new JTabbedPane();
//选项卡中的2个面板
private final JPanel tabbedPanel1=new JPanel();
private final JPanel tabbedPanel2=new JPanel();
//7个标签的面板
private final JPanel labPanel=new JPanel();
//7个文本框的面板
private final JPanel txtPanel=new JPanel();
//4个按钮的面板
private final JPanel btnPanel=new JPanel();
//定义构造方法
StudentGUI()
{
//4个工具按钮添加到工具栏
toolBar.add(btnUpdate);
toolBar.add(btnDelete);
toolBar.add(btnInsert);
toolBar.add(btnExit);
//提示信息
btnUpdate.setToolTipText("更新一条记录");
btnDelete.setToolTipText("删除一条记录");
btnInsert.setToolTipText("插入一条记录");
btnExit.setToolTipText("退出程序");
final Container cp=this.getContentPane();
cp.setLayout(new BorderLayout(5,5));
//添加功夫巨烂
cp.add(BorderLayout.NORTH,toolBar);
//管理器
labPanel.setLayout(new GridLayout(7,1,8,8));
labPanel.add(labStuNo);
labPanel.add(labStuName);
labPanel.add(labStuAge);
labPanel.add(labStuSex);
labPanel.add(labStuDepartment);
labPanel.add(labStuPhone);
labPanel.add(labStuEmail);
//放置文本框的面板布局管理器
txtPanel.setLayout(new GridLayout(7,1,8,8));
//添加到txtPanel中
final JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT));
p1.add(txtStuNo);
txtPanel.add(p1);
final JPanel p2=new JPanel(new FlowLayout(FlowLayout.LEFT));
p2.add(txtStuName);
txtPanel.add(p2);
final JPanel p3=new JPanel(new FlowLayout(FlowLayout.LEFT));
p3.add(txtStuAge);
txtPanel.add(p3);
final JPanel p4=new JPanel(new FlowLayout(FlowLayout.LEFT));
p4.add(txtStuSex);
txtPanel.add(p4);
final JPanel p5=new JPanel(new FlowLayout(FlowLayout.LEFT));
p5.add(txtStuDepartment);
txtPanel.add(p5);
final JPanel p6=new JPanel(new FlowLayout(FlowLayout.LEFT));
p6.add(txtStuPhone);
txtPanel.add(p6);
final JPanel p7=new JPanel(new FlowLayout(FlowLayout.LEFT));
p7.add(txtStuEmail);
txtPanel.add(p7);
//第一个选项卡的管理器
tabbedPanel1.setLayout(new BorderLayout(10,10));
//标签
tabbedPanel1.add(BorderLayout.WEST,labPanel);
//文本框
tabbedPanel1.add(BorderLayout.CENTER,txtPanel);
//滚动条
tabbedPanel2.add(scrollPane);
//两个选显卡
tabbedPane.add("个人信息",tabbedPanel1);
tabbedPane.add("个人简历",tabbedPanel2);
//选显卡
cp.add(tabbedPane);
//4个按钮中部
btnPanel.add(btnFirst);
btnPanel.add(btnPrior);
btnPanel.add(btnNext);
btnPanel.add(btnLast);
//南部
cp.add(BorderLayout.SOUTH,btnPanel);
//调用
refreshStudent();
btnFirst.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e)
{
final Student s=StudentDBA.getFirstStudent();
showStudent(s);
}
});
btnPrior.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e)
{
final Student s=StudentDBA.getPrevStudent();
showStudent(s);
}
});
btnNext.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e)
{
final Student s=StudentDBA.getNextStudent();
showStudent(s);
}
});
btnLast.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e)
{
final Student s=StudentDBA.getLastStudent();
showStudent(s);
}
});
btnUpdate.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e)
{
Student s=new Student();
s=getStudent();
StudentDBA.updateStudent(s);
refreshStudent();
}
});
//给删除按钮添加事件监听器
btnDelete.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
StudentDBA.deleteStudent();
refreshStudent();
}
});
//给添加按钮添加事件监听器
btnInsert.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
final Student s = getStudent();
StudentDBA.addStudent(s);
refreshStudent();
}
});
//给退出按钮添加事件监听器
btnExit.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
System.exit(1);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("学生信息管理");
this.setSize(400,400);
this.setVisible(true);
}
private void showStudent(final Student s){
txtStuNo.setText(s.getStuNo());
txtStuName.setText(s.getStuName());
txtStuAge.setText(s.getStuAge());
txtStuSex.setText(s.getStuSex());
txtStuDepartment.setText(s.getStuDepartment());
txtStuPhone.setText(s.getStuPhone());
txtStuEmail.setText(s.getStuEmail());
}
private void clearStudent(){
txtStuNo.setText("");
txtStuName.setText("");
txtStuAge.setText("");
txtStuSex.setText("");
txtStuDepartment.setText("");
txtStuPhone.setText("");
txtStuEmail.setText("");
txtArea.setText("");
}
private Student getStudent(){
final Student s = new Student();
s.setStuNo(txtStuNo.getText());
s.setStuName(txtStuName.getText());
s.setStuAge(txtStuAge.getText());
s.setStuSex(txtStuSex.getText());
s.setStuDepartment(txtStuDepartment.getText());
s.setStuPhone(txtStuPhone.getText());
s.setStuEmail(txtStuEmail.getText());
return s;
}
private void refreshStudent(){
StudentDBA.getAllStudents();
final Student s = StudentDBA.getCurrentStudent();
if(s != null){
showStudent(s);
}
else clearStudent();
}
public static void main(String[] args)
{
new StudentGUI();
}
}