简单的摆幅延迟 [英] Simple Swing Delay

查看:75
本文介绍了简单的摆幅延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在swing应用程序中,我有一个弹出的jDialog,其中弹出一个jlabel,上面写着"Hang on 5 seconds".

In a swing application, I have a popup jDialog which pops up with a jlabel that says "Hang on 5 seconds."

5秒后,标签应更改为好,现在我完成了."然后将出现一个按钮,允许用户单击继续.

After 5 Seconds, the label should change to "Okay, now I'm done." And a button should appear allowing the user to click continue.

在下面的示例操作(链接到导致弹出窗口的按钮)上,弹出窗口按原样出现,但它是空白的,而不是说挂5秒钟".然后5秒钟后,所有内容都会更新,标签在那里,按钮也在那里.发生什么了?线程在重绘之前休眠吗?

In the example action below (linked to a button which causes the popup), the popup appears as it should but it is blank instead of saying "Hang on 5 seconds." Then after 5 seconds everything updates and the labels are there and the button too. So what's going on? Is the thread sleeping before a repaint or something?

@Action
    public void popUp() {
        popUpDialog.setSize(300,200);
        popUpDialog.setLocationRelativeTo(null);
        popUpDialog.setVisible(true);

        popUpLabel.setVisible(true);
        popUpLabel.setText("Working, hang on a sec....");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

        popUpLabel.setText("Okay Now I'm Done.");
        popUpBut.setVisible(true);
    }

所以我尝试了这一点,目的是使用一个摆动计时器来代替线程睡眠:

So I tried this, in an effort to use a swing timer in place of thread sleeping:

@Action
        public void popUp() {
            popUpDialog.setSize(300,200);
            popUpDialog.setLocationRelativeTo(null);
            popUpDialog.setVisible(true);

            popUpLabel.setVisible(true);
            popUpLabel.setText("Working, hang on a sec....");

            Timer timer = new Timer(speed, this);
            timer.setInitialDelay(pause);
            timer.start(); 

            popUpLabel.setText("Okay Now I'm Done.");
            popUpBut.setVisible(true);
        }

很显然,我需要更多代码来完成计时器,但是马上我得到了一个未找到符号,变量:timer 错误.那是什么意思我做错了吗?

Obviously I'll need more code to finish the timer, but right off the bat I get a symbol not found, variable: timer error. What's that all about? Am I doing it wrong?

我更改了计时器声明并解决了一个问题,但创建了另一个问题.现在,我得到关于 speed 的找不到符号错误.我以前从未使用过摆动计时器,也不知道如何使用它们.关于该主题的Java教程比较复杂,难以理解.你们中的任何人都可以指出一个简单,清晰的计时器示例,以便我可以从中学习并弄清楚我需要做什么吗?

Edit 2: I changed the timer declaration and solved one problem but created another. Now I am getting the symbol not found error in regards to speed. I have never used a swing timer before and don't know how to use them. The java tutorial on the topic is convoluted and difficult to understand. Can any of you point me to a simple, clear example of a timer so I can learn from that and figure out what I need to do?

推荐答案

如果您修改Swing组件状态的代码位于此处的EDT上(应该是),则即使第一个文本的标签也不会重绘如果您确实调用repaint(),则因为所有其他EDT请求都需要在最后一次重画之前排队,然后才能到达该重画,并且此处的代码是这些已排队的EDT事件之一.

If your code modifying your Swing component's state is on the EDT here (it should be), then no repainting of the label with the first text will take place even if you do call repaint(), because all other EDT requests queued before the last repaint need to complete before you reach that repaint, and your code here is one of those queued EDT events.

如果您调用重新绘制,它会将重新绘制添加到队列中,而不会立即重新绘制.您在这里的操作将导致5秒钟的等待,下一次重绘之前的标签仅包含您上次设置的文本(因为在EDT中排队的代码在进入下一步排队之前已完全执行).

If you call repaint, it adds a repaint to the queue, it doesn't repaint right away. Your actions here will result in a 5 second wait, with the label before the next repaint having only the text you last set it to (as code queued on the EDT is executed fully before going to what next is queued).

尝试使用Swing计时器,从Swing计时器触发的事件已经在EDT上执行,因此几乎是您所需要的,这里一个事件是您最初设置文本的时间,另一个是由Swing计时器触发的事件来更改5秒后显示文字.

Try using a Swing Timer, events fired from the Swing Timer are already executing on the EDT, so it's pretty much what you need, one event here where you set the text initially, and another event fired by the Swing Timer to change the text after 5 seconds.

编辑,例如在作者要求的5秒后触发Swing计时器一次的示例:

Edit, example of Swing Timer firing once, after 5 seconds as requested by author:

    // set first jlabel text here

    ActionListener task = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("This is on the EDT after 5 seconds, " +
                "well depending on if I'm used with a Timer, and if " +
                "the right options are set to that Timer");
            // set second jlabel text here
        }
        };
    Timer timer = new Timer(5000 , task);
    timer.setRepeats(false);
    timer.start();

这篇关于简单的摆幅延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆