【问题标题】:How to implement a delay in ActionListener in Swings?如何在 Swings 中实现 ActionListener 的延迟?
【发布时间】:2014-06-02 06:27:24
【问题描述】:

我有一个 Swing 应用程序,我希望在其中添加一些延迟。我有一个关闭按钮,单击该按钮应显示 JTextArea,其中显示“正在关闭数据库连接....”,然后执行 Database.databaseClose() 方法和 System.exit()。我已经尝试使用 Thread.sleep() 方法,如下面的代码中的延迟。当我执行程序时,屏幕冻结 2 秒,然后关闭而不显示 JTextArea。关闭按钮和 JTextArea 直接添加到 JFrame 中。

我想要的是,在单击关闭按钮时,应立即显示 JTextArea,然后应用程序应延迟 2 秒,然后最终实现 Database.databaseClose() 方法并退出程序。 Database.databaseClose() 方法工作得很好。

我是 Swings 的初学者,如果有人可以修改代码以实现上述要求,我将不胜感激。谢谢!

这里是sn-p的代码:

    JButton btnClose = new JButton("Close");
    btnClose.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {
            JTextArea txtrClosingDatabaseConnections = new JTextArea();

            txtrClosingDatabaseConnections.setText("\r\n\tClosing database connections....");

            getContentPane().add(txtrClosingDatabaseConnections);
            validate();
            repaint();

            /*
             try 
             {
                Thread.sleep(2000);
             }
             catch (InterruptedException e2)
             {
                e2.printStackTrace();
             }
            */

            try 
            {
                Database.databaseClose();
            }
            catch (Exception e1) 
            {
                e1.printStackTrace();
            }

            System.exit(0);

        }
    });

    getContentPane().add(btnClose);

【问题讨论】:

  • 我尝试使用 Thread 方法 sleep()。但它会在指定时间冻结 GUI 屏幕并退出。 JTextArea 永远不会显示

标签: java swing


【解决方案1】:

Hej,这是一个在 Swing 中初始化 JFrame 上的 JMenuBar 的示例方法。

private JMenuBar initMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");

        exitApp = new JMenuItem("Exit App");
        exitApp.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Timer t = new Timer(2000, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.exit(0);
                    }
                });

                JOptionPane.showMessageDialog(getParent(), "Closing App in 2 Seconds");
                t.start();
            }

        });

        fileMenu.add(exitApp);
        menuBar.add(fileMenu);
        return menuBar;
    }

愿它对你有所帮助。它会创建一个JOptionPane,必须通过单击确定将其关闭,然后JFrame 将在2 秒后关闭。

【讨论】:

    【解决方案2】:

    计时器就是解决方案。 Swing 计时器的任务在事件分派线程中执行。这意味着任务可以安全地操作组件,但也意味着任务应该快速执行。

    您可以通过两种方式使用 Swing 计时器:

    1. To perform a task once, after a delay.
      例如,工具提示管理器使用 Swing 计时器来确定何时显示工具提示以及何时隐藏它。
    2. To perform a task repeatedly.
      例如,您可以执行动画或更新显示目标进度的组件。

    请通过http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html了解更多详情。

    【讨论】:

      【解决方案3】:

      您的代码正在事件调度线程上执行,因此您不能使用 Thread.sleep(),因为这会阻塞 EDT 并阻止它重新绘制 GUI。

      您需要使用单独的线程进行数据库处理。阅读 Concurrency 上的 Swing 教程部分了解更多信息以及使用 SwingWorker 为您管理此线程的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多