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
|
public class JMenuBarExample {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public static void main(String[] args) {
JFrame frame = new JFrame("星火纺织厂信息系统");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建菜单栏
JMenuBar menuBar = createMenuBar(frame);
frame.setJMenuBar(menuBar);
// 创建工具栏
JToolBar toolBar = new JToolBar();
JButton btn1 = new JButton("磨砂分厂职工信息");
btn1.addActionListener(e -> showTabbedPaneDialog(frame));
JButton btn2 = new JButton("纺纱分厂工资信息");
btn2.addActionListener(e -> JOptionPane.showMessageDialog(frame, "纺纱分厂工资信息功能未实现"));
JButton btn3 = new JButton("人事部职工信息");
btn3.addActionListener(e -> JOptionPane.showMessageDialog(frame, "人事部职工信息功能未实现"));
toolBar.add(btn1);
toolBar.add(btn2);
toolBar.add(btn3);
// 添加工具栏到窗口顶部
frame.add(toolBar, BorderLayout.NORTH);
// 添加右键菜单
JPopupMenu popupMenu = createPopupMenu(frame);
frame.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
// 窗口居中
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
frame.setLocation(screenSize.width / 2 - WIDTH / 2, screenSize.height / 2 - HEIGHT / 2);
frame.setVisible(true);
}
private static JMenuBar createMenuBar(JFrame frame) {
JMenuBar menuBar = new JMenuBar();
// 职工信息系统菜单
JMenu menu1 = createMenu("职工信息系统", 'Z',
createMenuItem("磨砂分厂职工信息", 'M', e -> showTabbedPaneDialog(frame)),
createMenuItem("纺纱分厂工资信息", 'F', null),
null,
createMenuItem("人事部职工信息", 'R', null),
createMenuItem("财务部职工信息", 'C', null),
createMenuItem("材料可职工信息", 0, null),
createMenuItem("销售科职工信息", 0, null),
createMenuItem("成品车间职工信息", 0, null));
// 中层干部信息系统菜单
JMenu menu2 = new JMenu("中层干部信息系统");
menu2.setMnemonic('C');
// 创建菜单项
JMenuItem factoryManagerItem = createMenuItem("分厂厂长信息", 0, null);
JMenuItem departmentManagerItem = createMenuItem("部门经理信息", 0, null);
// 使用 disableManagerMenu 方法处理禁用逻辑
factoryManagerItem.addActionListener(e -> disableManagerMenu(departmentManagerItem));
// 添加到 menu2
menu2.add(factoryManagerItem);
menu2.add(departmentManagerItem);
// 工资系统菜单(复选框菜单)
JMenu menu3 = new JMenu("工资系统");
menu3.setMnemonic('G');
menu3.add(createCheckBoxMenuItem("职工工资信息", false));
menu3.add(createCheckBoxMenuItem("领导工资信息", false));
// 查询系统菜单(单选框菜单)
JMenu menu4 = new JMenu("查询系统");
menu4.setMnemonic('F');
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem searchByName = createRadioButtonMenuItem("按照姓名查询", group, true);
JRadioButtonMenuItem searchByID = createRadioButtonMenuItem("按照工号查询", group, false);
menu4.add(searchByName);
menu4.add(searchByID);
// 登录系统菜单
JMenu menu5 = createMenu("登录系统", 'D',
createMenuItem("添加登录用户名", 0, null));
// 帮助系统菜单
JMenu menu6 = createMenu("帮助系统", 'H',
createMenuItem("版本信息", 0, null),
createMenuItem("帮助信息", 0, null));
// 添加菜单到菜单栏
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
menuBar.add(menu4);
menuBar.add(menu5);
menuBar.add(menu6);
return menuBar;
}
// 禁用菜单项的方法
private static void disableManagerMenu(JMenuItem menuItem) {
if (menuItem != null) {
menuItem.setEnabled(false); // 禁用菜单项
}
}
private static JPopupMenu createPopupMenu(JFrame frame) {
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem infoItem = new JMenuItem("显示信息");
infoItem.addActionListener(e -> JOptionPane.showMessageDialog(frame, "右键菜单:显示信息"));
popupMenu.add(infoItem);
JMenuItem exitItem = new JMenuItem("退出");
exitItem.addActionListener(e -> System.exit(0));
popupMenu.add(exitItem);
return popupMenu;
}
private static JCheckBoxMenuItem createCheckBoxMenuItem(String title, boolean selected) {
JCheckBoxMenuItem item = new JCheckBoxMenuItem(title);
item.setSelected(selected);
return item;
}
private static JRadioButtonMenuItem createRadioButtonMenuItem(String title, ButtonGroup group, boolean selected) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(title, selected);
group.add(item);
return item;
}
private static JMenu createMenu(String title, char mnemonic, Object... items) {
JMenu menu = new JMenu(title);
menu.setMnemonic(mnemonic);
for (Object item : items) {
if (item == null) {
menu.addSeparator();
} else if (item instanceof JMenuItem) {
menu.add((JMenuItem) item);
}
}
return menu;
}
private static JMenuItem createMenuItem(String title, int acceleratorKey, ActionListener action) {
JMenuItem item = new JMenuItem(title);
if (acceleratorKey != 0) {
item.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey, InputEvent.CTRL_DOWN_MASK));
}
if (action != null) {
item.addActionListener(action);
}
return item;
}
private static void showTabbedPaneDialog(JFrame parentFrame) {
JDialog dialog = new JDialog(parentFrame, "磨砂分厂职工信息", true);
dialog.setSize(700, 400);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
dialog.setLocation(screenSize.width / 2 - dialog.getWidth() / 2,
screenSize.height / 2 - dialog.getHeight() / 2);
JTabbedPane tabbedPane = new JTabbedPane();
for (int i = 1; i <= 5; i++) {
JPanel panel = createWorkshopPanel(i);
tabbedPane.addTab("车间 " + i + " 信息", panel);
}
dialog.add(tabbedPane);
dialog.setVisible(true);
}
private static JPanel createWorkshopPanel(int workshopNumber) {
JPanel panel = new JPanel(new java.awt.GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5); // 设置组件间的间距
gbc.fill = GridBagConstraints.HORIZONTAL;
// 添加基本信息的标题
JLabel titleLabel = new JLabel(" 基本信息", JLabel.CENTER);
titleLabel.setFont(new Font("Serif", Font.BOLD, 16));
titleLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 50, 0));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4; // 跨两列
panel.add(titleLabel, gbc);
// 恢复单元格宽度
gbc.gridwidth = 1;
// 字段标签与输入框
String[] labels = { "姓名", "出生年月", "工号", "家庭住址", "性别", "车间", "年龄", "职位" };
for (int i = 0; i < labels.length / 2; i++) {
// 添加标签
JLabel label1 = new JLabel(labels[i] + ":");
gbc.gridx = 0;
gbc.gridy = i + 1;
panel.add(label1, gbc);
// 添加文本框
JTextField textField1 = new JTextField(20);
gbc.gridx = 1;
gbc.gridy = i + 1;
panel.add(textField1, gbc);
// 添加标签
JLabel label2 = new JLabel(labels[i] + ":");
gbc.gridx = 2;
gbc.gridy = i + 1;
panel.add(label2, gbc);
// 添加文本框
JTextField textField2 = new JTextField(20);
gbc.gridx = 3;
gbc.gridy = i + 1;
panel.add(textField2, gbc);
}
return panel;
}
}
|