在 JOptionPane 中设置默认关闭操作
Setting a default close action in JOptionPane
如何设置红色'X'按钮关闭应用程序?我知道如何在 JFrame 中执行此操作,但我不知道如何在 JOptionPane 中进行设置。
目前,单击红色 'X' 会初始化游戏,而不是退出应用程序。
JOptionPane.showMessageDialog(null, panel, "Let's Play!", JOptionPane.QUESTION_MESSAGE, icon);
使用 ConfirmDialog
。根据操作,您会得到不同的值 (-1, 0, 1)。
int i = JOptionPane.showConfirmDialog(null, "Test", "test", JOptionPane.YES_NO_OPTION);
System.out.println(i);
"X" returns -1
, "Yes" returns 0
和 "No" returns 1
。现在可以根据数值选择是否开始游戏了
自己写JOptionDialog
并观察结束事件:
import javax.swing.*;
import java.awt.event.*;
public class MyOwnCloseOption extends JOptionPane{
//just to keep the names compliant
public static String panel = "Your Message!";
private Runnable closingRoutine;
public MyOwnCloseOption(Runnable closingRoutine){
super(panel, JOptionPane.QUESTION_MESSAGE);
this.closingRoutine = closingRoutine;
}
public void showMessage(){
JDialog dialog = createDialog("Let's Play!");
dialog.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
closingRoutine.run();
}
});
//From Documentation (Java SE7): java.awt.Dialog:
//"setVisible(true): If the dialog is not already visible, this call will not return until the dialog is hidden by calling setVisible(false) or dispose"
dialog.setVisible(true);
dialog.dispose();
}
public static void main(String[] args){
//The Original code
//JOptionPane.showMessageDialog(null, panel, JOptionPane.QUESTION_MESSAGE);
MyOwnCloseOption myOwnCloseOption = new MyOwnCloseOption(new Runnable(){
@Override
public void run(){
System.out.println("Okay. -______-");
System.exit(0);
}
});
myOwnCloseOption.showMessage();
System.out.println("Starting the Game!");
//something keeps the application still alive?
System.exit(0);
}
}
如何设置红色'X'按钮关闭应用程序?我知道如何在 JFrame 中执行此操作,但我不知道如何在 JOptionPane 中进行设置。
目前,单击红色 'X' 会初始化游戏,而不是退出应用程序。
JOptionPane.showMessageDialog(null, panel, "Let's Play!", JOptionPane.QUESTION_MESSAGE, icon);
使用 ConfirmDialog
。根据操作,您会得到不同的值 (-1, 0, 1)。
int i = JOptionPane.showConfirmDialog(null, "Test", "test", JOptionPane.YES_NO_OPTION);
System.out.println(i);
"X" returns -1
, "Yes" returns 0
和 "No" returns 1
。现在可以根据数值选择是否开始游戏了
自己写JOptionDialog
并观察结束事件:
import javax.swing.*;
import java.awt.event.*;
public class MyOwnCloseOption extends JOptionPane{
//just to keep the names compliant
public static String panel = "Your Message!";
private Runnable closingRoutine;
public MyOwnCloseOption(Runnable closingRoutine){
super(panel, JOptionPane.QUESTION_MESSAGE);
this.closingRoutine = closingRoutine;
}
public void showMessage(){
JDialog dialog = createDialog("Let's Play!");
dialog.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
closingRoutine.run();
}
});
//From Documentation (Java SE7): java.awt.Dialog:
//"setVisible(true): If the dialog is not already visible, this call will not return until the dialog is hidden by calling setVisible(false) or dispose"
dialog.setVisible(true);
dialog.dispose();
}
public static void main(String[] args){
//The Original code
//JOptionPane.showMessageDialog(null, panel, JOptionPane.QUESTION_MESSAGE);
MyOwnCloseOption myOwnCloseOption = new MyOwnCloseOption(new Runnable(){
@Override
public void run(){
System.out.println("Okay. -______-");
System.exit(0);
}
});
myOwnCloseOption.showMessage();
System.out.println("Starting the Game!");
//something keeps the application still alive?
System.exit(0);
}
}