我已将自己的 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/