如何设置自定义JOptionPane的响应?

时间:2014-03-12 12:10:55

标签: java user-input joptionpane

假设你有这个JOptionPane:

JOptionPane.showOptionDialog(null, 
    "Do you like this answer?", 
    "Feedback", 
    JOptionPane.OK_CANCEL_OPTION, 
    JOptionPane.INFORMATION_MESSAGE, 
    null, 
    new String[]{"Yes I do", "No I don't"}, // this is the array
    "default");

您如何设置答案才能显示?例如,你怎么称呼“是的我做”,你怎么称呼“不,我不”?我只是不确定如何让按钮做我想让他们做的事情。

2 个答案:

答案 0 :(得分:1)

选项对话框返回一个int以确定按下了哪个按钮

int result = JOptionPane.showOptionDialog(null, 
            "Do you like this answer?", 
            "Feedback", 
            JOptionPane.OK_CANCEL_OPTION, 
            JOptionPane.INFORMATION_MESSAGE, 
            null, 
            new String[]{"Yes I do", "No I don't"}, // this is the array
            "default");

    if(result == 0) { //0 is 'Yes I do' option
        //do stuff
    }

答案 1 :(得分:1)

静态方法不会让你到那里。您将不得不依赖具有类似参数列表的构造函数:

JOptionPane optionPane = new JOptionPane("Do you like this answer?", OptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, new String[]{"Yes I do", "No I don't"},"default");
optionPane.setVisible(true);
optionPane.setInputValue("Yes I do");