Java 定时关闭弹出的对话框

自动关闭JOptionPane弹窗

JOptionPane定时关闭。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package adbs.main.ui.jpanels.auto;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class AutoCloseJOption {

private static final int TIME_VISIBLE = 3000;

public static void main(String[] args) {

final JFrame frame1 = new JFrame("My App");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(100, 100);
frame1.setLocation(100, 100);

JButton button = new JButton("My Button");
frame1.getContentPane().add(button);

button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane pane = new JOptionPane("Message", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(null, "Title");
dialog.setModal(false);
dialog.setVisible(true);

new Timer(TIME_VISIBLE, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
}).start();
}
});

frame1.setVisible(true);

}
}

参考资料

https://www.w3cschool.cn/java/codedemo-484052162.html
https://cloud.tencent.com/developer/ask/sof/102428944