JOptionPane找不到问题解决

本文介绍了解决Eclipse中无法导入JOptionPane的问题步骤。通过调整项目的属性设置,使Deprecated and restricted API忽略警告,从而成功导入JOptionPane组件。

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

有些版本的eclipse里面没有JOptionPane ,遇到这样的问题我们怎么解决

1,打开eclipse,找到project里面的properties。


2.进入properties,按下图操作



3.按照上述步骤进入之后,找到Deprecated and restricted API.在把里面的相应项设置为所示的ignore


4.点击ok 完成相应的设置。在进入eclipse,就能够import  JOptionPane。


import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; public class MyFrame extends JFrame { private JTable table; private DefaultTableModel model; private JTextField searchField; private List<Transaction> transactions = new ArrayList<>(); // 用于存储进出库记录 public MyFrame() { setTitle("商品管理系统"); setSize(900, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // 创建顶部搜索面板 createTopPanel(); // 创建中间表格面板 createCenterPanel(); // 创建右侧按钮面板 createEastPanel(); // 创建底部状态栏 add(createStatusBar(), BorderLayout.SOUTH); } private void createTopPanel() { JPanel topPanel = new JPanel(new BorderLayout()); topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); JLabel searchLabel = new JLabel("按商品编号搜索:"); searchField = new JTextField(15); JButton searchButton = new JButton("搜索"); searchButton.addActionListener(e -> searchProduct()); JPanel searchPanel = new JPanel(); searchPanel.add(searchLabel); searchPanel.add(searchField); searchPanel.add(searchButton); topPanel.add(searchPanel, BorderLayout.WEST); add(topPanel, BorderLayout.NORTH); } private void createCenterPanel() { // 表格列名 String[] columnNames = {"商品编号", "商品名称", "建议单价", "一般成本价", "当前库存", "备注"}; // 表格数据 Object[][] data = { {"P001", "苹果手机", 6999.0, 5500.0, 77, "除掉商品"}, {"P002", "智能手机端", 6500.0, 1200.0, 13, "总订单"}, {"P003", "无线耳机", 299.0, 180.0, 56, "蓝牙5.0"}, {"P005", "耳机", 99.0, 88.0, 10, ""} }; // 创建表格模型(除特定列外不可编辑) model = new DefaultTableModel(data, columnNames) { @Override public boolean isCellEditable(int row, int column) { // 仅允许编辑备注列 return column == 5; } }; table = new JTable(model); table.setRowHeight(25); table.getTableHeader().setFont(new Font("SimSun", Font.BOLD, 14)); table.setFont(new Font("SimSun", Font.PLAIN, 14)); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane, BorderLayout.CENTER); } private void createEastPanel() { JPanel eastPanel = new JPanel(); eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS)); eastPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 创建按钮 JButton addButton = createButton("添加商品"); JButton editButton = createButton("编辑商品"); JButton deleteButton = createButton("删除商品"); JButton inButton = createButton("入库"); JButton outButton = createButton("出库"); JButton detailButton = createButton("查看详情"); JButton exitButton = createButton("退出系统"); // 添加动作监听器 addButton.addActionListener(e -> addProduct()); editButton.addActionListener(e -> editProduct()); deleteButton.addActionListener(e -> deleteProduct()); inButton.addActionListener(e -> stockIn()); outButton.addActionListener(e -> stockOut()); detailButton.addActionListener(e -> viewProductDetails()); exitButton.addActionListener(e -> System.exit(0)); // 添加按钮到面板并设置间距 eastPanel.add(addButton); eastPanel.add(Box.createVerticalStrut(10)); eastPanel.add(editButton); eastPanel.add(Box.createVerticalStrut(10)); eastPanel.add(deleteButton); eastPanel.add(Box.createVerticalStrut(10)); eastPanel.add(detailButton); eastPanel.add(Box.createVerticalStrut(20)); // 库存按钮前的额外间距 eastPanel.add(inButton); eastPanel.add(Box.createVerticalStrut(10)); eastPanel.add(outButton); eastPanel.add(Box.createVerticalStrut(20)); // 退出按钮前的额外间距 eastPanel.add(exitButton); add(eastPanel, BorderLayout.EAST); } private JButton createButton(String text) { JButton button = new JButton(text); button.setFont(new Font("SimSun", Font.PLAIN, 14)); button.setPreferredSize(new Dimension(120, 40)); button.setMaximumSize(new Dimension(120, 40)); button.setAlignmentX(Component.CENTER_ALIGNMENT); return button; } private JPanel createStatusBar() { JPanel statusPanel = new JPanel(); statusPanel.setBorder(BorderFactory.createEtchedBorder()); statusPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel statusLabel = new JLabel("当前模式:商品管理"); statusLabel.setFont(new Font("SimSun", Font.PLAIN, 12)); statusPanel.add(statusLabel); return statusPanel; } // 检查商品编号是否已存在 private boolean isProductIdExists(String id) { for (int i = 0; i < model.getRowCount(); i++) { if (model.getValueAt(i, 0).toString().equals(id)) { return true; } } return false; } // 按商品编号搜索 private void searchProduct() { String searchId = searchField.getText().trim(); if (searchId.isEmpty()) { JOptionPane.showMessageDialog(this, "请输入商品编号", "提示", JOptionPane.WARNING_MESSAGE); return; } int rowIndex = -1; for (int i = 0; i < model.getRowCount(); i++) { if (model.getValueAt(i, 0).toString().equals(searchId)) { rowIndex = i; break; } } if (rowIndex == -1) { JOptionPane.showMessageDialog(this, "未到商品编号为 " + searchId + " 的商品", "提示", JOptionPane.INFORMATION_MESSAGE); } else { table.setRowSelectionInterval(rowIndex, rowIndex); viewProductDetails(); // 直接查看详情 } } // 查看商品详情 private void viewProductDetails() { int selectedRow = table.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "请先选择要查看的商品", "提示", JOptionPane.WARNING_MESSAGE); return; } String productId = model.getValueAt(selectedRow, 0).toString(); // 创建详情面板 JPanel detailPanel = new JPanel(new GridLayout(7, 2, 5, 5)); detailPanel.add(new JLabel("商品编号:")); detailPanel.add(new JLabel(productId)); detailPanel.add(new JLabel("商品名称:")); detailPanel.add(new JLabel(model.getValueAt(selectedRow, 1).toString())); detailPanel.add(new JLabel("建议单价:")); detailPanel.add(new JLabel(model.getValueAt(selectedRow, 2).toString())); detailPanel.add(new JLabel("一般成本价:")); detailPanel.add(new JLabel(model.getValueAt(selectedRow, 3).toString())); detailPanel.add(new JLabel("当前库存:")); detailPanel.add(new JLabel(model.getValueAt(selectedRow, 4).toString())); detailPanel.add(new JLabel("备注:")); detailPanel.add(new JLabel(model.getValueAt(selectedRow, 5).toString())); // 添加进出库记录 detailPanel.add(new JLabel("进出库记录:")); // 创建记录表格 String[] columns = {"操作", "数量", "日期"}; DefaultTableModel recordModel = new DefaultTableModel(columns, 0); // 添加该商品的所有记录 for (Transaction transaction : transactions) { if (transaction.getProductId().equals(productId)) { recordModel.addRow(new Object[]{ transaction.isStockIn() ? "入库" : "出库", transaction.getQuantity(), transaction.getDate() }); } } JTable recordTable = new JTable(recordModel); recordTable.setRowHeight(20); JScrollPane recordScrollPane = new JScrollPane(recordTable); // 创建一个面板来容纳详情和记录 JPanel mainPanel = new JPanel(new BorderLayout()); mainPanel.add(detailPanel, BorderLayout.NORTH); mainPanel.add(recordScrollPane, BorderLayout.CENTER); JOptionPane.showMessageDialog(this, mainPanel, "商品详情", JOptionPane.PLAIN_MESSAGE); } private void addProduct() { JPanel panel = new JPanel(new GridLayout(6, 2, 5, 5)); JTextField idField = new JTextField(); JTextField nameField = new JTextField(); JTextField priceField = new JTextField(); JTextField costField = new JTextField(); JTextField stockField = new JTextField("0"); JTextField noteField = new JTextField(); panel.add(new JLabel("商品编号:")); panel.add(idField); panel.add(new JLabel("商品名称:")); panel.add(nameField); panel.add(new JLabel("建议单价:")); panel.add(priceField); panel.add(new JLabel("一般成本价:")); panel.add(costField); panel.add(new JLabel("当前库存:")); panel.add(stockField); panel.add(new JLabel("备注:")); panel.add(noteField); int result = JOptionPane.showConfirmDialog(this, panel, "添加新商品", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { try { String id = idField.getText().trim(); if (id.isEmpty()) { JOptionPane.showMessageDialog(this, "商品编号不能为空", "错误", JOptionPane.ERROR_MESSAGE); return; } if (isProductIdExists(id)) { JOptionPane.showMessageDialog(this, "商品编号已存在,请使用其他编号", "错误", JOptionPane.ERROR_MESSAGE); return; } Object[] rowData = { id, nameField.getText(), Double.parseDouble(priceField.getText()), Double.parseDouble(costField.getText()), Integer.parseInt(stockField.getText()), noteField.getText() }; model.addRow(rowData); JOptionPane.showMessageDialog(this, "商品添加成功!", "成功", JOptionPane.INFORMATION_MESSAGE); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "请输入有效的数字", "错误", JOptionPane.ERROR_MESSAGE); } } } private void editProduct() { int selectedRow = table.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "请先选择要编辑的商品", "提示", JOptionPane.WARNING_MESSAGE); return; } JPanel panel = new JPanel(new GridLayout(6, 2, 5, 5)); JTextField idField = new JTextField(model.getValueAt(selectedRow, 0).toString()); JTextField nameField = new JTextField(model.getValueAt(selectedRow, 1).toString()); JTextField priceField = new JTextField(model.getValueAt(selectedRow, 2).toString()); JTextField costField = new JTextField(model.getValueAt(selectedRow, 3).toString()); JTextField stockField = new JTextField(model.getValueAt(selectedRow, 4).toString()); JTextField noteField = new JTextField(model.getValueAt(selectedRow, 5).toString()); // 使ID和库存字段不可编辑 idField.setEditable(false); stockField.setEditable(false); panel.add(new JLabel("商品编号:")); panel.add(idField); panel.add(new JLabel("商品名称:")); panel.add(nameField); panel.add(new JLabel("建议单价:")); panel.add(priceField); panel.add(new JLabel("一般成本价:")); panel.add(costField); panel.add(new JLabel("当前库存:")); panel.add(stockField); panel.add(new JLabel("备注:")); panel.add(noteField); int result = JOptionPane.showConfirmDialog(this, panel, "编辑商品信息", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { try { model.setValueAt(nameField.getText(), selectedRow, 1); model.setValueAt(Double.parseDouble(priceField.getText()), selectedRow, 2); model.setValueAt(Double.parseDouble(costField.getText()), selectedRow, 3); // 不修改库存 model.setValueAt(noteField.getText(), selectedRow, 5); JOptionPane.showMessageDialog(this, "商品信息更新成功!", "成功", JOptionPane.INFORMATION_MESSAGE); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "请输入有效的数字", "错误", JOptionPane.ERROR_MESSAGE); } } } private void deleteProduct() { int selectedRow = table.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "请先选择要删除的商品", "提示", JOptionPane.WARNING_MESSAGE); return; } // 检查库存是否为0 int currentStock = Integer.parseInt(model.getValueAt(selectedRow, 4).toString()); if (currentStock > 0) { JOptionPane.showMessageDialog(this, "库存不为0,不能删除该商品", "错误", JOptionPane.ERROR_MESSAGE); return; } int confirm = JOptionPane.showConfirmDialog(this, "确定要删除选中的商品吗?", "确认删除", JOptionPane.YES_NO_OPTION); if (confirm == JOptionPane.YES_OPTION) { model.removeRow(selectedRow); JOptionPane.showMessageDialog(this, "商品删除成功!", "成功", JOptionPane.INFORMATION_MESSAGE); } } private void stockIn() { int selectedRow = table.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "请先选择要入库的商品", "提示", JOptionPane.WARNING_MESSAGE); return; } String input = JOptionPane.showInputDialog(this, "请输入入库数量:", "入库操作", JOptionPane.PLAIN_MESSAGE); if (input == null || input.trim().isEmpty()) return; try { int quantity = Integer.parseInt(input); if (quantity <= 0) { JOptionPane.showMessageDialog(this, "入库数量必须大于0", "错误", JOptionPane.ERROR_MESSAGE); return; } int currentStock = Integer.parseInt(model.getValueAt(selectedRow, 4).toString()); model.setValueAt(currentStock + quantity, selectedRow, 4); // 记录入库操作 String productId = model.getValueAt(selectedRow, 0).toString(); transactions.add(new Transaction(productId, quantity, true)); JOptionPane.showMessageDialog(this, "入库成功! 当前库存: " + (currentStock + quantity), "成功", JOptionPane.INFORMATION_MESSAGE); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "请输入有效的数字", "错误", JOptionPane.ERROR_MESSAGE); } } private void stockOut() { int selectedRow = table.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "请先选择要出库的商品", "提示", JOptionPane.WARNING_MESSAGE); return; } String input = JOptionPane.showInputDialog(this, "请输入出库数量:", "出库操作", JOptionPane.PLAIN_MESSAGE); if (input == null || input.trim().isEmpty()) return; try { int quantity = Integer.parseInt(input); if (quantity <= 0) { JOptionPane.showMessageDialog(this, "出库数量必须大于0", "错误", JOptionPane.ERROR_MESSAGE); return; } int currentStock = Integer.parseInt(model.getValueAt(selectedRow, 4).toString()); if (quantity > currentStock) { JOptionPane.showMessageDialog(this, "出库数量不能超过当前库存", "错误", JOptionPane.ERROR_MESSAGE); return; } model.setValueAt(currentStock - quantity, selectedRow, 4); // 记录出库操作 String productId = model.getValueAt(selectedRow, 0).toString(); transactions.add(new Transaction(productId, quantity, false)); JOptionPane.showMessageDialog(this, "出库成功! 当前库存: " + (currentStock - quantity), "成功", JOptionPane.INFORMATION_MESSAGE); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "请输入有效的数字", "错误", JOptionPane.ERROR_MESSAGE); } } // 进出库记录类 private class Transaction { private String productId; private int quantity; private boolean stockIn; private String date; public Transaction(String productId, int quantity, boolean stockIn) { this.productId = productId; this.quantity = quantity; this.stockIn = stockIn; this.date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()); } public String getProductId() { return productId; } public int getQuantity() { return quantity; } public boolean isStockIn() { return stockIn; } public String getDate() { return date; } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { MyFrame frame = new MyFrame(); frame.setVisible(true); }); } } 补充:删除某商品信息,库存不为0时不可删除;提供补充后的代码
最新发布
06-13
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值