package dell_system; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.lang.reflect.Method; import java.util.Map; import java.util.ResourceBundle; import java.util.regex.Pattern; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.DefaultListCellRenderer; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JTextField; import targets.SystemConfiguration; @SuppressWarnings("serial") public class SettingsPanelContent extends JPanel { private ResourceBundle messages; // ¶àÓïÑÔ×ÊÔ´°ü private Map settingComponents; // ÉèÖÃ×é¼þÓ³Éä private SystemConfiguration currentConfig; // µ±Ç°ÏµÍ³ÅäÖà private int settingIndex = 1; // ÉèÖÃÏîË÷Òý private int gridy = 0; // GridBag²¼¾ÖµÄÐмÆÊýÆ÷ private SystemSettingsPanel parentPanel; // ¸¸Ãæ°åÒýÓà // ¹¹Ô캯Êý public SettingsPanelContent(ResourceBundle messages, Map settingComponents, SystemConfiguration currentConfig) { this.messages = messages; this.settingComponents = settingComponents; this.currentConfig = currentConfig; initContent(); // ³õʼ»¯ÄÚÈÝ } // ÉèÖø¸Ãæ°å public void setParentPanel(SystemSettingsPanel parentPanel) { this.parentPanel = parentPanel; } // ³õʼ»¯ÄÚÈÝ private void initContent() { setLayout(new GridBagLayout()); // ʹÓÃGridBag²¼¾Ö setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15)); // ÉèÖñ߾à createBasicSettings(); // ´´½¨»ù±¾ÉèÖò¿·Ö createPositionSettings(); // ´´½¨Î»ÖÃÉèÖò¿·Ö createDisplaySettings(); // ´´½¨ÏÔʾÉèÖò¿·Ö createOtherSettings(); // ´´½¨ÆäËûÉèÖò¿·Ö } // ´´½¨»ù±¾ÉèÖò¿·Ö private void createBasicSettings() { addSectionTitle(messages.getString("BASIC_SETTINGS")); // Ìí¼Ó²¿·Ö±êÌâ // Ìí¼Ó¸÷ÖÖÎı¾×Ö¶ÎÉèÖà addTextFieldSetting("SYSTEM_NAME", "systemName", 30, false, "^.{1,30}$"); addTextFieldSetting("DATABASE_NAME", "databaseName", 20, false, "^.{1,20}$"); addPasswordFieldSetting("DATABASE_PASSWORD", "databasePassword", 20); addTextFieldSetting("HEX_UDP_PORT", "hexDataUdpReceivePort", 6, true, "^\\d{1,5}$"); addTextFieldSetting("ASCII_UDP_PORT", "asciiDataUdpReceivePort", 6, true, "^\\d{1,5}$"); addTextFieldSetting("TCP_PORT1", "tcpDataReceivePort1", 6, true, "^\\d{1,5}$"); addTextFieldSetting("TCP_PORT2", "tcpDataReceivePort2", 6, true, "^\\d{1,5}$"); addTextFieldSetting("UDP_SEND_PORT", "udpSendPort", 6, true, "^\\d{1,5}$"); addTextFieldSetting("SERVER_ADDRESS", "serverAddress", 15, false, "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); } // ´´½¨Î»ÖÃÉèÖò¿·Ö private void createPositionSettings() { addSectionTitle(messages.getString("POSITION_SETTINGS")); // Ìí¼Ó²¿·Ö±êÌâ // Ìí¼Ó¸÷ÖÖÎı¾×Ö¶ÎÉèÖà addTextFieldSetting("SMOOTHING_DURATION", "locationSmoothingDuration", 6, true, "^\\d{1,4}$"); addTextFieldSetting("FORWARDING_INTERVAL", "locationForwardingInterval", 6, true, "^\\d{1,4}$"); addTextFieldSetting("TRAJECTORY_SAVE_DAYS", "trajectorySaveDays", 6, true, "^\\d{1,3}$"); addTextFieldSetting("TAG_OFFLINE_DURATION", "tagOfflineJudgmentDuration", 6, true, "^\\d{1,4}$"); addTextFieldSetting("BASE_OFFLINE_DURATION", "baseStationOfflineJudgmentDuration", 6, true, "^\\d{1,4}$"); addTextFieldSetting("TCP_HEARTBEAT_TIME", "tcpHeartbeatReplyTime", 6, true, "^\\d{1,4}$"); addCheckboxSetting("SELF_RESOLUTION_MODE", "selfResolutionMode"); // Ìí¼Ó¸´Ñ¡¿òÉèÖà addTextFieldSetting("POWER_ISSUE_LIMIT", "electricityIssueLimit", 6, true, "^\\d{1,3}$"); addTextFieldSetting("DISORDER_RETROSPECTION", "disorderRetrospection", 6, true, "^\\d{1,4}$"); addTextFieldSetting("VIBRATION_DURATION", "vibrationDuration", 6, true, "^\\d{1,4}$"); addTextFieldSetting("URT_FENCE_SPACING", "urtFenceSpacing", 6, true, "^\\d{1,4}$"); addTextFieldSetting("BS_DATA_FLAG", "bsDataForwardingFlag", 10, false, "^[a-zA-Z0-9]{1,10}$"); addTextFieldSetting("CS_RECEIVE_PASSWORD", "csEndReceivePassword", 10, false, "^[a-zA-Z0-9]{1,10}$"); addTextFieldSetting("ATTENDANCE_SAVE_DAYS", "attendanceSaveDays", 6, true, "^\\d{1,3}$"); addTextFieldSetting("ELECTRICITY_SAVE_DAYS", "electricitySaveDays", 6, true, "^\\d{1,3}$"); } // ´´½¨ÏÔʾÉèÖò¿·Ö private void createDisplaySettings() { addSectionTitle(messages.getString("DISPLAY_SETTINGS")); // Ìí¼Ó²¿·Ö±êÌâ // Ìí¼Ó¸÷ÖÖÎı¾×Ö¶ÎÉèÖà addTextFieldSetting("HORIZONTAL_PRECISION_FILTER", "horizontalPrecisionFactorFilter", 6, true, "^\\d{1,3}$"); addTextFieldSetting("SATELLITE_NUMBER_FILTER", "satelliteNumberFilter", 6, true, "^\\d{1,2}$"); addTextFieldSetting("ENV_ATTENUATION_FACTOR", "environmentalAttenuationFactor", 6, true, "^\\d{1,3}$"); addTextFieldSetting("SIGNAL_STRENGTH", "signalStrength", 10, true, "^\\d{1,3}$"); // Ìí¼Ó¸÷ÖÖ¸´Ñ¡¿òÉèÖà addCheckboxSetting("WIRE_FREE_MODE", "wireFreeMode"); addCheckboxSetting("BASE_WIFI_MODE", "baseStationWifiMode"); addCheckboxSetting("3D_POSITIONING_MODE", "threeDimensionalPositioningMode"); addCheckboxSetting("BS_ENGINE_MODE", "doBsEngine"); addCheckboxSetting("BACKGROUND_DISPLAY", "backgroundDisplay"); addCheckboxSetting("ENGINE_HEARTBEAT", "enableEngineHeartbeatPacket"); addCheckboxSetting("SELF_DIFFERENTIAL_BASE", "selfDifferentialBaseStation"); addCheckboxSetting("XY_TO_GNSS", "xyToGnss"); addCheckboxSetting("GNSS_TO_XY", "gnssToXy"); addCheckboxSetting("UDP_LISTENING", "enableUdpListening"); addCheckboxSetting("NAV_ICON_DISPLAY", "displayNavigationBarIcon"); addCheckboxSetting("URT_BASE_MODE", "urtDoBaseStationMode"); addCheckboxSetting("OTHER_LAYERS_IN_0", "otherLayersDisplayIn0Layer"); addCheckboxSetting("AUTO_TIME_SLICE", "automaticTimeSliceMode"); addCheckboxSetting("TAG_COORD_DISPLAY", "tagCoordinateDisplay"); addCheckboxSetting("TAG_NUMBER_DISPLAY", "displayTagNumber"); addCheckboxSetting("BASE_NUMBER_DISPLAY", "displayBaseStationNumber"); addCheckboxSetting("BASE_ICON_DISPLAY", "displayBaseStationIcon"); addCheckboxSetting("BASE_COORD_DISPLAY", "displayBaseStationCoordinates"); addCheckboxSetting("REALTIME_POWER_DISPLAY", "realTimeElectricityDisplay"); addCheckboxSetting("ATTENDANCE_DISAPPEAR", "attendanceGoDisappear"); addCheckboxSetting("DISPLAY_NAME", "displayName"); addCheckboxSetting("OFFLINE_DISAPPEAR", "offlineDisappear"); addCheckboxSetting("TUNNEL_POSITIONING", "tunnelPositioning"); addCheckboxSetting("ENABLE_TRAJECTORY_SAVE", "enableTrajectorySave"); addCheckboxSetting("GAS_DISPLAY", "enableGasDisplay"); addCheckboxSetting("BASE_UPGRADE_MODE", "baseStationUpgradeMode"); addCheckboxSetting("RANGING_JUDGMENT_ONLINE", "rangingJudgmentOnline"); addCheckboxSetting("READ_COMM_GROUP", "readBaseStationCommunicationGroup"); addCheckboxSetting("DISPLAY_FENCE", "displayFence"); addCheckboxSetting("STATIC_COORD_FIX", "staticCoordinateFix"); // Ìí¼ÓÎı¾×Ö¶ÎÉèÖà addTextFieldSetting("REALTIME_TRAJECTORY_DURATION", "realTimeTrajectoryDuration", 6, true, "^\\d{1,4}$"); // Ìí¼Ó×éºÏ¿òÉèÖà addComboBoxSetting("REALTIME_TRAJECTORY_COLOR", "realTimeTrajectoryColor", new String[]{"ºìÉ«", "ÂÌÉ«", "ºÚÉ«", "×ÏÉ«", "»ÆÉ«"}, new Integer[]{1, 2, 3, 4, 5}); addComboBoxSetting("SYSTEM_LANGUAGE", "systemLanguage", new String[]{"ÖÐÎÄ", "Ó¢ÎÄ", "·¨ÎÄ", "°¢À­²®ÎÄ"}, new Integer[]{1, 2, 3, 4}); // Ìí¼Ó¸´Ñ¡¿òÉèÖà addCheckboxSetting("JNA_LOG_SWITCH", "jnaLogSwitch"); // Ìí¼Óµ¥Ñ¡°´Å¥ÉèÖà addRadioButtonSetting("TIME_SLICE_METHOD", "timeSliceDistributionMethod", new String[]{"MIX", "GAP"}, new Integer[]{1, 2}); // Ìí¼ÓÎı¾×Ö¶ÎÉèÖà addTextFieldSetting("LOW_POWER_WARNING", "lowElectricityWarningValue", 6, true, "^\\d{1,3}$"); addTextFieldSetting("SYSTEM_COMPANY", "systemBelongingCompany", 30, false, "^.{1,30}$"); } // ´´½¨ÆäËûÉèÖò¿·Ö private void createOtherSettings() { // ÆäËûÉèÖÃÒѰüº¬ÔÚÖ÷ÒªÀà±ðÖÐ } // Ìí¼Ó²¿·Ö±êÌâ private void addSectionTitle(String title) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; // ÁÐË÷Òý0 gbc.gridy = gridy++; // ÐÐË÷Òý²¢×ÔÔö gbc.gridwidth = 4; // ¿ç4ÁÐ gbc.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbc.insets = new Insets(20, 0, 5, 0); // Éϱ߾à20£¬Ï±߾à5 gbc.fill = GridBagConstraints.HORIZONTAL; // ˮƽÌî³ä JLabel sectionLabel = new JLabel(title); sectionLabel.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 16)); // ÉèÖÃ×ÖÌå add(sectionLabel, gbc); // Ìí¼Ó±êÇ© } // Ìí¼ÓÎı¾×Ö¶ÎÉèÖà private void addTextFieldSetting(String labelKey, String componentKey, int maxLength, boolean numeric, String regex) { // ÐòºÅÁÐ GridBagConstraints gbcIndex = new GridBagConstraints(); gbcIndex.gridx = 0; // ÁÐË÷Òý0 gbcIndex.gridy = gridy; // ÐÐË÷Òý gbcIndex.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcIndex.insets = new Insets(5, 5, 5, 10); // ±ß¾à JLabel indexLabel = new JLabel(settingIndex + "."); indexLabel.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 14)); // ÉèÖÃ×ÖÌå add(indexLabel, gbcIndex); // Ìí¼ÓÐòºÅ±êÇ© // ±êÌâÁÐ GridBagConstraints gbcTitle = new GridBagConstraints(); gbcTitle.gridx = 1; // ÁÐË÷Òý1 gbcTitle.gridy = gridy; // ÐÐË÷Òý gbcTitle.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcTitle.insets = new Insets(5, 0, 5, 10); // ±ß¾à JLabel titleLabel = new JLabel(messages.getString(labelKey)); titleLabel.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14)); // ÉèÖÃ×ÖÌå add(titleLabel, gbcTitle); // Ìí¼Ó±êÌâ±êÇ© // ÊäÈë×é¼þÁÐ GridBagConstraints gbcField = new GridBagConstraints(); gbcField.gridx = 2; // ÁÐË÷Òý2 gbcField.gridy = gridy; // ÐÐË÷Òý gbcField.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcField.insets = new Insets(5, 0, 5, 10); // ±ß¾à JTextField textField = new JTextField(); textField.setPreferredSize(new Dimension(300, 30)); // ÉèÖÃÊ×Ñ¡´óС textField.setDocument(new SystemSettingsPanel.LimitedLengthDocument(maxLength)); // ÉèÖó¤¶ÈÏÞÖÆ // Èç¹ûÊÇÊý×Ö×ֶΣ¬Ìí¼ÓÑéÖ¤Æ÷ if (numeric) { textField.setInputVerifier(new SystemSettingsPanel.NumericVerifier()); } textField.putClientProperty("regex", regex); // ÉèÖÃÕýÔò±í´ïʽ // ´ÓÅäÖüÓÔØ³õʼֵ String initialValue = getConfigField(componentKey); if (initialValue != null) { textField.setText(initialValue); } else { textField.setText(""); // Ã÷È·ÉèÖÃΪ¿Õ×Ö·û´® } settingComponents.put(componentKey, textField); // Ìí¼Óµ½×é¼þÓ³Éä add(textField, gbcField); // Ìí¼ÓÎı¾×Ö¶Î // °´Å¥ÁÐ GridBagConstraints gbcButton = new GridBagConstraints(); gbcButton.gridx = 3; // ÁÐË÷Òý3 gbcButton.gridy = gridy; // ÐÐË÷Òý gbcButton.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcButton.insets = new Insets(5, 0, 5, 5); // ±ß¾à JButton confirmButton = new JButton(messages.getString("SAVE")); // "±£´æ"°´Å¥ confirmButton.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 12)); // ÉèÖÃ×ÖÌå confirmButton.setPreferredSize(new Dimension(80, 30)); // ÉèÖôóС // Ìí¼Ó±£´æ²Ù×÷¼àÌýÆ÷ confirmButton.addActionListener(e -> validateAndSaveSetting(textField, regex, labelKey, componentKey)); add(confirmButton, gbcButton); // Ìí¼Ó°´Å¥ settingIndex++; // ÉèÖÃÏîË÷Òý×ÔÔö gridy++; // ÐÐË÷Òý×ÔÔö } // Ìí¼ÓÃÜÂë×Ö¶ÎÉèÖà private void addPasswordFieldSetting(String labelKey, String componentKey, int maxLength) { // ÐòºÅÁÐ GridBagConstraints gbcIndex = new GridBagConstraints(); gbcIndex.gridx = 0; // ÁÐË÷Òý0 gbcIndex.gridy = gridy; // ÐÐË÷Òý gbcIndex.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcIndex.insets = new Insets(5, 5, 5, 10); // ±ß¾à JLabel indexLabel = new JLabel(settingIndex + "."); indexLabel.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 14)); // ÉèÖÃ×ÖÌå add(indexLabel, gbcIndex); // Ìí¼ÓÐòºÅ±êÇ© // ±êÌâÁÐ GridBagConstraints gbcTitle = new GridBagConstraints(); gbcTitle.gridx = 1; // ÁÐË÷Òý1 gbcTitle.gridy = gridy; // ÐÐË÷Òý gbcTitle.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcTitle.insets = new Insets(5, 0, 5, 10); // ±ß¾à JLabel titleLabel = new JLabel(messages.getString(labelKey)); titleLabel.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14)); // ÉèÖÃ×ÖÌå add(titleLabel, gbcTitle); // Ìí¼Ó±êÌâ±êÇ© // ÊäÈë×é¼þÁÐ GridBagConstraints gbcField = new GridBagConstraints(); gbcField.gridx = 2; // ÁÐË÷Òý2 gbcField.gridy = gridy; // ÐÐË÷Òý gbcField.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcField.insets = new Insets(5, 0, 5, 10); // ±ß¾à JPasswordField passwordField = new JPasswordField(); passwordField.setPreferredSize(new Dimension(300, 30)); // ÉèÖôóС // ÉèÖó¤¶ÈÏÞÖÆºÍ»ØÏÔ×Ö·û passwordField.setDocument(new SystemSettingsPanel.LimitedLengthDocument(maxLength)); passwordField.setEchoChar('*'); // ´ÓÅäÖüÓÔØ³õʼֵ String initialValue = getConfigField(componentKey); if (initialValue != null) { passwordField.setText(initialValue); } settingComponents.put(componentKey, passwordField); // Ìí¼Óµ½×é¼þÓ³Éä add(passwordField, gbcField); // Ìí¼ÓÃÜÂë×Ö¶Î // °´Å¥ÁÐ GridBagConstraints gbcButton = new GridBagConstraints(); gbcButton.gridx = 3; // ÁÐË÷Òý3 gbcButton.gridy = gridy; // ÐÐË÷Òý gbcButton.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcButton.insets = new Insets(5, 0, 5, 5); // ±ß¾à JButton confirmButton = new JButton(messages.getString("SAVE")); // "±£´æ"°´Å¥ confirmButton.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 12)); // ÉèÖÃ×ÖÌå confirmButton.setPreferredSize(new Dimension(80, 30)); // ÉèÖôóС // Ìí¼Ó±£´æ²Ù×÷¼àÌýÆ÷ confirmButton.addActionListener(e -> { char[] password = passwordField.getPassword(); // ¼ì²éÃÜÂëÊÇ·ñΪ¿Õ if (password.length == 0) { JOptionPane.showMessageDialog(this, messages.getString("FIELD_REQUIRED"), "ÑéÖ¤´íÎó", JOptionPane.ERROR_MESSAGE); return; } saveSetting(componentKey, new String(password)); // ±£´æÉèÖà // ÏÔʾ³É¹¦ÏûÏ¢ JOptionPane optionPane = new JOptionPane(messages.getString("SAVE_SETTING_SUCCESS"), JOptionPane.INFORMATION_MESSAGE); JDialog dialog = optionPane.createDialog(this, messages.getString("SUCCESS")); dialog.setLocationRelativeTo(null); // ¾ÓÖÐÏÔʾ dialog.setVisible(true); }); add(confirmButton, gbcButton); // Ìí¼Ó°´Å¥ settingIndex++; // ÉèÖÃÏîË÷Òý×ÔÔö gridy++; // ÐÐË÷Òý×ÔÔö } // Ìí¼Ó¸´Ñ¡¿òÉèÖà private void addCheckboxSetting(String labelKey, String componentKey) { // ÐòºÅÁÐ GridBagConstraints gbcIndex = new GridBagConstraints(); gbcIndex.gridx = 0; // ÁÐË÷Òý0 gbcIndex.gridy = gridy; // ÐÐË÷Òý gbcIndex.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcIndex.insets = new Insets(5, 5, 5, 10); // ±ß¾à JLabel indexLabel = new JLabel(settingIndex + "."); indexLabel.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 14)); // ÉèÖÃ×ÖÌå add(indexLabel, gbcIndex); // Ìí¼ÓÐòºÅ±êÇ© // ±êÌâÁÐ GridBagConstraints gbcTitle = new GridBagConstraints(); gbcTitle.gridx = 1; // ÁÐË÷Òý1 gbcTitle.gridy = gridy; // ÐÐË÷Òý gbcTitle.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcTitle.insets = new Insets(5, 0, 5, 10); // ±ß¾à JLabel titleLabel = new JLabel(messages.getString(labelKey)); titleLabel.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14)); // ÉèÖÃ×ÖÌå // ³õʼ±êÌâÑÕÉ« add(titleLabel, gbcTitle); // Ìí¼Ó±êÌâ±êÇ© // ÊäÈë×é¼þÁÐ GridBagConstraints gbcField = new GridBagConstraints(); gbcField.gridx = 2; // ÁÐË÷Òý2 gbcField.gridy = gridy; // ÐÐË÷Òý gbcField.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcField.insets = new Insets(5, 0, 5, 10); // ±ß¾à JCheckBox checkBox = new JCheckBox(); // ´ÓÅäÖüÓÔØ³õʼֵ String initialValue = getConfigField(componentKey); if (initialValue != null && !initialValue.isEmpty()) { boolean isSelected = "1".equals(initialValue); checkBox.setSelected(isSelected); // ¸ù¾ÝÑ¡ÖÐ״̬ÉèÖÃÑÕÉ« titleLabel.setForeground(isSelected ? Color.RED : Color.BLACK); } else { titleLabel.setForeground(Color.BLACK); } // Ìí¼Ó״̬¸ü¸Ä¼àÌýÆ÷ checkBox.addItemListener(e -> { boolean isSelected = checkBox.isSelected(); // ¸ù¾ÝÑ¡ÖÐ״̬¸üбêÌâÑÕÉ« titleLabel.setForeground(isSelected ? Color.RED : Color.BLACK); }); settingComponents.put(componentKey, checkBox); // Ìí¼Óµ½×é¼þÓ³Éä add(checkBox, gbcField); // Ìí¼Ó¸´Ñ¡¿ò // °´Å¥ÁÐ GridBagConstraints gbcButton = new GridBagConstraints(); gbcButton.gridx = 3; // ÁÐË÷Òý3 gbcButton.gridy = gridy; // ÐÐË÷Òý gbcButton.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcButton.insets = new Insets(5, 0, 5, 5); // ±ß¾à JButton confirmButton = new JButton(messages.getString("SAVE")); // "±£´æ"°´Å¥ confirmButton.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 12)); // ÉèÖÃ×ÖÌå confirmButton.setPreferredSize(new Dimension(80, 30)); // ÉèÖôóС // Ìí¼Ó±£´æ²Ù×÷¼àÌýÆ÷ confirmButton.addActionListener(e -> { saveSetting(componentKey, checkBox.isSelected() ? "1" : "0"); // ±£´æÉèÖà // ÏÔʾ³É¹¦ÏûÏ¢ JOptionPane optionPane = new JOptionPane(messages.getString("SAVE_SETTING_SUCCESS"), JOptionPane.INFORMATION_MESSAGE); JDialog dialog = optionPane.createDialog(this, messages.getString("SUCCESS")); dialog.setLocationRelativeTo(null); // ¾ÓÖÐÏÔʾ dialog.setVisible(true); }); add(confirmButton, gbcButton); // Ìí¼Ó°´Å¥ settingIndex++; // ÉèÖÃÏîË÷Òý×ÔÔö gridy++; // ÐÐË÷Òý×ÔÔö } // Ìí¼Ó×éºÏ¿òÉèÖà private void addComboBoxSetting(String labelKey, String componentKey, String[] options, Integer[] values) { // ÐòºÅÁÐ GridBagConstraints gbcIndex = new GridBagConstraints(); gbcIndex.gridx = 0; // ÁÐË÷Òý0 gbcIndex.gridy = gridy; // ÐÐË÷Òý gbcIndex.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcIndex.insets = new Insets(5, 5, 5, 10); // ±ß¾à JLabel indexLabel = new JLabel(settingIndex + "."); indexLabel.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 14)); // ÉèÖÃ×ÖÌå add(indexLabel, gbcIndex); // Ìí¼ÓÐòºÅ±êÇ© // ±êÌâÁÐ GridBagConstraints gbcTitle = new GridBagConstraints(); gbcTitle.gridx = 1; // ÁÐË÷Òý1 gbcTitle.gridy = gridy; // ÐÐË÷Òý gbcTitle.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcTitle.insets = new Insets(5, 0, 5, 10); // ±ß¾à JLabel titleLabel = new JLabel(messages.getString(labelKey)); titleLabel.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14)); // ÉèÖÃ×ÖÌå add(titleLabel, gbcTitle); // Ìí¼Ó±êÌâ±êÇ© // ÊäÈë×é¼þÁÐ GridBagConstraints gbcField = new GridBagConstraints(); gbcField.gridx = 2; // ÁÐË÷Òý2 gbcField.gridy = gridy; // ÐÐË÷Òý gbcField.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcField.insets = new Insets(5, 0, 5, 10); // ±ß¾à JComboBox comboBox = new JComboBox<>(options); comboBox.setPreferredSize(new Dimension(300, 30)); // ÉèÖôóС // ÉèÖÃäÖȾÆ÷ comboBox.setRenderer(new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); setPreferredSize(new Dimension(300, 25)); // ÉèÖÃäÖȾ´óС return this; } }); comboBox.putClientProperty("values", values); // ÉèÖÃÖµÊý×é // ´ÓÅäÖüÓÔØ³õʼֵ String initialValue = getConfigField(componentKey); if (initialValue != null && !initialValue.isEmpty()) { int selectedIndex = 0; for (int i = 0; i < values.length; i++) { if (values[i].toString().equals(initialValue)) { selectedIndex = i; break; } } comboBox.setSelectedIndex(selectedIndex); // ÉèÖÃÑ¡ÖÐÏî } settingComponents.put(componentKey, comboBox); // Ìí¼Óµ½×é¼þÓ³Éä add(comboBox, gbcField); // Ìí¼Ó×éºÏ¿ò // °´Å¥ÁÐ GridBagConstraints gbcButton = new GridBagConstraints(); gbcButton.gridx = 3; // ÁÐË÷Òý3 gbcButton.gridy = gridy; // ÐÐË÷Òý gbcButton.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcButton.insets = new Insets(5, 0, 5, 5); // ±ß¾à JButton confirmButton = new JButton(messages.getString("SAVE")); // "±£´æ"°´Å¥ confirmButton.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 12)); // ÉèÖÃ×ÖÌå confirmButton.setPreferredSize(new Dimension(80, 30)); // ÉèÖôóС // Ìí¼Ó±£´æ²Ù×÷¼àÌýÆ÷ confirmButton.addActionListener(e -> { int selectedIndex = comboBox.getSelectedIndex(); Integer value = values[selectedIndex]; saveSetting(componentKey, value.toString()); // ±£´æÉèÖà // ÏÔʾ³É¹¦ÏûÏ¢ JOptionPane optionPane = new JOptionPane(messages.getString("SAVE_SETTING_SUCCESS"), JOptionPane.INFORMATION_MESSAGE); JDialog dialog = optionPane.createDialog(this, messages.getString("SUCCESS")); dialog.setLocationRelativeTo(null); // ¾ÓÖÐÏÔʾ dialog.setVisible(true); }); add(confirmButton, gbcButton); // Ìí¼Ó°´Å¥ settingIndex++; // ÉèÖÃÏîË÷Òý×ÔÔö gridy++; // ÐÐË÷Òý×ÔÔö } // Ìí¼Óµ¥Ñ¡°´Å¥ÉèÖà private void addRadioButtonSetting(String labelKey, String componentKey, String[] options, Integer[] values) { // ÐòºÅÁÐ GridBagConstraints gbcIndex = new GridBagConstraints(); gbcIndex.gridx = 0; // ÁÐË÷Òý0 gbcIndex.gridy = gridy; // ÐÐË÷Òý gbcIndex.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcIndex.insets = new Insets(5, 5, 5, 10); // ±ß¾à JLabel indexLabel = new JLabel(settingIndex + "."); indexLabel.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 14)); // ÉèÖÃ×ÖÌå add(indexLabel, gbcIndex); // Ìí¼ÓÐòºÅ±êÇ© // ±êÌâÁÐ GridBagConstraints gbcTitle = new GridBagConstraints(); gbcTitle.gridx = 1; // ÁÐË÷Òý1 gbcTitle.gridy = gridy; // ÐÐË÷Òý gbcTitle.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcTitle.insets = new Insets(5, 0, 5, 10); // ±ß¾à JLabel titleLabel = new JLabel(messages.getString(labelKey)); titleLabel.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14)); // ÉèÖÃ×ÖÌå add(titleLabel, gbcTitle); // Ìí¼Ó±êÌâ±êÇ© // ÊäÈë×é¼þÁÐ GridBagConstraints gbcField = new GridBagConstraints(); gbcField.gridx = 2; // ÁÐË÷Òý2 gbcField.gridy = gridy; // ÐÐË÷Òý gbcField.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcField.insets = new Insets(5, 0, 5, 10); // ±ß¾à ButtonGroup group = new ButtonGroup(); // °´Å¥×é JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0)); // µ¥Ñ¡°´Å¥Ãæ°å radioPanel.setPreferredSize(new Dimension(300, 30)); // ÉèÖôóС // ´ÓÅäÖüÓÔØ³õʼֵ String initialValue = getConfigField(componentKey); int selectedIndex = 0; if (initialValue != null && !initialValue.isEmpty()) { for (int i = 0; i < values.length; i++) { if (values[i].toString().equals(initialValue)) { selectedIndex = i; break; } } } // ´´½¨µ¥Ñ¡°´Å¥ for (int i = 0; i < options.length; i++) { JRadioButton radioButton = new JRadioButton(options[i]); radioButton.putClientProperty("value", values[i]); // ÉèÖùØÁªÖµ group.add(radioButton); // Ìí¼Óµ½°´Å¥×é radioPanel.add(radioButton); // Ìí¼Óµ½Ãæ°å if (i == selectedIndex) { radioButton.setSelected(true); // ÉèÖÃÑ¡ÖÐ״̬ } } settingComponents.put(componentKey, radioPanel); // Ìí¼Óµ½×é¼þÓ³Éä add(radioPanel, gbcField); // Ìí¼Óµ¥Ñ¡°´Å¥Ãæ°å // °´Å¥ÁÐ GridBagConstraints gbcButton = new GridBagConstraints(); gbcButton.gridx = 3; // ÁÐË÷Òý3 gbcButton.gridy = gridy; // ÐÐË÷Òý gbcButton.anchor = GridBagConstraints.WEST; // ×ó¶ÔÆë gbcButton.insets = new Insets(5, 0, 5, 5); // ±ß¾à JButton confirmButton = new JButton(messages.getString("SAVE")); // "±£´æ"°´Å¥ confirmButton.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 12)); // ÉèÖÃ×ÖÌå confirmButton.setPreferredSize(new Dimension(80, 30)); // ÉèÖôóС // Ìí¼Ó±£´æ²Ù×÷¼àÌýÆ÷ confirmButton.addActionListener(e -> { for (Component comp : radioPanel.getComponents()) { if (comp instanceof JRadioButton) { JRadioButton radioButton = (JRadioButton) comp; if (radioButton.isSelected()) { Integer value = (Integer) radioButton.getClientProperty("value"); saveSetting(componentKey, value.toString()); // ±£´æÉèÖà // ÏÔʾ³É¹¦ÏûÏ¢ JOptionPane optionPane = new JOptionPane(messages.getString("SAVE_SETTING_SUCCESS"), JOptionPane.INFORMATION_MESSAGE); JDialog dialog = optionPane.createDialog(this, messages.getString("SUCCESS")); dialog.setLocationRelativeTo(null); // ¾ÓÖÐÏÔʾ dialog.setVisible(true); return; } } } // Èç¹ûûÓÐÑ¡ÔñÈκÎÑ¡ÏÏÔʾ´íÎóÏûÏ¢ JOptionPane.showMessageDialog(this, messages.getString("SELECT_OPTION"), "´íÎó", JOptionPane.ERROR_MESSAGE); }); add(confirmButton, gbcButton); // Ìí¼Ó°´Å¥ settingIndex++; // ÉèÖÃÏîË÷Òý×ÔÔö gridy++; // ÐÐË÷Òý×ÔÔö } // ÑéÖ¤²¢±£´æÉèÖà private void validateAndSaveSetting(JTextField textField, String regex, String labelKey, String componentKey) { String value = textField.getText().trim(); // ¼ì²é×Ö¶ÎÊÇ·ñΪ¿Õ if (value.isEmpty()) { JOptionPane.showMessageDialog(this, messages.getString(labelKey) + messages.getString("FIELD_REQUIRED"), messages.getString("VALIDATION_ERROR"), JOptionPane.ERROR_MESSAGE); return; } // ¼ì²é×ֶθñʽ if (regex != null && !Pattern.matches(regex, value)) { String errorMessage = messages.getString("INVALID_INPUT"); // ¸ù¾ÝÕýÔò±í´ïʽÀàÐÍÉèÖÃÌØ¶¨´íÎóÏûÏ¢ if (regex.equals("^\\d{1,5}$")) { errorMessage = messages.getString("INVALID_PORT"); } else if (regex.equals("^\\d{1,4}$")) { errorMessage = messages.getString("INVALID_NUMBER"); } else if (regex.equals("^\\d{1,3}$")) { errorMessage = messages.getString("INVALID_NUMBER_1_999"); } else if (regex.equals("^\\d{1,2}$")) { errorMessage = messages.getString("INVALID_NUMBER_1_99"); } else if (regex.equals("^[a-zA-Z0-9]{1,10}$")) { errorMessage = messages.getString("INVALID_ALPHANUMERIC"); } else if (regex.equals("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) { errorMessage = messages.getString("INVALID_IP"); } JOptionPane.showMessageDialog(this, messages.getString(labelKey) + ": " + errorMessage, messages.getString("VALIDATION_ERROR"), JOptionPane.ERROR_MESSAGE); return; } // ʹÓø¸Ãæ°å±£´æÉèÖà if (parentPanel != null) { parentPanel.saveSetting(componentKey, value); } else { saveSetting(componentKey, value); } // ÏÔʾ³É¹¦ÏûÏ¢ JOptionPane optionPane = new JOptionPane(messages.getString("SAVE_SETTING_SUCCESS"), JOptionPane.INFORMATION_MESSAGE); JDialog dialog = optionPane.createDialog(this, messages.getString("SUCCESS")); dialog.setLocationRelativeTo(null); // ¾ÓÖÐÏÔʾ dialog.setVisible(true); } // »ñÈ¡ÅäÖÃ×Ö¶ÎÖµ private String getConfigField(String fieldName) { try { // ¹¹½¨getter·½·¨Ãû String getterName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); // »ñÈ¡getter·½·¨ Method getter = SystemConfiguration.class.getMethod(getterName); // µ÷ÓÃgetter·½·¨»ñȡֵ return (String) getter.invoke(currentConfig); } catch (NoSuchMethodException e) { // ·½·¨²»´æÔÚʱ·µ»Ø¿Õ×Ö·û´® System.err.println("Warning: Method not found: " + fieldName); return ""; } catch (Exception e) { e.printStackTrace(); return ""; } } // ±£´æÉèÖà private void saveSetting(String key, String value) { try { // ¹¹½¨setter·½·¨Ãû String setterName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); // »ñÈ¡setter·½·¨ Method setter = SystemConfiguration.class.getMethod(setterName, String.class); // µ÷ÓÃsetter·½·¨ÉèÖÃÖµ setter.invoke(currentConfig, value); } catch (Exception e) { e.printStackTrace(); } } }