简体   繁体   English

从Jframe打开一定时间后如何关闭JDialog

[英]How to close a JDialog after a certain period of timeopened from a Jframe

So I am trying to make a shopping cart app using Java Swing forms with MYSQL JDBC Connectivity, In one part of the app, when a user clicks a button like "View Product", it takes time for the machine to load the information from the database and output in the app, so in that time I want to show a loading icon until the work is done.To achieve this I created a separate JDialog to be used to show a loading gif, and open it from a JFrame on the ActionPerformed event of a button. 因此,我试图使用带有Java SQL Swing表单和MYSQL JDBC Connectivity的购物车应用程序,在该应用程序的一部分中,当用户单击“查看产品”之类的按钮时,机器需要花费一些时间才能从中加载信息。数据库和应用程序中的输出,因此在那个时候我想显示一个加载图标,直到工作完成。为此,我创建了一个单独的JDialog用于显示加载gif,然后从ActionPerformed上的JFrame中打开它按钮事件。 Here's my work: 这是我的工作:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JDialog dialog = new loader(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
        Timer timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dialog.dispose();
            }
        });
        timer.setRepeats(false);
        timer.start();
    }

But using a Swing Timer to close the JDialog after 1sec, as suggested by other QA on Stack Overflow, doesn't work as expected, so is there any way to achieve this task, that is, to closethe JDialog window after a certain period of time? 但是,如Stack Overflow上的其他QA所建议的那样,使用Swing计时器在1秒后关闭JDialog并不能按预期工作,因此,有任何方法可以完成此任务,即在一定时间后关闭JDialog窗口。时间?

Your dialog is modal. 您的对话框是模态的。 So the setVisible(true) method call doesn't return until the dialog has been closed. 因此,在关闭对话框之前,不会返回setVisible(true)方法调用。 So the timer is only created once the dialog is closed. 因此,仅在关闭对话框后才创建计时器。

You need to create it and start it before making the dialog visible. 使对话框可见之前 ,您需要创建并启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM