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

时间:2012-01-30 15:59:07

标签: 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"
    );

1 个答案:

答案 0 :(得分:1)

这是非常不寻常的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,你应该为它添加动作监听器以使其能够持续点击事件等;

以这种方式修改代码:

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"

报告帮助

祝你好运