[英]JoptionPane ShowConfirmDialog
我有一个Java程序。 当我运行程序时,它会给我一个GUI,我附上。
当我想关闭它时,它会提示确认对话框。 如果我按下是按钮,它将使用System.exit()
退出程序。
public static void main(String args[])
{
ButtonTest app = new ButtonTest( );
app.addWindowListener(
new WindowAdapter( )
{
public void windowClosing (WindowEvent e)
{
String message = " Really Quit ? ";
String title = "Quit";
int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
);
}
如果我不想退出该计划,我该怎么办? System.continued()
?
在这种情况下你不需要别的
试试这个,
app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
将帖子
那么,你的代码会变成这样的,
public static void main(String args[]) {
ButtonTest app = new ButtonTest();
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int reply = JOptionPane.showConfirmDialog(null,
"Really Quit ?", "Quit", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
System.exit(0);
}
});
app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
app.setSize(640, 480);
app.setVisible(true);
}
[说明]
你可能会想到为什么会这样。 与Frame
不同, JFrame
的Windows关闭按钮的行为是隐藏窗口。 因此,无论如何它都会隐藏/关闭窗口。 但是当您指定它还必须退出程序时,当用户单击yes
。 然后,除了关闭窗口,它还退出程序。 当用户点击“ no
,它无论如何都会关闭窗口。 因此,您必须明确告诉它DO_NOTHING_ON_CLOSE
。
[文件]
与Frame不同,JFrame有一些关于当用户试图关闭窗口时如何响应的概念。 默认行为是在用户关闭窗口时隐藏JFrame。 要更改默认行为,请调用setDefaultCloseOperation(int)方法。 要使JFrame的行为与Frame实例相同,请使用setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)。
参考: JFrame文档
如果您要问我,我将继续使用YES SELECTION
而不是突然关闭我的应用程序与System.exit(0)
,我将选择通过使用frameObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
关闭我的应用程序的优雅方式,在NO SELECTION
,我将使用frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
。 以下是一个示例程序供您使用:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ApplicationCloseExample
{
private void displayGUI()
{
final JFrame frame = new JFrame("Application Close Example");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
int result = JOptionPane.showConfirmDialog(
frame, "Do you want to Exit ?"
, "Exit Confirmation : ", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else if (result == JOptionPane.NO_OPTION)
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
});
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ApplicationCloseExample().displayGUI();
}
});
}
}
如果您希望程序在按NO时继续,请将其余代码放在else块中,或者调用已放置其余代码的函数。
如果您不想在NO按钮上放置任何操作,则删除else块也是一个选项,因为JOptionPane.showConfirmDialog()将会关闭。 您可以在if语句后继续使用其余代码。
仅供参考 - 没有System.continue()。 该程序几乎就是这样做的。
您可以添加else
块。 如果你想再次运行main方法(我假设你这样做)它应该是这样的。 如果用户选择no,您应该运行一些方法,无论是main方法main(null)
还是其他方法。
public static void main(String args[])
{
ButtonTest app = new ButtonTest( );
app.addWindowListener(
new WindowAdapter( )
{
public void windowClosing (WindowEvent e)
{
String message = " Really Quit ? ";
String title = "Quit";
int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
System.exit(0);
}
else
{
//whatever you plan on running instead here, instead of quitting,
//main(null) to run the main method, or put another method if you want
}
}
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.