【问题标题】:JAVA: how to make a popUp window close automatically?JAVA:如何使弹出窗口自动关闭?
【发布时间】:2014-09-27 13:54:12
【问题描述】:

有没有办法让应用程序或 Applet 中的弹出窗口在一段时间(例如 5 秒)后自动关闭?


我找到了解决方案:

对于任何可能正在寻找相同的人:

public static void main(String[] args) {
    JFrame f = new JFrame();
    final JDialog dialog = new JDialog(f, "Test", true);
    Timer timer = new Timer(2000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    timer.setRepeats(false);
    timer.start();

    dialog.setVisible(true); // if modal, application will pause here

    System.out.println("Dialog closed");
}

谢谢你们的回答。

【问题讨论】:

标签: java popup popupwindow


【解决方案1】:

为了自动关闭弹出窗口,您必须设置新线程并在其中设置计时器。

试试下面的代码

public class FrmPopUpInfo extends JDialog{  
public boolean isCancel = false;

private String trackHeader = "Pop Up is:";
private final String message;

public FrmPopUpInfo(String message){
    this.message = message;
    initComponents();
}

private void initComponents() {
    Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen
    Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());// height of the task bar
    setLocation(scrSize.width - 275, scrSize.height - toolHeight.bottom - 120);
    ImageIcon image;

    setSize(225,120);
    setLayout(null);
    setUndecorated(true);
    setLayout(new GridBagLayout());

    GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.weightx = 1.0f;
    constraints.weighty = 1.0f;
    constraints.insets = new Insets(5, 5, 5, 5);
    constraints.fill = GridBagConstraints.BOTH;

    JLabel headingLabel = new JLabel(trackHeader + message);
    image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(FrmPopUpInfo.class.getResource("/images/yourImage.jpg")));
    headingLabel .setIcon(image);
    headingLabel.setOpaque(false);

    add(headingLabel, constraints);

    constraints.gridx++;
    constraints.weightx = 0f;
    constraints.weighty = 0f;
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.NORTH;
    JButton cloesButton = new JButton(new AbstractAction("x") {
        @Override
        public void actionPerformed(final ActionEvent e) {
               dispose();
        }
    });
    cloesButton.setMargin(new Insets(1, 4, 1, 4));
    cloesButton.setFocusable(false);

    add(cloesButton, constraints);

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    setVisible(true);
    setAlwaysOnTop(true);

    new Thread(){
          @Override
          public void run() {
               try {
                      Thread.sleep(5000); // time after which pop up will be disappeared.
                      dispose();
               } catch (InterruptedException e) {
                      e.printStackTrace();
               }
          };
    }.start();
}
}

你像这样从其他框架调用上面的类

FrmPopUpInfo frm = new FrmPopUpInfo(); 

弹出对话框将在 5 秒后消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2018-02-13
    • 1970-01-01
    相关资源
    最近更新 更多