Swing 写的客户端程序在java web start 运行,多语言过程中,JOptionPane.showMessageDialog() 按钮多语言问题

本文介绍如何在Java程序中实现多语言支持,通过设置默认语言环境并应用到Swing组件上,确保界面元素按指定语言显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在程序启动的Main方法中,根据不同的语言,实现java本身类的多语言处理,这样弹出的消息框上的按钮就显示相应语言的文字了

Locale.setDefault(Locale.ENGLISH);  //英文

Local.setDefault(Locale.China) ; //设置为中文

但是放在java web start 中启动的时候,Swing 的组建还显示的是操作系统的语言

如何处理呢:

在设置了默认的语言后,然后设置java组建的默认语言

JComponent.setDefaultLocale(Locale.getDefault());

/ 出入库管理模块 class InventoryManager { private JTabbedPane tabbedPane = new JTabbedPane(); private Map<String, DefaultTableModel> models = new LinkedHashMap<>(); private Map<String, JTable> tables = new LinkedHashMap<>(); public JPanel createPanel() { JPanel panel = new JPanel(new BorderLayout()); tabbedPane.addTab("入库管理", createInventoryPanel("入库")); tabbedPane.addTab("出库管理", createInventoryPanel("出库")); panel.add(tabbedPane, BorderLayout.CENTER); return panel; } private JPanel createInventoryPanel(String type) { JPanel panel = new JPanel(new BorderLayout()); JToolBar toolbar = new JToolBar(); JButton addBtn = new JButton("添加"); JButton editBtn = new JButton("修改"); JButton deleteBtn = new JButton("删除"); JButton refreshBtn = new JButton("刷新"); JButton capacityBtn = new JButton("仓库容量"); JButton stockBtn = new JButton("商品库存"); addBtn.addActionListener(e -> addRecord(type)); editBtn.addActionListener(e -> editRecord(type)); deleteBtn.addActionListener(e -> deleteRecord(type)); refreshBtn.addActionListener(e -> refreshData(type)); capacityBtn.addActionListener(e -> showWarehouseCapacity()); stockBtn.addActionListener(e -> showProductStock()); toolbar.add(addBtn); toolbar.add(editBtn); toolbar.add(deleteBtn); toolbar.add(refreshBtn); toolbar.add(capacityBtn); toolbar.add(stockBtn); JPanel searchPanel = new JPanel(); JTextField warehouseField = new JTextField(8); JTextField productField = new JTextField(8); JTextField dateField = new JTextField(8); JButton searchBtn = new JButton("搜索"); searchBtn.addActionListener(e -> searchRecords( type, warehouseField.getText().trim(), productField.getText().trim(), dateField.getText().trim() )); searchPanel.add(new JLabel("仓库ID:")); searchPanel.add(warehouseField); searchPanel.add(new JLabel("商品ID:")); searchPanel.add(productField); searchPanel.add(new JLabel("日期:")); searchPanel.add(dateField); searchPanel.add(searchBtn); DefaultTableModel model = new DefaultTableModel() { @Override public boolean isCellEditable(int row, int column) { return false; } }; model.setColumnIdentifiers(new String[]{"编号", "仓库ID", "商品ID", "商品数量", "日期", "类型"}); models.put(type, model); JTable table = new JTable(model); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); tables.put(type, table); refreshData(type); JPanel topPanel = new JPanel(new BorderLayout()); topPanel.add(toolbar, BorderLayout.NORTH); topPanel.add(searchPanel, BorderLayout.CENTER); panel.add(topPanel, BorderLayout.NORTH); panel.add(new JScrollPane(table), BorderLayout.CENTER); return panel; } private void refreshData(String type) { List<Map<String, Object>> data = MySQL.readTable("出入库"); DefaultTableModel model = models.get(type); model.setRowCount(0); for (Map<String, Object> row : data) { if (type.equals(row.get("类型"))) { model.addRow(new Object[]{ row.get("编号"), row.get("仓库ID"), row.get("商品ID"), row.get("商品数量"), row.get("日期"), row.get("类型") }); } } } private void searchRecords(String type, String warehouseId, String productId, String date) { DefaultTableModel model = models.get(type); model.setRowCount(0); String sql = "SELECT * FROM 出入库 WHERE 类型='" + type + "'"; if (!warehouseId.isEmpty()) { sql += " AND 仓库ID LIKE '%" + warehouseId + "%'"; } if (!productId.isEmpty()) { sql += " AND 商品ID LIKE '%" + productId + "%'"; } if (!date.isEmpty()) { sql += " AND 日期 LIKE '%" + date + "%'"; } try (Connection conn = MySQL.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { while (rs.next()) { model.addRow(new Object[]{ rs.getString("编号"), rs.getString("仓库ID"), rs.getString("商品ID"), rs.getInt("商品数量"), rs.getString("日期"), rs.getString("类型") }); } } catch (SQLException e) { MySQL.showError("搜索错误", e); } } private void addRecord(String type) { JDialog dialog = new JDialog((Frame) null, type + "记录", true); dialog.setSize(500, 300); dialog.setLayout(new GridLayout(5, 2, 10, 10)); dialog.setLocationRelativeTo(null); JTextField warehouseField = new JTextField(); JTextField productField = new JTextField(); JTextField quantityField = new JTextField(); JTextField dateField = new JTextField(); JButton submitBtn = new JButton("提交"); submitBtn.addActionListener(e -> { try { String warehouseId = warehouseField.getText().trim(); String productId = productField.getText().trim(); int quantity = Integer.parseInt(quantityField.getText().trim()); String date = dateField.getText().trim(); if (warehouseId.isEmpty() || productId.isEmpty() || date.isEmpty()) { JOptionPane.showMessageDialog(dialog, "所有字段都必须填!"); return; } if (!MySQL.checkForeignKeyExists("编号", "编号", warehousenum) { if (!MySQL.checkForeignKeyExists("仓库", "仓库编号", warehouseId)) { JOptionPane.showMessageDialog(dialog, "仓库ID不存在!"); return; } if (!MySQL.checkForeignKeyExists("商品", "商品编号", productId)) { JOptionPane.showMessageDialog(dialog, "商品ID不存在!"); return; } if (quantity <= 0) { JOptionPane.showMessageDialog(dialog, "商品数量必须大于0!"); return; } boolean isInbound = "入库".equals(type); if (isInbound && !checkWarehouseCapacity(warehouseId, quantity)) { JOptionPane.showMessageDialog(dialog, "仓库剩余容量不足!"); return; } if (!isInbound && !checkProductStock(productId, quantity)) { JOptionPane.showMessageDialog(dialog, "商品库存不足!"); return; } if (MySQL.executeUpdate( "INSERT INTO 出入库 (编号,仓库ID, 商品ID, 商品数量, 日期, 类型) VALUES (?,?, ?, ?, ?, ?)", warehouseId, productId, quantity, date, type) > 0) { MySQL.updateWarehouseCapacity(warehouseId, quantity, isInbound); MySQL.updateProductStock(productId, quantity, isInbound); JOptionPane.showMessageDialog(dialog, type + "记录添加成功!"); dialog.dispose(); refreshData(type); } else { JOptionPane.showMessageDialog(dialog, type + "记录添加失败!"); } } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(dialog, "请输入有效的数字!"); } }); JButton cancelBtn = new JButton("取消"); cancelBtn.addActionListener(e -> dialog.dispose()); dialog.add(new JLabel("仓库ID:")); dialog.add(warehouseField); dialog.add(new JLabel("商品ID:")); dialog.add(productField); dialog.add(new JLabel("商品数量:")); dialog.add(quantityField); dialog.add(new JLabel("日期(YYYY-MM-DD):")); dialog.add(dateField); dialog.add(submitBtn); dialog.add(cancelBtn); dialog.setVisible(true); } private boolean checkWarehouseCapacity(String warehouseId, int quantity) { String sql = "SELECT 剩余容量 FROM 仓库 WHERE 仓库编号 = '" + warehouseId + "'"; try (Connection conn = MySQL.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { return rs.next() && rs.getInt("剩余容量") >= quantity; } catch (SQLException e) { MySQL.showError("容量检查错误", e); return false; } } private boolean checkProductStock(String productId, int quantity) { String sql = "SELECT 库存 FROM 商品 WHERE 商品编号 = '" + productId + "'"; try (Connection conn = MySQL.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { return rs.next() && rs.getInt("库存") >= quantity; } catch (SQLException e) { MySQL.showError("库存检查错误", e); return false; } } private void showWarehouseCapacity() { String warehouseId = JOptionPane.showInputDialog("请输入仓库编号:"); if (warehouseId != null && !warehouseId.isEmpty()) { String sql = "SELECT 剩余容量 FROM 仓库 WHERE 仓库编号 = '" + warehouseId + "'"; try (Connection conn = MySQL.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { if (rs.next()) { JOptionPane.showMessageDialog(null, "仓库 " + warehouseId + " 的剩余容量: " + rs.getInt("剩余容量")); } else { JOptionPane.showMessageDialog(null, "未找到仓库!"); } } catch (SQLException e) { MySQL.showError("查询错误", e); } } } private void showProductStock() { String productId = JOptionPane.showInputDialog("请输入商品编号:"); if (productId != null && !productId.isEmpty()) { String sql = "SELECT 库存 FROM 商品 WHERE 商品编号 = '" + productId + "'"; try (Connection conn = MySQL.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { if (rs.next()) { JOptionPane.showMessageDialog(null, "商品 " + productId + " 的库存: " + rs.getInt("库存")); } else { JOptionPane.showMessageDialog(null, "未找到商品!"); } } catch (SQLException e) { MySQL.showError("查询错误", e); } } } private void editRecord(String type) { JTable table = tables.get(type); int row = table.getSelectedRow(); if (row == -1) { JOptionPane.showMessageDialog(null, "请选择要修改的记录"); return; } String id = (String) table.getValueAt(row, 0); String warehouseId = (String) table.getValueAt(row, 1); String productId = (String) table.getValueAt(row, 2); int quantity = (int) table.getValueAt(row, 3); String date = (String) table.getValueAt(row, 4); JDialog dialog = new JDialog((Frame) null, "修改记录", true); dialog.setSize(500, 300); dialog.setLayout(new GridLayout(5, 2, 10, 10)); dialog.setLocationRelativeTo(null); JTextField warehouseField = new JTextField(warehouseId); JTextField productField = new JTextField(productId); JTextField quantityField = new JTextField(String.valueOf(quantity)); JTextField dateField = new JTextField(date); JButton submitBtn = new JButton("提交"); submitBtn.addActionListener(e -> { try { String newWarehouseId = warehouseField.getText().trim(); String newProductId = productField.getText().trim(); int newQuantity = Integer.parseInt(quantityField.getText().trim()); String newDate = dateField.getText().trim(); if (newWarehouseId.isEmpty() || newProductId.isEmpty() || newDate.isEmpty()) { JOptionPane.showMessageDialog(dialog, "所有字段都必须填!"); return; } if (!MySQL.checkForeignKeyExists("仓库", "仓库编号", newWarehouseId)) { JOptionPane.showMessageDialog(dialog, "仓库ID不存在!"); return; } if (!MySQL.checkForeignKeyExists("商品", "商品编号", newProductId)) { JOptionPane.showMessageDialog(dialog, "商品ID不存在!"); return; } if (newQuantity <= 0) { JOptionPane.showMessageDialog(dialog, "商品数量必须大于0!"); return; } boolean isInbound = "入库".equals(type); if (isInbound && !checkWarehouseCapacity(newWarehouseId, newQuantity)) { JOptionPane.showMessageDialog(dialog, "仓库剩余容量不足!"); return; } if (!isInbound && !checkProductStock(newProductId, newQuantity)) { JOptionPane.showMessageDialog(dialog, "商品库存不足!"); return; } if (MySQL.executeUpdate( "UPDATE 出入库 SET 仓库ID=?, 商品ID=?, 商品数量=?, 日期=? WHERE 编号=?", newWarehouseId, newProductId, newQuantity, newDate, id) > 0) { MySQL.updateWarehouseCapacity(warehouseId, quantity, !isInbound); MySQL.updateProductStock(productId, quantity, !isInbound); MySQL.updateWarehouseCapacity(newWarehouseId, newQuantity, isInbound); MySQL.updateProductStock(newProductId, newQuantity, isInbound); JOptionPane.showMessageDialog(dialog, "记录更新成功!"); dialog.dispose(); refreshData(type); } else { JOptionPane.showMessageDialog(dialog, "记录更新失败!"); } } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(dialog, "请输入有效的数字!"); } }); JButton cancelBtn = new JButton("取消"); cancelBtn.addActionListener(e -> dialog.dispose()); dialog.add(new JLabel("仓库ID:")); dialog.add(warehouseField); dialog.add(new JLabel("商品ID:")); dialog.add(productField); dialog.add(new JLabel("商品数量:")); dialog.add(quantityField); dialog.add(new JLabel("日期(YYYY-MM-DD):")); dialog.add(dateField); dialog.add(submitBtn); dialog.add(cancelBtn); dialog.setVisible(true); } private void deleteRecord(String type) { JTable table = tables.get(type); int row = table.getSelectedRow(); if (row == -1) { JOptionPane.showMessageDialog(null, "请选择要删除的记录"); return; } String id = (String) table.getValueAt(row, 0); String warehouseId = (String) table.getValueAt(row, 1); String productId = (String) table.getValueAt(row, 2); int quantity = (int) table.getValueAt(row, 3); int confirm = JOptionPane.showConfirmDialog( null, "确定要删除这条记录吗?", "确认删除", JOptionPane.YES_NO_OPTION ); if (confirm == JOptionPane.YES_OPTION) { if (MySQL.executeUpdate("DELETE FROM 出入库 WHERE 编号=?", id) > 0) { boolean isInbound = "入库".equals(type); MySQL.updateWarehouseCapacity(warehouseId, quantity, !isInbound); MySQL.updateProductStock(productId, quantity, !isInbound); JOptionPane.showMessageDialog(null, "记录删除成功!"); refreshData(type); } else { JOptionPane.showMessageDialog(null, "记录删除失败!"); } } } } 在添加入库时要手动输入编号
最新发布
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值