利用Java的setVisible方法隐藏或显示对话框
导读:import javax.swing.*; public class DialogExample { public static void main(String[] args { JFrame frame =...
import javax.swing.*;
public class DialogExample {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300, 200);
JButton button = new JButton("Show Dialog");
button.addActionListener(e ->
{
JOptionPane.showMessageDialog(frame, "Hello, this is a dialog!");
}
);
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Hide the dialog after 3 seconds
Timer timer = new Timer(3000, e ->
{
Window[] windows = Window.getWindows();
for (Window window : windows) {
if (window instanceof JDialog) {
window.setVisible(false);
}
}
}
);
timer.setRepeats(false);
timer.start();
}
}
在上面的示例中,我们首先创建一个JFrame并在其上放置一个按钮。当点击按钮时,会显示一个JOptionPane对话框。然后通过定时器在3秒后将对话框隐藏。在定时器的回调函数中,我们遍历所有窗口,如果窗口是JDialog类型,则将其设置为不可见。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 利用Java的setVisible方法隐藏或显示对话框
本文地址: https://pptw.com/jishu/695456.html
