package home; import javax.swing.*; import javax.swing.Timer; import java.awt.*; import java.awt.event.*; import java.io.File; public class ConfigPanel extends JPanel { private JTextField deviceIdField; private JTextField groupField; private JTextField frequencyField; private JButton readConfigBtn; private JButton saveConfigBtn; private JTextField filePathField; private JButton selectFileBtn; private JButton upgradeBtn; private JProgressBar progressBar; private SerialPortService serialService; private FirmwareUpgrader firmwareUpgrader; private AOAFollowSystem parentFrame; // ÓÃÓÚ½çÃæÎı¾µÄ±êÇ© private JLabel deviceIdLabel; private JLabel groupLabel; private JLabel frequencyLabel; private JLabel selectFileLabel; public ConfigPanel(SerialPortService serialService, AOAFollowSystem parentFrame) { this.serialService = serialService; this.parentFrame = parentFrame; this.firmwareUpgrader = new FirmwareUpgrader(serialService); setLayout(new GridBagLayout()); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10, 10, 10, 10); gbc.fill = GridBagConstraints.HORIZONTAL; // É豸±àºÅ - ʹÓÃgetString()·½·¨ gbc.gridx = 0; gbc.gridy = 0; deviceIdLabel = new JLabel(getString("device_id") + ":"); add(deviceIdLabel, gbc); gbc.gridx = 1; deviceIdField = new JTextField(15); deviceIdField.setText("2548"); add(deviceIdField, gbc); // ͨÐÅС×é - ʹÓÃgetString()·½·¨ gbc.gridx = 0; gbc.gridy = 1; groupLabel = new JLabel(getString("group") + ":"); add(groupLabel, gbc); gbc.gridx = 1; groupField = new JTextField(15); groupField.setText("2"); add(groupField, gbc); // ͨÐÅÆµÂÊ - ʹÓÃgetString()·½·¨ gbc.gridx = 0; gbc.gridy = 2; frequencyLabel = new JLabel(getString("frequency") + ":"); add(frequencyLabel, gbc); gbc.gridx = 1; frequencyField = new JTextField(15); frequencyField.setText("1"); add(frequencyField, gbc); // Ñ¡ÔñÎļþ - ʹÓÃgetString()·½·¨ gbc.gridx = 0; gbc.gridy = 3; selectFileLabel = new JLabel(getString("select_file") + ":"); add(selectFileLabel, gbc); gbc.gridx = 1; JPanel filePanel = new JPanel(new BorderLayout(5, 0)); filePathField = new JTextField(); filePathField.setEditable(false); selectFileBtn = new JButton("..."); selectFileBtn.setPreferredSize(new Dimension(30, 25)); selectFileBtn.addActionListener(e -> selectFile()); filePanel.add(filePathField, BorderLayout.CENTER); filePanel.add(selectFileBtn, BorderLayout.EAST); add(filePanel, gbc); // ¹Ì¼þÉý¼¶ gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 2; upgradeBtn = createColoredButton(getString("upgrade"), new Color(70, 130, 180)); upgradeBtn.setPreferredSize(new Dimension(100, 30)); upgradeBtn.addActionListener(e -> upgradeFirmware()); add(upgradeBtn, gbc); // ½ø¶ÈÌõ gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 2; progressBar = new JProgressBar(0, 100); progressBar.setStringPainted(true); progressBar.setVisible(false); add(progressBar, gbc); // ²Ù×÷°´Å¥ gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 2; JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); readConfigBtn = createColoredButton(getString("read_config"), new Color(70, 130, 180)); readConfigBtn.setPreferredSize(new Dimension(100, 30)); saveConfigBtn = createColoredButton(getString("save_config"), new Color(50, 205, 50)); saveConfigBtn.setPreferredSize(new Dimension(100, 30)); readConfigBtn.addActionListener(e -> readConfig()); saveConfigBtn.addActionListener(e -> saveConfig()); buttonPanel.add(readConfigBtn); buttonPanel.add(saveConfigBtn); add(buttonPanel, gbc); } private JButton createColoredButton(String text, Color color) { JButton button = new JButton(text); button.setBackground(color); button.setForeground(Color.WHITE); button.setFocusPainted(false); button.setBorder(BorderFactory.createEmptyBorder(8, 15, 8, 15)); button.addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { button.setBackground(color.darker()); } @Override public void mouseExited(MouseEvent e) { button.setBackground(color); } }); return button; } private void selectFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("BIN Files", "bin")); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); filePathField.setText(selectedFile.getAbsolutePath()); } } private void upgradeFirmware() { String filePath = filePathField.getText(); if (filePath.isEmpty()) { JOptionPane.showMessageDialog(this, getString("select_bin_file"), getString("error"), JOptionPane.ERROR_MESSAGE); return; } if (!serialService.isOpen()) { JOptionPane.showMessageDialog(this, getString("open_serial_first"), getString("error"), JOptionPane.ERROR_MESSAGE); return; } // ½øÐÐÉý¼¶£¬ÏÔʾ½ø¶ÈÌõ upgradeBtn.setEnabled(false); selectFileBtn.setEnabled(false); progressBar.setVisible(true); progressBar.setValue(0); // ÔÚÐÂÏß³ÌÖÐÖ´ÐÐÉý¼¶²Ù×÷ new Thread(() -> { try { firmwareUpgrader.upgradeFirmware(filePath, progress -> { SwingUtilities.invokeLater(() -> { progressBar.setValue(progress); }); }); SwingUtilities.invokeLater(() -> { JOptionPane.showMessageDialog(this, getString("upgrade_success"), getString("info"), JOptionPane.INFORMATION_MESSAGE); upgradeBtn.setEnabled(true); selectFileBtn.setEnabled(true); }); } catch (Exception e) { SwingUtilities.invokeLater(() -> { JOptionPane.showMessageDialog(this, getString("upgrade_failed") + ": " + e.getMessage(), getString("error"), JOptionPane.ERROR_MESSAGE); upgradeBtn.setEnabled(true); selectFileBtn.setEnabled(true); }); } }).start(); } private void readConfig() { // Ä£Äâ¶ÁÈ¡ÅäÖà readConfigBtn.setBackground(Color.GREEN); Timer timer = new Timer(500, e -> { readConfigBtn.setBackground(new Color(70, 130, 180)); }); timer.setRepeats(false); timer.start(); JOptionPane.showMessageDialog(this, getString("config_read_success"), getString("info"), JOptionPane.INFORMATION_MESSAGE); } private void saveConfig() { // ÑéÖ¤ÊäÈë try { Integer.parseInt(deviceIdField.getText()); Integer.parseInt(groupField.getText()); Integer.parseInt(frequencyField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, getString("input_valid_number"), getString("error"), JOptionPane.ERROR_MESSAGE); return; } // Ä£Äâ±£´æÅäÖà saveConfigBtn.setBackground(Color.GREEN); Timer timer = new Timer(500, e -> { saveConfigBtn.setBackground(new Color(50, 205, 50)); }); timer.setRepeats(false); timer.start(); JOptionPane.showMessageDialog(this, getString("config_save_success"), getString("info"), JOptionPane.INFORMATION_MESSAGE); } public void updateLanguage() { // ¸üа´Å¥Îı¾ readConfigBtn.setText(getString("read_config")); saveConfigBtn.setText(getString("save_config")); upgradeBtn.setText(getString("upgrade")); // ¸üбêÇ©Îı¾ deviceIdLabel.setText(getString("device_id") + ":"); groupLabel.setText(getString("group") + ":"); frequencyLabel.setText(getString("frequency") + ":"); selectFileLabel.setText(getString("select_file") + ":"); } private String getString(String key) { return parentFrame.getString(key); } }