我想更改此代码以仅显示“OK”并删除取消按钮 [英] I would like to change this code to display only "OK" and delete the cancel button

查看:65
本文介绍了我想更改此代码以仅显示“OK”并删除取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改此代码以仅显示确定并删除取消按钮。

I would like to change this code to display only "OK" and delete the cancel button.

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);


这里是图片,我想要它,但没有取消按钮,谢谢!

Here it is the picture, I want it exactly as this but without the Cancel button, thanks!

推荐答案

我做了一些实验。使用 showInputDialog 很容易在下拉列表(组合框)中显示答案,但似乎无法删除取消按钮。

I did some experimenting. It's easy enough to use showInputDialog to show the answers in a dropdown list (combobox) but it does not seem to be possible to remove the Cancel button.

相反,你可以使用 showConfirmDialog ,其中'message'不是一个简单的String,而是一个包含以下内容的可视面板:(1)一个标签对于这个问题; (2)组合框用于答案。我把它包装成一个让它更容易使用的方法:

Instead you can use showConfirmDialog, where the 'message' is not a simple String, but a visual panel containing: (1) a label for the question; (2) a combobox for the answers. I've wrapped this up into a method to make it easier to use:

static int showQuestion(String dialogTitle, String question, String[] answers) {
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(new JLabel(question), BorderLayout.NORTH);
    JComboBox<String> 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 如果用户没有做出选择,因为他们以某种方式关闭了对话框而没有点击确定。

The showQuestion method returns the 0-based index of the answer the user chose. The dialog has an 'OK' button but no 'Cancel' button; however, there's still a problem: the user can still close the dialog by clicking the 'X' of the dialog, or by right-clicking the titlebar and selecting 'Close' from the popup menu. That has the same effect as 'Cancel'. So, the code above checks for this, and returns -1 if the user did not make a choice because they closed the dialog somehow without clicking 'OK'.

我看不到一种简单的方法来删除对话框的关闭按钮。无论如何它会很烦人,因为它会阻止他们关闭程序或取消测试。最好让用户关闭/取消对话框,如果他们真的想要,并在适当的时候处理这​​种情况。

I can't see an easy way to remove the close button of the dialog. It would be annoying anyway, because it would prevent them from closing the program or cancelling the test. It's best to let the user close/cancel the dialog if they really want to, and handle that situation as appropriate.

此外,它可能更加用户友好作为单选按钮的选择(这些东西:(●)A ()B ()C )而不是下拉列表。这样,用户可以立即读取所有选项而无需额外点击。这是一个替代的 showQuestion 方法,如果你愿意的话。 (它在循环中调用对话框,以防用户在单击确定之前没有选择任何选项。)

Also, it might be more user-friendly to show the choices as radio buttons (these things: (●) A, ( ) B, ( ) C) instead of a dropdown list. That way, the user can read all the choices at once without an extra click. Here's an alternative showQuestion method which does that, if you want. (It calls the dialog in a loop just in case the user did not select any option before clicking 'OK'.)

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;
        }
    }
}






编辑:要直接将答案返回到数组中的索引,请对上面的函数进行一些小的更改:


Edit: To return the answer directly instead of an index into the array, make a few small changes to the function above:


  1. 返回类型字符串而不是 int

  2. 当用户取消
  3. 返回 null 而不是 -1

  4. return answers [comboBox.getSelectedIndex()] 而不只是 comboBox.getSelectedIndex()

  1. return type String instead of int.
  2. return null instead of -1 when the user cancelled it
  3. return answers[comboBox.getSelectedIndex()] instead of just 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<String> 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()];
}

然后,相当于原始代码段:

Then, the equivalent of the original snippet is:

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.",
});

这篇关于我想更改此代码以仅显示“OK”并删除取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆