繁体   English   中英

JOptionPane 选项被点击后可以做些什么吗?

[英]Can JOptionPane options do something after being clicked?

我有一个 JOptionPane 我今天早上一直在使用,因为我正在尝试学习 Swing,我一直在查看 oracle 的参考页面,我得到的最远的是制作 2 个按钮,当您单击其中一个按钮时. 他们关闭(所以他们做同样的事情?)有没有一种方法可以检查何时单击按钮并执行某些操作(例如打印某些内容?)

我的代码有效,我只需要下一步做什么的帮助。

    import javax.swing.JOptionPane;

    public class Main {

    public static void main(String[] args) {
        JOptionPane frame = new JOptionPane();
      //Custom button text
        
        Object[] options = {"Barack",
                            "Obama"};
        int n = JOptionPane.showOptionDialog(frame,
            "His name is..",
            "Obamas name is?",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,     //do not use a custom Icon
            options,  //the titles of buttons
            options[0]); //default button title
    }
  }

Output 运行时:

框架

您可以使用 YES_OPTION 和 NO_OPTION 之类的方法检查是否按下了 JOptionPane 按钮。

if(n == OptionPane.YES_OPTION){
    //do this
}

这对 NO_OPTION 和 OK_OPTION 都是一样的。 希望这会有所帮助:)

暂无
暂无

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

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