-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeManagerUI.java
More file actions
291 lines (257 loc) · 11.4 KB
/
EmployeeManagerUI.java
File metadata and controls
291 lines (257 loc) · 11.4 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package sys;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class EmployeeManagerUI extends JFrame {
private JFrame frame;
private JTable table; // 表格
private DefaultTableModel model; // 表格模型,用于管理表格的数据
private JTextField NameTextFieldSearch; // 搜索输入框
private JButton btnSearch; // 搜索按钮
private JButton btnAdd; // 添加按钮
private JButton btnShowAll; // 显示全部按钮
private String filePath = "E:\\jcode\\newjavaseaiman\\day08_oop\\src\\employees.txt";
public EmployeeManagerUI(String username) {
super("欢迎 " + username + " 进入管理系统");
frame = this;
initialize();
frame.setVisible(true);
// 加载员工信息
loadEmployees();
}
private void initialize() {
// 设置全局字体大小
UIManager.put("defaultFont", new java.awt.Font("楷体", Font.PLAIN, 16));
this.setBounds(100, 100, 800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
// 输入框和搜索按钮
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
topPanel.setBackground(new Color(240, 240, 240));
NameTextFieldSearch = new JTextField(20);
NameTextFieldSearch.setFont(new Font("楷体", Font.PLAIN, 14));
btnSearch = new JButton("搜索");
btnSearch.setFont(new Font("楷体", Font.PLAIN, 14));
btnAdd = new JButton("添加");
btnAdd.setFont(new Font("楷体", Font.PLAIN, 14));
btnShowAll = new JButton("显示全部");
btnShowAll.setFont(new Font("楷体", Font.PLAIN, 14));
topPanel.add(NameTextFieldSearch);
topPanel.add(btnSearch);
topPanel.add(btnAdd);
topPanel.add(btnShowAll);
// 创建表格模型
model = new DefaultTableModel(
new Object[][]{},
new String[]{"ID", "姓名", "性别", "年龄", "电话", "职位", "入职日期", "薪水", "部门"}
) {
@Override
public boolean isCellEditable(int row, int column) {
return false; // 设置所有单元格为不可编辑
}
};
table = new JTable(model);
table.setFont(new Font("楷体", Font.PLAIN, 14)); // 设置表格字体大小
table.setRowHeight(30);
table.setBackground(Color.WHITE);
table.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
// 右键菜单
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem editItem = new JMenuItem("编辑");
JMenuItem deleteItem = new JMenuItem("删除");
popupMenu.add(editItem);
popupMenu.add(deleteItem);
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) { // 检查是否为鼠标右键
int row = table.rowAtPoint(e.getPoint());
if (row >= 0) {
table.setRowSelectionInterval(row, row);
popupMenu.show(table, e.getX(), e.getY());
}
}
}
});
// 绑定事件到菜单项
editItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
showEditDialog(selectedRow);
}
}
});
deleteItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
Object idObj = model.getValueAt(selectedRow, 0); // 假设ID在第一列
int id;
try {
id = Integer.parseInt(idObj.toString()); // 尝试转换为整数
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "ID不是一个有效的整数: " + idObj);
return;
}
int option = JOptionPane.showConfirmDialog(frame, "确定删除 ID: " + id + " 的记录吗?", "确认删除", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION) {
// 从模型中移除行
model.removeRow(selectedRow);
// 更新文件
updateFileAfterDelete(selectedRow);
}
}
}
});
// 搜索按钮监听器
btnSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String searchValue = NameTextFieldSearch.getText();
if (!searchValue.isEmpty()) {
performSearch(searchValue);
} else {
JOptionPane.showMessageDialog(frame, "请输入搜索条件!");
}
}
});
// 添加按钮监听器
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 这里可以添加添加新员工的逻辑
new AddEmployee(EmployeeManagerUI.this); // 传递 EmployeeManagerUI 实例
}
});
// 显示全部按钮监听器
btnShowAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
refreshTable();
}
});
frame.getContentPane().add(topPanel, BorderLayout.NORTH);
frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
}
protected void loadEmployees() {
// 清空表格模型
model.setRowCount(0);
// 从文件中读取员工信息
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 9) {
model.addRow(parts);
}
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "文件读取失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void updateFileAfterDelete(int deletedRow) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath));
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath + ".tmp"))) {
String line;
int currentRow = 0;
while ((line = reader.readLine()) != null) {
if (currentRow != deletedRow) {
writer.write(line);
writer.newLine();
}
currentRow++;
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "文件更新失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
} finally {
// 删除原始文件
File originalFile = new File(filePath);
originalFile.delete();
// 重命名临时文件为原文件名
File tempFile = new File(filePath + ".tmp");
tempFile.renameTo(originalFile);
}
}
private void showEditDialog(final int selectedRow) {
final Object[] rowData = new Object[model.getColumnCount()];
for (int i = 0; i < model.getColumnCount(); i++) {
rowData[i] = model.getValueAt(selectedRow, i);
}
JPanel panel = new JPanel();
List<JTextField> textFields = new ArrayList<>();
for (int i = 0; i < model.getColumnCount(); i++) {
JLabel label = new JLabel(model.getColumnName(i) + ": ");
JTextField textField = new JTextField(rowData[i].toString());
panel.add(label);
panel.add(textField);
textFields.add(textField);
}
int result = JOptionPane.showConfirmDialog(frame, panel, "编辑员工信息", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
for (int i = 0; i < textFields.size(); i++) {
model.setValueAt(textFields.get(i).getText(), selectedRow, i);
}
updateFileAfterEdit(selectedRow, textFields);
}
}
private void updateFileAfterEdit(int selectedRow, List<JTextField> textFields) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath));
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath + ".tmp"))) {
String line;
int currentRow = 0;
while ((line = reader.readLine()) != null) {
if (currentRow == selectedRow) {
StringBuilder updatedLine = new StringBuilder();
for (JTextField textField : textFields) {
updatedLine.append(textField.getText()).append(",");
}
updatedLine.deleteCharAt(updatedLine.length() - 1); // 移除最后一个逗号
writer.write(updatedLine.toString());
} else {
writer.write(line);
}
writer.newLine();
currentRow++;
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "文件更新失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
} finally {
// 删除原始文件
File originalFile = new File(filePath);
originalFile.delete();
// 重命名临时文件为原文件名
File tempFile = new File(filePath + ".tmp");
tempFile.renameTo(originalFile);
}
}
private void performSearch(String searchValue) {
// 清空表格模型
model.setRowCount(0);
// 从文件中读取员工信息
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 9 && (parts[0].equals(searchValue) || parts[1].contains(searchValue))) {
model.addRow(parts);
}
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "文件读取失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
public void refreshTable() {
loadEmployees();
}
}