【转】Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解

本文深入探讨了 Java Swing 中的 JOptionPane 类如何用于创建消息对话框、确认对话框、输入对话框和选项对话框。通过展示具体实例,详细解释了各个方法的参数及其作用,旨在提供一种直观且实用的对话框使用指南。

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

在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示.

Swing中提供了JOptionPane类来实现类似Windows平台下的MessageBox的功能,同样在Java中也有,利用JOptionPane类中的各个static方法来生成各种标准的对话框,实现显示出信息、提出问题、警告、用户输入参数等功能。这些对话框都是模式对话框。
ConfirmDialog --- 确认对话框,提出问题,然后由用户自己来确认(按"Yes"或"No"按钮)
InputDialog --- 提示输入文本
MessageDialog --- 显示信息
OptionDialog -- 组合其它三个对话框类型。
  这四个对话框可以采用showXXXDialog()来显示,如showConfirmDialog()显示确认对话框、showInputDialog()显示输入文本对话框、showMessageDialog()显示信息对话框、showOptionDialog()显示选择性的对话框。它们所使用的参数说明如下:
① ParentComponent:指示对话框的父窗口对象,一般为当前窗口。也可以为null即采用缺省的Frame作为父窗口,此时对话框将设置在屏幕的正中。
② message:指示要在对话框内显示的描述性的文字
③ String title:标题条文字串。
④ Component:在对话框内要显示的组件(如按钮)
⑤ Icon:在对话框内要显示的图标
⑥ messageType:一般可以为如下的值ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE、PLAIN_MESSAGE、
⑦ optionType:它决定在对话框的底部所要显示的按钮选项。一般可以为DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION、OK_CANCEL_OPTION。
使用实例:
(1)显示MessageDialog
JOptionPane.showMessageDialog(null, "在对话框内显示的描述性的文字", "标题条文字串", JOptionPane.ERROR_MESSAGE);
(2)显示ConfirmDialog
JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
(3)显示OptionDialog:该种对话框可以由用户自己来设置各个按钮的个数并返回用户点击各个按钮的序号(从0开始计数)
Object[] options = {"确定","取消","帮助"};
int response=JOptionPane.showOptionDialog(this, "这是个选项对话框,用户可以选择自己的按钮的个数", "选项对话框标题",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(response==0)
{ this.setTitle("您按下了第OK按钮 ");
}
else if(response==1)
{ this.setTitle("您按下了第Cancel按钮 ");
}
else if(response==2)
{ this.setTitle("您按下了第Help按钮 ");
}
(4)显示InputDialog 以便让用户进行输入
String inputValue = JOptionPane.showInputDialog("Please input a value");
(5)显示InputDialog 以便让用户进行选择地输入
Object[] possibleValues = { "First", "Second", "Third" }; //用户的选择项目
Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input",JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
setTitle("您按下了"+(String)selectedValue+"项目");

 转自:http://blog.youkuaiyun.com/zhaobisha/article/details/2714881

import java.awt.*; import javax.swing.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; import javax.swing.table.DefaultTableModel; public class LoginWindow { private JFrame frame; private JTextField txtUser; private JPasswordField txtPassword; private JRadioButton r1, r2; private ButtonGroup bg; private JComboBox<String> city; private JButton btnLogin, btnReset; private JCheckBox c1, c2, c3; private MainPage mainPage; public static void main(String[] args) { SwingUtilities.invokeLater(() -> new LoginWindow().show()); } public LoginWindow() { frame = new JFrame("电商购物平台 - 登录"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 400); frame.setLocationRelativeTo(null); frame.getContentPane().setLayout(null); // 用户名标签和文本 JLabel lblUser = new JLabel("账号:"); lblUser.setBounds(64, 46, 69, 15); frame.getContentPane().add(lblUser); txtUser = new JTextField(); txtUser.setBounds(167, 43, 193, 21); frame.getContentPane().add(txtUser); // 密码标签和密码 JLabel lblPassword = new JLabel("密码:"); lblPassword.setBounds(64, 86, 69, 15); frame.getContentPane().add(lblPassword); txtPassword = new JPasswordField(); txtPassword.setBounds(167, 83, 193, 21); frame.getContentPane().add(txtPassword); // 性别单选按钮 r1 = new JRadioButton("男"); r1.setBounds(167, 123, 60, 21); r1.setSelected(true); frame.getContentPane().add(r1); r2 = new JRadioButton("女"); r2.setBounds(247, 123, 60, 21); frame.getContentPane().add(r2); bg = new ButtonGroup(); bg.add(r1); bg.add(r2); // 城市下拉 city = new JComboBox<>(new String[]{"", "北京", "上海", "天津", "石家庄"}); city.setBounds(167, 163, 193, 21); frame.getContentPane().add(city); JLabel lblCity = new JLabel("所在城市:"); lblCity.setBounds(64, 166, 69, 15); frame.getContentPane().add(lblCity); // 兴趣爱好复选 c1 = new JCheckBox("文学"); c1.setBounds(167, 203, 60, 21); frame.getContentPane().add(c1); c2 = new JCheckBox("体育"); c2.setBounds(247, 203, 60, 21); frame.getContentPane().add(c2); c3 = new JCheckBox("音乐"); c3.setBounds(327, 203, 60, 21); frame.getContentPane().add(c3); JLabel lblAihao = new JLabel("个人爱好:"); lblAihao.setBounds(64, 206, 69, 15); frame.getContentPane().add(lblAihao); // 登录按钮 btnLogin = new JButton("登 录"); btnLogin.setBounds(170, 250, 80, 30); btnLogin.setFont(new Font("微软雅黑", Font.BOLD, 14)); btnLogin.setForeground(Color.BLUE); btnLogin.setBorder(BorderFactory.createLineBorder(Color.BLUE)); btnLogin.addActionListener(e -> login()); frame.getContentPane().add(btnLogin); // 重置按钮 btnReset = new JButton("重 置"); btnReset.setBounds(260, 250, 80, 30); btnReset.setFont(new Font("微软雅黑", Font.BOLD, 14)); btnReset.setForeground(Color.BLUE); btnReset.setBorder(BorderFactory.createLineBorder(Color.BLUE)); btnReset.addActionListener(e -> reset()); frame.getContentPane().add(btnReset); } private void login() { String name = txtUser.getText(); String password = new String(txtPassword.getPassword()); if (!name.isEmpty() && !password.isEmpty()) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { // 加载 JDBC 驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 获取数据库连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password"); String sql = "SELECT * FROM yonghu WHERE yhMC = ? AND yhXB = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, password); rs = pstmt.executeQuery(); if (rs.next()) { // 创建并显示主页面 if (mainPage == null) { mainPage = new MainPage(); } mainPage.setVisible(true); frame.setVisible(false); } else { JOptionPane.showMessageDialog(frame, "用户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE); } } catch (ClassNotFoundException e) { JOptionPane.showMessageDialog(frame, "未找到 MySQL JDBC 驱动!", "错误", JOptionPane.ERROR_MESSAGE); } catch (SQLException e) { JOptionPane.showMessageDialog(frame, "数据库连接失败!", "错误", JOptionPane.ERROR_MESSAGE); } finally { // 关闭资源 try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } else { JOptionPane.showMessageDialog(frame, "用户名和密码不能为空!", "错误", JOptionPane.ERROR_MESSAGE); } } private void reset() { txtUser.setText(""); txtPassword.setText(""); r1.setSelected(true); r2.setSelected(false); city.setSelectedIndex(0); c1.setSelected(false); c2.setSelected(false); c3.setSelected(false); } public void show() { frame.setVisible(true); } // 主页面类 public class MainPage extends JFrame { public MainPage() { setTitle("电商购物平台 - 主页面"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // 添加主页面的内容 JLabel welcomeLabel = new JLabel("欢迎使用电商购物平台。"); welcomeLabel.setHorizontalAlignment(JLabel.CENTER); welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 18)); add(welcomeLabel); // 创建菜单栏 JMenuBar menuBar = new JMenuBar(); // 创建菜单 JMenu productMenu = new JMenu("商品管理"); JMenu userMenu = new JMenu("用户管理"); JMenu orderMenu = new JMenu("订单管理"); JMenu purchaseMenu = new JMenu("购买"); // 创建菜单项 JMenuItem addProduct = new JMenuItem("商品新增"); JMenuItem modifyProduct = new JMenuItem("商品修改"); JMenuItem deleteProduct = new JMenuItem("商品删除"); JMenuItem queryProduct = new JMenuItem("商品查询"); JMenuItem addUser = new JMenuItem("用户新增"); JMenuItem modifyUser = new JMenuItem("用户修改"); JMenuItem deleteUser = new JMenuItem("用户删除"); JMenuItem queryUser = new JMenuItem("用户查询"); JMenuItem queryOrder = new JMenuItem("订单查询"); JMenuItem deleteOrder = new JMenuItem("订单删除"); JMenuItem modifyOrder = new JMenuItem("订单修改"); JMenuItem buyProduct = new JMenuItem("商品购买"); // 将菜单项添加到菜单 productMenu.add(addProduct); productMenu.add(modifyProduct); productMenu.add(deleteProduct); productMenu.add(queryProduct); userMenu.add(addUser); userMenu.add(modifyUser); userMenu.add(deleteUser); userMenu.add(queryUser); orderMenu.add(queryOrder); orderMenu.add(deleteOrder); orderMenu.add(modifyOrder); purchaseMenu.add(buyProduct); // 将菜单添加到菜单栏 menuBar.add(productMenu); menuBar.add(userMenu); menuBar.add(orderMenu); menuBar.add(purchaseMenu); // 设置菜单栏 setJMenuBar(menuBar); // 添加菜单项的事件监听器 addProduct.addActionListener(e -> { new AddProductFrame().setVisible(true); }); modifyProduct.addActionListener(e -> { new ModifyProductFrame().setVisible(true); }); queryProduct.addActionListener(e -> { new QueryProductFrame().setVisible(true); }); // 其他菜单项的监听器可以类似地添加 } } // 商品新增窗口 public class AddProductFrame extends JFrame { public AddProductFrame() { setTitle("商品新增"); setSize(400, 600); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLocationRelativeTo(null); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 商品ID JLabel productIdLabel = new JLabel("商品ID:"); JTextField productIdText = new JTextField(); productIdText.setPreferredSize(new Dimension(200, 25)); JPanel productIdPanel = new JPanel(); productIdPanel.add(productIdLabel); productIdPanel.add(productIdText); // 商品名称 JLabel productNameLabel = new JLabel("商品名称:"); JTextField productNameText = new JTextField(); productNameText.setPreferredSize(new Dimension(200, 25)); JPanel productNamePanel = new JPanel(); productNamePanel.add(productNameLabel); productNamePanel.add(productNameText); // 商品价格 JLabel productPriceLabel = new JLabel("商品价格:"); JTextField productPriceText = new JTextField(); productPriceText.setPreferredSize(new Dimension(200, 25)); JPanel productPricePanel = new JPanel(); productPricePanel.add(productPriceLabel); productPricePanel.add(productPriceText); // 商品类别 JLabel productCategoryLabel = new JLabel("商品类别:"); JComboBox<String> productCategoryComboBox = new JComboBox<>(new String[]{"", "电子产品", "服装", "食品", "书籍"}); productCategoryComboBox.setPreferredSize(new Dimension(200, 25)); JPanel productCategoryPanel = new JPanel(); productCategoryPanel.add(productCategoryLabel); productCategoryPanel.add(productCategoryComboBox); // 商品产地 JLabel productOriginLabel = new JLabel("商品产地:"); JTextField productOriginText = new JTextField(); productOriginText.setPreferredSize(new Dimension(200, 25)); JPanel productOriginPanel = new JPanel(); productOriginPanel.add(productOriginLabel); productOriginPanel.add(productOriginText); // 商品简介 JLabel productDescriptionLabel = new JLabel("商品简介:"); JTextArea productDescriptionArea = new JTextArea(5, 25); JScrollPane descriptionScrollPane = new JScrollPane(productDescriptionArea); descriptionScrollPane.setPreferredSize(new Dimension(200, 100)); JPanel productDescriptionPanel = new JPanel(); productDescriptionPanel.add(productDescriptionLabel); productDescriptionPanel.add(descriptionScrollPane); // 按钮面板 JButton saveButton = new JButton("保存"); JButton cancelButton = new JButton("取消"); JPanel buttonPanel = new JPanel(); buttonPanel.add(saveButton); buttonPanel.add(cancelButton); // 添加所有面板到主面板 panel.add(productIdPanel); panel.add(productNamePanel); panel.add(productPricePanel); panel.add(productCategoryPanel); panel.add(productOriginPanel); panel.add(productDescriptionPanel); panel.add(buttonPanel); // 将主面板添加到窗口 add(panel); // 添加按钮事件监听 saveButton.addActionListener(e -> { Connection conn = null; PreparedStatement pstmt = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password"); String sql = "INSERT INTO shangpin (spMC, spLB, spJG, spCD, spJJ) VALUES (?, ?, ?, ?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, productNameText.getText()); pstmt.setString(2, productCategoryComboBox.getSelectedItem().toString()); pstmt.setFloat(3, Float.parseFloat(productPriceText.getText())); pstmt.setString(4, productOriginText.getText()); pstmt.setString(5, productDescriptionArea.getText()); int rowsAffected = pstmt.executeUpdate(); if (rowsAffected > 0) { JOptionPane.showMessageDialog(this, "商品新增成功!"); } } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "商品新增失败!"); ex.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } }); cancelButton.addActionListener(e -> { dispose(); }); } } // 商品修改窗口 public class ModifyProductFrame extends JFrame { public ModifyProductFrame() { setTitle("商品修改"); setSize(400, 600); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLocationRelativeTo(null); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 商品ID查询 JLabel searchIdLabel = new JLabel("请输入商品ID:"); JTextField searchIdText = new JTextField(); searchIdText.setPreferredSize(new Dimension(200, 25)); JButton searchButton = new JButton("查询"); JPanel searchPanel = new JPanel(); searchPanel.add(searchIdLabel); searchPanel.add(searchIdText); searchPanel.add(searchButton); // 商品ID显示 JLabel productIdLabel = new JLabel("商品ID:"); JTextField productIdText = new JTextField(); productIdText.setEditable(false); productIdText.setPreferredSize(new Dimension(200, 25)); JPanel productIdPanel = new JPanel(); productIdPanel.add(productIdLabel); productIdPanel.add(productIdText); // 商品名称 JLabel productNameLabel = new JLabel("商品名称:"); JTextField productNameText = new JTextField(); productNameText.setPreferredSize(new Dimension(200, 25)); JPanel productNamePanel = new JPanel(); productNamePanel.add(productNameLabel); productNamePanel.add(productNameText); // 商品价格 JLabel productPriceLabel = new JLabel("商品价格:"); JTextField productPriceText = new JTextField(); productPriceText.setPreferredSize(new Dimension(200, 25)); JPanel productPricePanel = new JPanel(); productPricePanel.add(productPriceLabel); productPricePanel.add(productPriceText); // 商品类别 JLabel productCategoryLabel = new JLabel("商品类别:"); JComboBox<String> productCategoryComboBox = new JComboBox<>(new String[]{"", "电子产品", "服装", "食品", "书籍"}); productCategoryComboBox.setPreferredSize(new Dimension(200, 25)); JPanel productCategoryPanel = new JPanel(); productCategoryPanel.add(productCategoryLabel); productCategoryPanel.add(productCategoryComboBox); // 商品产地 JLabel productOriginLabel = new JLabel("商品产地:"); JTextField productOriginText = new JTextField(); productOriginText.setPreferredSize(new Dimension(200, 25)); JPanel productOriginPanel = new JPanel(); productOriginPanel.add(productOriginLabel); productOriginPanel.add(productOriginText); // 商品简介 JLabel productDescriptionLabel = new JLabel("商品简介:"); JTextArea productDescriptionArea = new JTextArea(5, 25); JScrollPane descriptionScrollPane = new JScrollPane(productDescriptionArea); descriptionScrollPane.setPreferredSize(new Dimension(200, 100)); JPanel productDescriptionPanel = new JPanel(); productDescriptionPanel.add(productDescriptionLabel); productDescriptionPanel.add(descriptionScrollPane); // 按钮面板 JButton updateButton = new JButton("修改"); JButton deleteButton = new JButton("删除"); JButton exitButton = new JButton("取消"); JPanel buttonPanel = new JPanel(); buttonPanel.add(updateButton); buttonPanel.add(deleteButton); buttonPanel.add(exitButton); // 添加所有面板到主面板 panel.add(searchPanel); panel.add(productIdPanel); panel.add(productNamePanel); panel.add(productPricePanel); panel.add(productCategoryPanel); panel.add(productOriginPanel); panel.add(productDescriptionPanel); panel.add(buttonPanel); // 将主面板添加到窗口 add(panel); // 添加按钮事件监听 searchButton.addActionListener(e -> { String productId = searchIdText.getText(); if (productId.isEmpty()) { JOptionPane.showMessageDialog(this, "请输入商品ID!"); return; } Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password"); String sql = "SELECT * FROM shangpin WHERE spID = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, productId); rs = pstmt.executeQuery(); if (rs.next()) { productIdText.setText(rs.getString("spID")); productNameText.setText(rs.getString("spMC")); productCategoryComboBox.setSelectedItem(rs.getString("spLB")); productPriceText.setText(rs.getString("spJG")); productOriginText.setText(rs.getString("spCD")); productDescriptionArea.setText(rs.getString("spJJ")); } else { JOptionPane.showMessageDialog(this, "未找到商品ID为 " + productId + " 的商品信息"); } } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "查询失败!"); ex.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } }); updateButton.addActionListener(e -> { Connection conn = null; PreparedStatement pstmt = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password"); String sql = "UPDATE shangpin SET spMC = ?, spLB = ?, spJG = ?, spCD = ?, spJJ = ? WHERE spID = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, productNameText.getText()); pstmt.setString(2, productCategoryComboBox.getSelectedItem().toString()); pstmt.setFloat(3, Float.parseFloat(productPriceText.getText())); pstmt.setString(4, productOriginText.getText()); pstmt.setString(5, productDescriptionArea.getText()); pstmt.setString(6, productIdText.getText()); int rowsAffected = pstmt.executeUpdate(); if (rowsAffected > 0) { JOptionPane.showMessageDialog(this, "商品修改成功!"); } } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "商品修改失败!"); ex.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } }); deleteButton.addActionListener(e -> { Connection conn = null; PreparedStatement pstmt = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password"); String sql = "DELETE FROM shangpin WHERE spID = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, productIdText.getText()); int rowsAffected = pstmt.executeUpdate(); if (rowsAffected > 0) { JOptionPane.showMessageDialog(this, "商品删除成功!"); dispose(); } } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "商品删除失败!"); ex.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } }); exitButton.addActionListener(e -> { dispose(); }); } } // 商品查询窗口 public class QueryProductFrame extends JFrame { public QueryProductFrame() { setTitle("商品查询"); setSize(800, 600); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLocationRelativeTo(null); // 北部面板 - 查询条件 JPanel northPanel = new JPanel(); northPanel.setLayout(new FlowLayout()); JLabel conditionLabel = new JLabel("查询条件:"); northPanel.add(conditionLabel); JButton queryAllButton = new JButton("查询所有商品信息"); northPanel.add(queryAllButton); JButton queryDepartmentButton = new JButton("查询部门员工信息"); northPanel.add(queryDepartmentButton); JLabel departmentLabel = new JLabel("部门:"); JComboBox<String> departmentComboBox = new JComboBox<>(new String[]{"", "设计部", "开发部", "市场部", "人事部"}); northPanel.add(departmentLabel); northPanel.add(departmentComboBox); // 中部面板 - 查询结果表格 JPanel centerPanel = new JPanel(); centerPanel.setLayout(new BorderLayout()); String[] columnNames = {"商品ID", "商品名称", "商品类别", "商品价格", "商品产地", "商品简介"}; Object[][] data = {}; JTable table = new JTable(data, columnNames); JScrollPane scrollPane = new JScrollPane(table); centerPanel.add(scrollPane, BorderLayout.CENTER); // 添加面板到窗口 add(northPanel, BorderLayout.NORTH); add(centerPanel, BorderLayout.CENTER); // 添加按钮事件监听 queryAllButton.addActionListener(e -> { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password"); String sql = "SELECT * FROM shangpin"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); // 获取查询结果并更新表格数据 ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); Object[][] newData = new Object[0][]; int rowCount = 0; while (rs.next()) { Object[] row = new Object[columnCount]; for (int i = 0; i < columnCount; i++) { row[i] = rs.getObject(i + 1); } newData = Arrays.copyOf(newData, newData.length + 1); newData[newData.length - 1] = row; rowCount++; } table.setModel(new DefaultTableModel(newData, columnNames)); } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "查询所有商品信息失败!"); ex.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } }); queryDepartmentButton.addActionListener(e -> { String department = departmentComboBox.getSelectedItem().toString(); if (department.isEmpty()) { JOptionPane.showMessageDialog(this, "请选择部门!"); return; } JOptionPane.showMessageDialog(this, "查询 " + department + " 的商品信息成功!"); }); } } }为什么显示无法找到jdbc
最新发布
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值