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

Java JOptionPane上自定义创建的按钮不起作用?,java,swing,button,joptionpane,Java,Swing,Button,Joptionpane,我已将自己的JPanel和JButton添加到JOptionPane,如下所示。 当我点击“确定”按钮时,没有显示任何内容。除此之外还有别的选择吗?我只想获取用户的用户名和密码,但我的按钮不是JOptionpane中的默认按钮。 有人能看出这个代码有什么问题吗 final WebTextField user = new WebTextField(); final WebPasswordField password = new WebPasswordField(); WebButt

我已将自己的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 listener,使其成为listent以单击事件等

以如下方式修改代码:

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"
有帮助的报告


祝你好运

要更快地获得更好的帮助,请发布一条消息。@itro:你的键盘工作正常吗?键入时,您总是会遗漏文本中的某些字符。此外,您将初始化WebButton两次,一次在您提供的代码开头,一次在showOptionDIalog()中;出于某种原因,一定要检查一下。不确定,但似乎它们都指的是同一件事!!当做