java - JOptionPane 上自定义创建的按钮不起作用?

标签 java swing button joptionpane

我已将自己的 JPanel 和 JButton 添加到 JOptionPane 中,如下所示。 当我单击“确定”按钮时,什么也没有显示。有什么替代方案吗?我只想获取用户的用户名和密码,但我的按钮不是 JOptionpane 的默认按钮。 任何人都可以看出这段代码有什么问题吗?

final WebTextField user = new WebTextField();
    final WebPasswordField password = new WebPasswordField();
    WebButton ok = new WebButton("OK");
    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    System.out.println("Zip file exist, continuing with extraction of zip file");
                }
            });

        }
    });
    WebButton cancel = new WebButton("Cancel");
    WebPanel panel = new WebPanel(new GridLayout(2, 2));
    panel.setOpaque(false);
    panel.add(new WebLabel("User:"));
    panel.add(user);
    panel.add(new WebLabel("Password:"));
    panel.add(password);

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (InstantiationException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IllegalAccessException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    UIManager.put("OptionPane.background", Color.WHITE);
    UIManager.put("Panel.background", Color.WHITE);

    int o =JOptionPane.showOptionDialog(bcfiDownloadPanel,
            new Object[]{panel},
            "Authorization Required",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.INFORMATION_MESSAGE,
            null,
            new WebButton[]{new WebButton("OK"), new WebButton("Cancel")}, // this is the array
            "default"
    );

最佳答案

虽然 JOptionPane 很不寻常...我确实希望 WebButton 是 JButton 的扩展;

int o =JOptionPane.showOptionDialog(bcfiDownloadPanel,
        new Object[]{panel},
        "Authorization Required",
        JOptionPane.OK_CANCEL_OPTION,
        JOptionPane.INFORMATION_MESSAGE,
        null,
        new WebButton[]{new WebButton("OK"), new WebButton("Cancel")}, // this is the array
        "default"

...所以,对于任何 JButton,您应该为其添加 Action 监听器以使其监听单击事件等;

以如下方式修改您的代码:

int o =JOptionPane.showOptionDialog(bcfiDownloadPanel,
            new Object[]{panel},
            "Authorization Required",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.INFORMATION_MESSAGE,
            null,
            new WebButton[]{this.ok, this.cancel}, // this is the array
            "default"

有帮助的报告

祝你好运

关于java - JOptionPane 上自定义创建的按钮不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9066736/

相关文章:

Java 监听器 - 不监听

java - 使用 Hibernate 将 jTable 中的所有行添加到 MS SQL Server DB

css - div 和链接/表单的问题

java - 对优先队列中的这一行感到困惑

java - Selenium Java - 检查元素和页面源显示不同的信息

java - 如何让按钮按下某个键?

c# - 制作一个按钮资源并在多个地方使用它

c# - 按钮内容变化

java - 使用 Google Sheets API v4 将新数据插入到 Google 电子表格顶部

java - 错误 : variable might already have been assigned. 为什么使用 "might"这个词?