我想更改此代码只显示“确定”并删除取消按钮
我想更改此代码只显示“确定”并删除取消按钮。
Object contestacion5 = JOptionPane.showInputDialog(null, "#5 Que describe mejor a la Norteña?", "Examen Tijuanas PR", //3 JOptionPane.DEFAULT_OPTION, null, new Object[] {"Ensalada de espinacas, tomates, zetas, cebolla, tocineta, aguacate, queso de hoja y tiras de maiz crujientes en vinagreta de la casa.", "Lechuga romana servida con tomate, cebolla, maiz, aguacate, queso de hoja y tiritas de maiz crujientes acompañado de su seleccion de filetes de pollo de res.", "Ensalada vegetariana de nopales, tomates, cebolla, lechuga romana, queso de hoja, aguacate, y aderezo especial de la casa." }, null);
这是图片,我想要它,但没有取消按钮,谢谢!
我做了一些实验。 使用showInputDialog
在下拉列表(combobox)中显示答案很容易,但似乎无法删除“取消”按钮。
相反,你可以使用showConfirmDialog
,其中’message’不是一个简单的String,而是一个包含以下内容的可视面板:(1)问题的标签; (2)combobox用于答案。 我把它包装成一个方法,使它更容易使用:
static int showQuestion(String dialogTitle, String question, String[] answers) { JPanel panel = new JPanel(new BorderLayout()); panel.add(new JLabel(question), BorderLayout.NORTH); JComboBox comboBox = new JComboBox<>(answers); panel.add(comboBox); if (JOptionPane.CLOSED_OPTION == JOptionPane.showConfirmDialog(null, panel, dialogTitle, JOptionPane.DEFAULT_OPTION)) { return -1; } return comboBox.getSelectedIndex(); }
用法示例:
int choice = showQuestion("Examen Tijuanas PR", "#5 Que describe mejor a la Norteña?", new String[] { "Ensalada de espinacas, tomates, zetas, cebolla, tocineta, aguacate, queso de hoja y tiras de maiz crujientes en vinagreta de la casa.", "Lechuga romana servida con tomate, cebolla, maiz, aguacate, queso de hoja y tiritas de maiz crujientes acompañado de su seleccion de filetes de pollo de res.", "Ensalada vegetariana de nopales, tomates, cebolla, lechuga romana, queso de hoja, aguacate, y aderezo especial de la casa.", }); System.out.println("User chose #" + choice);
showQuestion
方法返回用户选择的答案的从0开始的索引。 对话框有一个“确定”按钮,但没有“取消”按钮; 但是,仍然存在一个问题:用户仍然可以通过单击对话框的“X”关闭对话框,或者右键单击标题栏并从弹出菜单中选择“关闭”。 这与“取消”具有相同的效果。 因此,上面的代码检查这个,如果用户没有做出选择则返回-1
,因为他们以某种方式关闭了对话框而没有单击“确定”。
我看不到一种简单的方法来删除对话框的关闭按钮。 无论如何它会很烦人,因为它会阻止他们关闭程序或取消测试。 如果用户真的想要,最好让用户关闭/取消对话,并在适当的时候处理这种情况。
此外,将选项显示为单选按钮(这些东西: (●) A
, ( ) B
, ( ) C
)而不是下拉列表可能更加用户友好。 这样,用户可以立即读取所有选项而无需额外点击。 如果你愿意,这里有一个替代的showQuestion
方法。 (它在循环中调用对话框,以防用户在单击“确定”之前没有选择任何选项。)
static int showQuestion(String dialogTitle, String question, String[] answers) { Box box = new Box(BoxLayout.Y_AXIS); box.add(new JLabel(question)); JRadioButton[] radioButtons = new JRadioButton[answers.length]; ButtonGroup buttonGroup = new ButtonGroup(); for (int i = 0; i < answers.length; i++) { radioButtons[i] = new JRadioButton(answers[i]); buttonGroup.add(radioButtons[i]); box.add(radioButtons[i]); } for (;;) { if (JOptionPane.CLOSED_OPTION == JOptionPane.showConfirmDialog(null, box, dialogTitle, JOptionPane.DEFAULT_OPTION)) { return -1; } for (int i = 0; i < radioButtons.length; i++) { if (radioButtons[i].isSelected()) return i; } } }
编辑 :要直接返回答案而不是索引到数组中,请对上面的函数进行一些小的更改:
- 返回类型
String
而不是int
。 - 当用户取消它时返回
null
而不是-1
- 返回
answers[comboBox.getSelectedIndex()]
而不仅仅是comboBox.getSelectedIndex()
所以它变成了:
static String showQuestion(String dialogTitle, String question, String[] answers) { JPanel panel = new JPanel(new BorderLayout()); panel.add(new JLabel(question), BorderLayout.NORTH); JComboBox comboBox = new JComboBox<>(answers); panel.add(comboBox); if (JOptionPane.CLOSED_OPTION == JOptionPane.showConfirmDialog(null, panel, dialogTitle, JOptionPane.DEFAULT_OPTION)) { return null; } return answers[comboBox.getSelectedIndex()]; }
然后,原始代码段的等价物是:
Object contestacion5 = showQuestion("Examen Tijuanas PR", "#5 Que describe mejor a la Norteña?", new String[] { "Ensalada de espinacas, tomates, zetas, cebolla, tocineta, aguacate, queso de hoja y tiras de maiz crujientes en vinagreta de la casa.", "Lechuga romana servida con tomate, cebolla, maiz, aguacate, queso de hoja y tiritas de maiz crujientes acompañado de su seleccion de filetes de pollo de res.", "Ensalada vegetariana de nopales, tomates, cebolla, lechuga romana, queso de hoja, aguacate, y aderezo especial de la casa.", });