如何暂停 WindowClosing |框架
How to pause on WindowClosing | JFrame
我想做的是,当应用程序使用标题栏中的 X 按钮关闭时,将标签更改为“正在关闭”,暂停 1500 毫秒然后关闭。
代码
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
public class app {
public static void main(String[] args) {
final Timer timer = new Timer(1500, new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
JDialog dialog = new JDialog(new JFrame(), "", true);
dialog.setLayout(new FlowLayout());
JLabel label = new JLabel();
label.setText("Running");
dialog.add(label);
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event) {
label.setText("Closing");
timer.start();
}
});
dialog.pack();
dialog.setVisible(true);
}
}
问题是它甚至不会暂停
我试过了
使用Thread.sleep()
。睡眠时标签不会改变
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event) {
label.setText("Closing");
Thread.sleep(1500);
System.exit(0);
}
});
由于您在计时器中使用了 'exit' 进程,请指定默认关闭操作不关闭表单。
dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
我想做的是,当应用程序使用标题栏中的 X 按钮关闭时,将标签更改为“正在关闭”,暂停 1500 毫秒然后关闭。
代码
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
public class app {
public static void main(String[] args) {
final Timer timer = new Timer(1500, new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
JDialog dialog = new JDialog(new JFrame(), "", true);
dialog.setLayout(new FlowLayout());
JLabel label = new JLabel();
label.setText("Running");
dialog.add(label);
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event) {
label.setText("Closing");
timer.start();
}
});
dialog.pack();
dialog.setVisible(true);
}
}
问题是它甚至不会暂停
我试过了
使用Thread.sleep()
。睡眠时标签不会改变
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event) {
label.setText("Closing");
Thread.sleep(1500);
System.exit(0);
}
});
由于您在计时器中使用了 'exit' 进程,请指定默认关闭操作不关闭表单。
dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);