【问题标题】:JOptionPane in java x optionjava x 选项中的 JOptionPane
【发布时间】:2015-08-22 21:56:26
【问题描述】:

我需要 Java 中的 JOptionPane 方面的帮助。当我单击确定时,一切正常。 :) 但是当我点击 x 时,就像我点击 OK 一样。我该如何解决这个问题。当我点击 x 我想关闭应用程序。这是我的代码。

import javax.swing.JList;
import javax.swing.JOptionPane;

public class Main {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        @SuppressWarnings("rawtypes")
        JList choise = new JList(new String[] { "Enter the complaints",
                "View complaints" });
        JOptionPane.showMessageDialog(null, choise, "Multi-Select Example",
                JOptionPane.PLAIN_MESSAGE);
        if (choise.getSelectedIndex() == 0) {
            new ComplaintsApp();
        } else if(choise.getSelectedIndex() == 1 && JOptionPane.OK_OPTION > 0){

            new ComplaintsView();
        }
    }
}

【问题讨论】:

    标签: java swing joptionpane


    【解决方案1】:

    使用不同的 JOptionPane,它会返回给程序以指示按下了哪个按钮:JOptionPane.showConfirmDialog(...)

    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    
    public class Main {
        public static void main(String[] args) {
    
            JList<String> choice = new JList<>(new String[] {
                    "Enter the complaints", "View complaints" });
            int result = JOptionPane.showConfirmDialog(null,
                    new JScrollPane(choice), "Multi-select Example",
                    JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (result != JOptionPane.OK_OPTION) {
                // they didn't press OK
                return;
            }
            if (choice.getSelectedIndex() == 0) {
                System.out.println("Complaints App");
            } else if (choice.getSelectedIndex() == 1) {
                System.out.println("Complaints View");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 2012-11-08
      相关资源
      最近更新 更多