如何将现有按钮应用到 yes/no 个按钮?
how to apply existed buttons to yes/no button(s)?
我有一个带有 "Add new user" 按钮的 JFrame,一个扩展 JPanel
的 AddUser class
AddUser 内部 class 有名称、工作、....、添加和 "Clear all" 按钮
我正在使用 showOptionDialog 打开 AddUser
AddUser addPanel = new AddUser();
JButton[] temp = new JButton[1];
temp[0] = AddUser.jButton1; //this is add button
int option = JOptionPane.showOptionDialog(rootPane, addPanel,"Add new user"
,JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE, null, temp, null);
showOptionDialog 需要 Object[] 所以我创建了 JButton[] 然后将添加按钮放入
它确实打开了添加面板,但是当我点击添加按钮时,面板没有关闭
如果您将 JButton 组件作为选项提供给 JOptionDialog,则需要在 Buttons 动作侦听器中手动设置用户选择的值。
这在(我从答案 2 中复制了 getOptionPane 代码)中进行了描述:
- JOptionPane Passing Custom Buttons
- Disable ok button on JOptionPane.dialog until user gives an input
下面是一些示例代码,它为 JOptionDialog 使用自定义 JButton:
class OptionPanetest {
public static void main(String[] args) {
new OptionPanetest();
}
protected JOptionPane getOptionPane(JComponent parent) {
JOptionPane pane = null;
if (!(parent instanceof JOptionPane)) {
pane = getOptionPane((JComponent)parent.getParent());
} else {
pane = (JOptionPane) parent;
}
return pane;
}
public OptionPanetest() {
JFrame testFrame = new JFrame();
testFrame.setSize(500, 500);
JPanel addPanel = new JPanel();
addPanel.setLayout(new GridLayout(1,1));
JButton testButton = new JButton("New Button");
testButton.addActionListener(e -> {
System.out.println("Test Button Was Pressed");
JButton testButtonInstance = (JButton)e.getSource();
JOptionPane pane = getOptionPane(testButtonInstance);
pane.setValue(testButtonInstance);
});
testFrame.setLocationRelativeTo(null);
testFrame.setVisible(true);
Object[] options = {testButton};
int option = JOptionPane.showOptionDialog(null, "This is a test", "Add Panel Test", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
}
}
我有一个带有 "Add new user" 按钮的 JFrame,一个扩展 JPanel
的 AddUser classAddUser 内部 class 有名称、工作、....、添加和 "Clear all" 按钮
我正在使用 showOptionDialog 打开 AddUser
AddUser addPanel = new AddUser();
JButton[] temp = new JButton[1];
temp[0] = AddUser.jButton1; //this is add button
int option = JOptionPane.showOptionDialog(rootPane, addPanel,"Add new user"
,JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE, null, temp, null);
showOptionDialog 需要 Object[] 所以我创建了 JButton[] 然后将添加按钮放入
它确实打开了添加面板,但是当我点击添加按钮时,面板没有关闭
如果您将 JButton 组件作为选项提供给 JOptionDialog,则需要在 Buttons 动作侦听器中手动设置用户选择的值。
这在(我从答案 2 中复制了 getOptionPane 代码)中进行了描述:
- JOptionPane Passing Custom Buttons
- Disable ok button on JOptionPane.dialog until user gives an input
下面是一些示例代码,它为 JOptionDialog 使用自定义 JButton:
class OptionPanetest {
public static void main(String[] args) {
new OptionPanetest();
}
protected JOptionPane getOptionPane(JComponent parent) {
JOptionPane pane = null;
if (!(parent instanceof JOptionPane)) {
pane = getOptionPane((JComponent)parent.getParent());
} else {
pane = (JOptionPane) parent;
}
return pane;
}
public OptionPanetest() {
JFrame testFrame = new JFrame();
testFrame.setSize(500, 500);
JPanel addPanel = new JPanel();
addPanel.setLayout(new GridLayout(1,1));
JButton testButton = new JButton("New Button");
testButton.addActionListener(e -> {
System.out.println("Test Button Was Pressed");
JButton testButtonInstance = (JButton)e.getSource();
JOptionPane pane = getOptionPane(testButtonInstance);
pane.setValue(testButtonInstance);
});
testFrame.setLocationRelativeTo(null);
testFrame.setVisible(true);
Object[] options = {testButton};
int option = JOptionPane.showOptionDialog(null, "This is a test", "Add Panel Test", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
}
}