如何更改确认对话框中的是/否选项?

11

我想将“是”和“否”更改为类似于“同意/不同意”的内容。我该怎么做?

int reply = JOptionPane.showConfirmDialog(null,
                                          "Are you want to continue the process?",
                                          "YES?",
                                          JOptionPane.YES_NO_OPTION);

5个回答

31

您可以执行以下操作

JFrame frame = new JFrame();
String[] options = new String[2];
options[0] = "Agree";
options[1] = "Disagree";
JOptionPane.showOptionDialog(frame.getContentPane(), "Message!", "Title", 0, JOptionPane.INFORMATION_MESSAGE, null, options, null);

输出结果如下

enter image description here

有关showOptionDialog()函数的更多详细信息,请参见此处


是的,这个功能正常工作,但有没有办法更改“同意”和“不同意”按钮的字体?我想实现国际化应用程序。 - Disapamok

7
您可以使用options参数将自定义选项推送到showOptionDialog;
Object[] options = { "Agree", "Disagree" };
JOptionPane.showOptionDialog(null, "These are my terms", "Terms",
    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, 
    options, options[0]);

它适用于JOptionPane.showOptionDialog并进行更改,但我想要更改为JOptionPane.showConfirmDialog是否以相同的方式进行更改? - Kashama Shinn

3

试试这个:

查看JOptionPane文档

JOptionPane(Object message, int messageType, int optionType,
        Icon icon, Object[] options, Object initialValue)

选项(options)指定了带有initialValue的按钮,因此您可以更改它们。

示例

Object[] options = { "Agree", "Disagree" };

JOptionPane.showOptionDialog(null, "Are you want to continue the process?", "information",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);

3

2

试试这个!

int res = JOptionPane.showConfirmDialog(null, "Are you want to continue the process?", "", JOptionPane.YES_NO_OPTION);
        switch (res) {
            case JOptionPane.YES_OPTION:
            JOptionPane.showMessageDialog(null, "Process Successfully");
            break;
            case JOptionPane.NO_OPTION:
            JOptionPane.showMessageDialog(null, "Process is Canceled");
            break;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接