如何对自定义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");

您将如何为此设置答案?例如,您将如何称呼"是我做",如何称呼"不,我不"?我只是不确定如何使按钮做我想做的事情。

选项对话框返回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
    }

静态方法不会使您到达那里。相反,您必须依靠具有类似参数列表的构造函数:

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

相关内容

  • 没有找到相关文章