(ActionEvent e)
| 402 | updateButton.addActionListener(new ActionListener() { |
| 403 | |
| 404 | @Override |
| 405 | public void actionPerformed(ActionEvent e) { |
| 406 | |
| 407 | // 创建一个JPanel来包含两个输入框 |
| 408 | JPanel inputPanel = new JPanel(new GridLayout(4, 4)); |
| 409 | inputPanel.add(new JLabel("键名:")); |
| 410 | JTextField nameField = new JTextField(10); |
| 411 | inputPanel.add(nameField); |
| 412 | inputPanel.add(new JLabel("键值:")); |
| 413 | JTextField valueField = new JTextField(10); |
| 414 | inputPanel.add(valueField); |
| 415 | |
| 416 | |
| 417 | // 弹出自定义对话框 |
| 418 | int result = JOptionPane.showConfirmDialog(null, inputPanel, "新增按键", |
| 419 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); |
| 420 | |
| 421 | // 当用户点击OK时处理输入 |
| 422 | if (result == JOptionPane.OK_OPTION) { |
| 423 | String keyName = nameField.getText().trim(); |
| 424 | String keyValue = valueField.getText().trim(); |
| 425 | |
| 426 | // 验证输入是否非空 |
| 427 | if (!keyName.isEmpty() && !keyValue.isEmpty()) { |
| 428 | // 将键名和键值以"键名":{键值}的形式保存在rule.txt的最后一行 |
| 429 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(rulesPath, true))) { |
| 430 | System.out.println(keyValue); |
| 431 | writer.write("\"" + keyName + "\":{" + keyValue + "},"); |
| 432 | writer.newLine(); // Ensure the new entry is on a new line |
| 433 | } catch (IOException addError) { |
| 434 | addError.printStackTrace(); |
| 435 | JOptionPane.showMessageDialog(null, "无法写入文件", "错误", JOptionPane.ERROR_MESSAGE); |
| 436 | } |
| 437 | |
| 438 | // 添加右键菜单功能 |
| 439 | try { |
| 440 | BufferedReader reader = new BufferedReader(new FileReader(rulesPath)); |
| 441 | Map<String, String> newMap = new LinkedHashMap<>(); |
| 442 | String line; |
| 443 | while ((line = reader.readLine()) != null) { |
| 444 | line = line.trim(); |
| 445 | // 跳过井号注释 |
| 446 | if (line.startsWith("#")) { |
| 447 | continue; |
| 448 | } |
| 449 | if (line.startsWith("\"") && line.contains("{") && line.contains("}")) { |
| 450 | String[] parts = line.split(":", 2); |
| 451 | |
| 452 | String key = parts[0].substring(1, parts[0].length() - 1).trim(); |
| 453 | |
| 454 | String value = parts[1].substring(1, parts[1].length() - 2).trim(); |
| 455 | newMap.put(key, value); |
| 456 | } |
| 457 | } |
| 458 | reader.close(); |
| 459 | // 配置文件更新并新增按钮 |
| 460 | for (Map.Entry<String, String> entry : newMap.entrySet()) { |
| 461 | JButton existingButton = buttonsMap.get(entry.getKey()); |
nothing calls this directly
no test coverage detected