package home; import javax.swing.*; import java.awt.*; import java.util.Locale; import java.util.ResourceBundle; import java.io.InputStream; import java.io.IOException; import java.io.File; import java.io.FileInputStream; import java.util.PropertyResourceBundle; import javax.imageio.ImageIO; public class MainFrame extends JFrame { private static final long serialVersionUID = 1L; private ResourceBundle messages; private Locale currentLocale; private CardLayout cardLayout; private JPanel mainPanel; private JButton serialCommBtn, networkCommBtn, networkConfigBtn; private JComboBox languageCombo; // 新增:卫星数据按钮与面板 private JButton satelliteBtn; private SatelliteDataPanel satellitePanel; // 各功能面板 private SerialCommunicationPanel serialPanel; private NetworkCommunicationPanel networkPanel; private NetworkConfigPanel configPanel; private DataLogPanel dataLogPanel; private SendPanel sendPanel; private JSplitPane contentSplitPane; private JSplitPane rightSplitPane; // 新增:快捷计算面板相关 private JButton quickCalcBtn; private JDialog quickCalcDialog; private QuickCalculationPanel quickCalcPanel; public MainFrame() { this.currentLocale = Locale.SIMPLIFIED_CHINESE; this.messages = loadResourceBundle(currentLocale); initializeUI(); } private ResourceBundle loadResourceBundle(Locale locale) { String fileName = locale.equals(Locale.ENGLISH) ? "Messages_en.properties" : "Messages_zh.properties"; File langFile = new File("systemfile/" + fileName); if (!langFile.exists()) { System.err.println("默认资源文件未找到: " + langFile.getAbsolutePath()); // 备用方案:尝试从类路径加载 try { return ResourceBundle.getBundle("Messages", locale); } catch (Exception e) { System.err.println("无法加载备用资源文件"); return createDefaultResourceBundle(); } } try (InputStream inputStream = new FileInputStream(langFile)) { return new PropertyResourceBundle(inputStream); } catch (IOException e) { System.err.println("无法加载资源文件: " + e.getMessage()); // 备用方案 try { return ResourceBundle.getBundle("Messages", locale); } catch (Exception ex) { System.err.println("无法加载备用资源文件"); return createDefaultResourceBundle(); } } } private ResourceBundle createDefaultResourceBundle() { // 创建默认资源包(中文) java.util.Properties properties = new java.util.Properties(); properties.setProperty("app.title", "配置软件"); properties.setProperty("serial.communication", "串口通信"); properties.setProperty("network.communication", "网络通信"); properties.setProperty("network.config", "网络配置"); properties.setProperty("language.switch", "语言切换"); properties.setProperty("select.serial", "选择串口"); properties.setProperty("baud.rate", "波特率"); properties.setProperty("open", "打开"); properties.setProperty("close", "关闭"); // 新增:快捷计算相关资源 properties.setProperty("quick.calculation", "快捷计算"); properties.setProperty("quick.calc.title", "坐标计算工具"); try { return new PropertyResourceBundle(new java.io.ByteArrayInputStream( properties.toString().replace("=", ": ").getBytes())); } catch (IOException e) { e.printStackTrace(); } return messages; } public String getString(String key) { if (messages != null) { try { return messages.getString(key); } catch (Exception e) { // 如果资源文件中没有找到对应的key,返回默认值 return getDefaultString(key); } } return getDefaultString(key); } private String getDefaultString(String key) { // 默认文本(中文) switch (key) { case "app.title": return "配置软件"; case "serial.communication": return "串口通信"; case "network.communication": return "网络通信"; case "network.config": return "网络配置"; case "language.switch": return "语言切换"; case "select.serial": return "选择串口"; case "baud.rate": return "波特率"; case "open": return "打开"; case "close": return "关闭"; case "base.parameters": return "基础参数"; case "base.station": return "基站"; case "tag": return "标签"; case "anti.collision": return "防撞"; case "upgrade": return "升级"; case "device.version": return "设备版本"; case "device.id": return "设备编号"; case "comm.frequency": return "通信频率"; case "base.stations.per.comm": return "单次通信基站数"; case "group.id": return "分组编号"; case "distance.calibration": return "距离校准值"; case "device.type": return "设备类型"; case "base.station.ranging": return "基站主动测距"; case "alarm.device": return "报警设备"; case "pairing.id": return "配对编号"; case "heartbeat.switch": return "心跳包开关"; case "modbus.mode": return "MODBUS模式"; case "rf.power": return "射频发射功率"; case "read.parameters": return "读取参数"; case "save.parameters": return "保存参数"; case "restart.device": return "重启设备"; case "reset.factory": return "恢复出厂设置"; case "protocol.type": return "协议类型"; case "local.host.address": return "本地主机地址"; case "local.host.port": return "本地主机端口"; case "remote.host.address": return "远程主机地址"; case "remote.host.port": return "远程主机端口"; case "data.log": return "数据日志"; case "ascii.display": return "ASCII显示"; case "auto.save": return "自动保存"; case "show.time": return "显示时间"; case "start": return "开始"; case "pause": return "暂停"; case "clear": return "清空"; case "send.data": return "发送数据"; case "hex.send": return "HEX发送"; case "loop.send": return "循环发送"; case "loop.time": return "循环时间(ms)"; case "send": return "发送"; case "extension": return "扩展"; case "LANGUAGE": return "Language"; case "external.control": return "外部控制"; case "adjacent.stations.count": return "相邻基站数量"; case "adjacent.station1": return "相邻基站1"; case "adjacent.station2": return "相邻基站2"; case "adjacent.station3": return "相邻基站3"; case "adjacent.station4": return "相邻基站4"; case "adjacent.station5": return "相邻基站5"; case "adjacent.station6": return "相邻基站6"; case "adjacent.station7": return "相邻基站7"; case "adjacent.station8": return "相邻基站8"; case "adjacent.station9": return "相邻基站9"; case "adjacent.station10": return "相邻基站10"; case "sync.station.id": return "同步基站ID"; case "sync.station.type": return "同步基站类型"; case "tag.id": return "标签ID"; case "tag.type": return "标签类型"; case "tag.mode": return "标签模式"; case "tag.interval": return "标签间隔"; case "tag.power": return "标签功率"; case "tag.sensitivity": return "标签灵敏度"; case "anti.collision.enable": return "防撞使能"; case "safety.distance": return "安全距离"; case "warning.distance": return "警告距离"; case "alarm.distance": return "报警距离"; case "alarm.type": return "报警类型"; case "upgrade.description": return "固件升级功能\n\n请选择要升级的固件文件,然后点击开始升级按钮。升级过程中请勿断开电源。"; case "start.upgrade": return "开始升级"; // 新增:快捷计算相关键值对 case "quick.calculation": return "快捷计算"; case "quick.calc.title": return "坐标计算工具"; // 新增:卫星数据相关键值对 case "satellite.data": return "卫星数据"; case "sat.table.title": return "卫星数据"; case "sat.col.prn": return "PRN"; case "sat.col.snr": return "信噪比(SNR)"; case "sat.col.azimuth": return "方位角"; case "sat.col.elevation": return "仰角"; case "sat.col.used": return "是否被使用"; // 升级相关键值对 case "upgrade.instructions": return "升级说明"; case "select.folder": return "选择文件夹"; case "browse": return "浏览"; case "select.bin.file": return "选择BIN文件"; case "selected.file": return "已选择文件"; case "file.selected": return "文件已选择"; case "please.select.folder.first": return "请先选择文件夹"; case "warning": return "警告"; case "upgrade.completed": return "升级完成"; case "success": return "成功"; case "upgrade.in.progress": return "升级进行中..."; case "select.upgrade.file": return "选择升级文件"; case "upgrade.file": return "升级文件"; case "upgrade.progress": return "升级进度"; case "cancel.upgrade": return "取消升级"; case "upgrade.success": return "升级成功"; case "upgrade.failed": return "升级失败"; case "confirm.upgrade": return "确认升级"; case "upgrade.confirmation": return "升级确认"; case "upgrade.confirm.message": return "确定要开始升级吗?升级过程中请勿断开电源。"; case "select.file": return "选择文件"; case "no.file.selected": return "未选择文件"; case "file.not.found": return "文件未找到"; case "invalid.file": return "无效文件"; case "upgrade.ready": return "准备升级"; case "preparing.upgrade": return "准备升级环境..."; case "writing.firmware": return "写入固件..."; case "verifying.firmware": return "验证固件..."; case "upgrade.cancelled": return "升级已取消"; // 扩展面板相关键值对 case "common.commands": return "常用指令"; case "command.string": return "指令字符串"; case "save.common.commands": return "保存常用指令"; case "modify.common.commands": return "修改常用指令"; case "load.commands.failed": return "加载指令失败:"; case "save.commands.success": return "保存指令成功!"; case "save.commands.failed": return "保存指令失败:"; case "command.cannot.empty": return "指令不能为空!"; case "now.can.modify.commands": return "现在可以修改表格中的指令内容,修改完成后点击保存常用指令按钮"; case "prompt": return "提示"; case "error": return "错误"; // 扩展命令相关键值对 case "extension.command": return "扩展命令"; default: return key; } } private void initializeUI() { setTitle(getString("app.title")); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(1250, 800); setLocationRelativeTo(null); // 设置窗口图标 setWindowIcon(); // 创建顶部按钮面板 JPanel topPanel = createTopPanel(); // 创建主内容面板 cardLayout = new CardLayout(); mainPanel = new JPanel(cardLayout); // 初始化各功能面板 serialPanel = new SerialCommunicationPanel(this); networkPanel = new NetworkCommunicationPanel(this); configPanel = new NetworkConfigPanel(this); mainPanel.add(serialPanel, "SERIAL"); mainPanel.add(networkPanel, "NETWORK"); mainPanel.add(configPanel, "CONFIG"); // 新增:卫星数据面板 satellitePanel = new SatelliteDataPanel(this); mainPanel.add(satellitePanel, "SATELLITE"); // 创建右侧面板(数据日志和发送面板) JPanel rightPanel = createRightPanel(); // 设置布局 setLayout(new BorderLayout()); add(topPanel, BorderLayout.NORTH); contentSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, mainPanel, rightPanel); contentSplitPane.setResizeWeight(0.6); // 修改:设置主面板占60% contentSplitPane.setOneTouchExpandable(true); contentSplitPane.setContinuousLayout(true); contentSplitPane.setDividerLocation(0.6); // 修改:分割条位置在60%处 add(contentSplitPane, BorderLayout.CENTER); // 初始化快捷计算对话框 initQuickCalculationDialog(); // 添加事件监听 setupEventListeners(); // 关键修改:连接串口面板和数据日志面板 connectPanels(); } private JPanel createTopPanel() { JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 使用ButtonUtils创建导航按钮 serialCommBtn = ButtonUtils.createBlueButton(getString("serial.communication")); networkCommBtn = ButtonUtils.createBlueButton(getString("network.communication")); networkConfigBtn = ButtonUtils.createBlueButton(getString("network.config")); // 新增:快捷计算按钮 quickCalcBtn = ButtonUtils.createBlueButton(getString("quick.calculation")); // 新增:卫星数据按钮 satelliteBtn = ButtonUtils.createBlueButton(getString("satellite.data")); languageCombo = new JComboBox<>(new String[]{"中文", "English"}); languageCombo.setSelectedIndex(0); languageCombo.addActionListener(e -> { Locale newLocale = languageCombo.getSelectedIndex() == 0 ? Locale.SIMPLIFIED_CHINESE : Locale.ENGLISH; switchLanguage(newLocale); }); panel.add(serialCommBtn); panel.add(networkCommBtn); panel.add(networkConfigBtn); panel.add(quickCalcBtn); // 添加快捷计算按钮 panel.add(satelliteBtn); // 添加卫星数据按钮 JLabel languageLabel = new JLabel(getString("LANGUAGE") + ":"); panel.add(Box.createHorizontalStrut(20)); panel.add(languageLabel); panel.add(languageCombo); return panel; } private JPanel createRightPanel() { dataLogPanel = new DataLogPanel(this); sendPanel = new SendPanel(this); rightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, dataLogPanel, sendPanel); rightSplitPane.setResizeWeight(0.75); // 3:1 高度比例 rightSplitPane.setContinuousLayout(true); rightSplitPane.setDividerSize(6); rightSplitPane.setBorder(null); SwingUtilities.invokeLater(() -> rightSplitPane.setDividerLocation(0.75)); JPanel container = new JPanel(new BorderLayout()); // 修改:设置右侧面板宽度为主窗口宽度的40%(4:6比例) container.setPreferredSize(new Dimension((int)(getWidth() * 0.4), getHeight())); container.add(rightSplitPane, BorderLayout.CENTER); return container; } /** * 初始化快捷计算对话框 */ private void initQuickCalculationDialog() { quickCalcDialog = new JDialog(this, getString("quick.calc.title"), false); quickCalcDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); quickCalcDialog.setSize(900, 700); quickCalcDialog.setLocationRelativeTo(this); // 创建快捷计算面板 quickCalcPanel = new QuickCalculationPanel(messages); quickCalcDialog.add(quickCalcPanel); } private void setupEventListeners() { serialCommBtn.addActionListener(e -> cardLayout.show(mainPanel, "SERIAL")); networkCommBtn.addActionListener(e -> cardLayout.show(mainPanel, "NETWORK")); networkConfigBtn.addActionListener(e -> cardLayout.show(mainPanel, "CONFIG")); // 新增:快捷计算按钮事件监听 quickCalcBtn.addActionListener(e -> { quickCalcDialog.setVisible(true); }); // 新增:卫星数据按钮事件监听 satelliteBtn.addActionListener(e -> { cardLayout.show(mainPanel, "SATELLITE"); }); } /** * 关键修改:连接串口面板和数据日志面板 */ private void connectPanels() { if (serialPanel != null && dataLogPanel != null) { serialPanel.setDataLogPanel(dataLogPanel); } if (networkPanel != null && dataLogPanel != null) { networkPanel.setDataLogPanel(dataLogPanel); } if (satellitePanel != null && dataLogPanel != null) { satellitePanel.setDataLogPanel(dataLogPanel); } } private void switchLanguage(Locale newLocale) { this.currentLocale = newLocale; this.messages = loadResourceBundle(currentLocale); // 更新界面文本 updateUILanguage(); // 更新语言选择框 languageCombo.setSelectedIndex(newLocale.equals(Locale.SIMPLIFIED_CHINESE) ? 0 : 1); revalidate(); repaint(); } private void updateUILanguage() { // 更新导航按钮文本 serialCommBtn.setText(getString("serial.communication")); networkCommBtn.setText(getString("network.communication")); networkConfigBtn.setText(getString("network.config")); quickCalcBtn.setText(getString("quick.calculation")); // 更新快捷计算按钮文本 if (satelliteBtn != null) satelliteBtn.setText(getString("satellite.data")); // 更新卫星按钮文本 // 更新语言标签 setTitle(getString("app.title")); // 更新对话框标题 if (quickCalcDialog != null) { quickCalcDialog.setTitle(getString("quick.calc.title")); } // 更新各面板文本 if (serialPanel != null) serialPanel.updateLanguage(); if (networkPanel != null) networkPanel.updateLanguage(); if (configPanel != null) configPanel.updateLanguage(); if (dataLogPanel != null) dataLogPanel.updateLanguage(); if (satellitePanel != null) satellitePanel.updateLanguage(); if (sendPanel != null) sendPanel.updateLanguage(); // 更新快捷计算面板 if (quickCalcPanel != null) { // 重新创建快捷计算面板以更新语言 quickCalcDialog.getContentPane().removeAll(); quickCalcPanel = new QuickCalculationPanel(messages); quickCalcDialog.add(quickCalcPanel); quickCalcDialog.revalidate(); quickCalcDialog.repaint(); } } public Locale getCurrentLocale() { return currentLocale; } /** * 获取数据日志面板实例 * * @return DataLogPanel实例 */ public DataLogPanel getDataLogPanel() { return dataLogPanel; } /** * 从扩展面板发送数据 * * @param data 要发送的数据 * @param isHex 是否为HEX格式 */ public void sendDataFromExtension(String data, boolean isHex) { if (data == null || data.trim().isEmpty()) { return; } // 根据当前激活的面板决定发送方式 Component currentPanel = null; for (Component comp : mainPanel.getComponents()) { if (comp.isVisible()) { currentPanel = comp; break; } } if (currentPanel instanceof SerialCommunicationPanel) { // 串口通信面板激活,通过串口发送 sendViaSerial(data, isHex); } else if (currentPanel instanceof NetworkCommunicationPanel) { // 网络通信面板激活,通过网络发送 sendViaNetwork(data, isHex); } else { // 默认尝试通过串口发送 sendViaSerial(data, isHex); } } /** * 通过串口发送数据 */ private void sendViaSerial(String data, boolean isHex) { if (serialPanel != null) { // 这里调用串口面板的发送方法 // 需要根据您的SerialCommunicationPanel实现来调用相应方法 // 示例:如果串口面板有发送数据的方法 // serialPanel.sendData(data, isHex); } else { System.err.println("串口面板未初始化"); } } /** * 通过网络发送数据 */ private void sendViaNetwork(String data, boolean isHex) { if (networkPanel != null) { if (isHex) { // networkPanel.sendHexNetworkData(data); } else { //networkPanel.sendNetworkData(data.getBytes(java.nio.charset.StandardCharsets.UTF_8)); } } else { System.err.println("网络面板未初始化"); } } public void addToDataLog(String logMessage) { if (dataLogPanel != null) { // 通过DataLogPanel的公共方法添加日志 dataLogPanel.addLog(logMessage); } } /** * 获取网络通信面板实例 */ public NetworkCommunicationPanel getNetworkCommunicationPanel() { // 根据您的实际实现返回网络通信面板 // 例如,如果网络通信面板是选项卡中的一个,可以这样获取: return networkPanel; } /** * 设置窗口图标 */ private void setWindowIcon() { try { File iconFile = new File("image/setting.png"); if (iconFile.exists()) { Image icon = ImageIO.read(iconFile); if (icon != null) { setIconImage(icon); } } else { System.err.println("图标文件不存在: " + iconFile.getAbsolutePath()); } } catch (IOException e) { System.err.println("加载图标文件失败: " + e.getMessage()); } } }