我需要Java中的JOptionPane帮助。当我单击确定时一切正常。 :)但是,当我点击x同样像我点击确定。我该怎么解决这个问题。当我点击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();
}
}
}
答案 0 :(得分:2)
使用另一个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");
}
}
}