我想在有人关闭框架时添加一个选项,以检查是否有任何线程在运行,是否有任何线程我想显示一个对话框,该对话框应询问用户是否真的要退出。问题是无论用户选择哪个选项,程序都将关闭。
这是我尝试过的:
private void addActionWhenFrameIsClosed( )
{
addWindowListener( new WindowAdapter( )
{
public void windowClosing( WindowEvent e )
{
if( isThereAnyThreadRunning( ) )
{
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog( null, "There are some threads running... "
+ "\nAre you sure you want to exit?",
"Warning", dialogButton );
if( dialogResult == JOptionPane.YES_OPTION)
{
dispose( );
}
}
}
} );
}
答案 0 :(得分:0)
请尝试将这一行代码添加为addActionWhenFrameIsClosed
的第一行
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);