简体   繁体   English

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

[英]How to set a response to custom JOptionPane?

Say you have this 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");

How would you set an answer to this to display? 你会如何设置答案来展示? For example, how would you call the "Yes I do" and how would you call the "No I don't" ? 例如,你怎么称呼“是的我做”,你怎么称呼“不,我不”? I'm just not sure how to make the buttons do what I want them to do. 我只是不确定如何让按钮做我想让他们做的事情。

The option dialog return an int to determine what button have been pressed 选项对话框返回一个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
    }

The static methods won't get you there. 静态方法不会让你到那里。 You will instead have to rely on the constructor with a similar parameter list: 您将不得不依赖具有类似参数列表的构造函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM