即使在单独的线程中实现睡眠之后,GUI执行也没有延迟 [英] No delay in GUI execution even after implementing sleep in a separate thread

查看:126
本文介绍了即使在单独的线程中实现睡眠之后,GUI执行也没有延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码来移动特定数量的形状。我已将此代码放在循环中,以便程序将形状移动10次。但是,我不想只显示最后一个输出,而是希望看到形状在移动。所以,我在一个单独的线程中实现了sleep方法。但是,形状仍然快速移动,我只看到最终输出。

I am writing a code to move shapes by a particular amount. I have put this code in a loop so that the program moves the shapes 10 times. But, rather than just showing the last output, I want to see the shapes moving. So, I have implemented the sleep method in a separate thread. But, still the shapes move fast and I am only seeing the final output.

有人知道这里有什么问题,如何解决?

Anybody knows what is the issue here and how can I resolve it?

代码:

public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().equals("START"))
    {
            for(int i=0;i<100;i++)
            {
                pf.particleFilter();
                repaint();  
                sleepFunction();
            }

    }   
}

private void sleepFunction()
{
    System.out.println("In sleep thread");
    Thread mythread = new Thread()
    {
        public void run()
        {
            try
            {
                System.out.println("Going to sleep!");
                Thread.sleep(1000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt(); 
            }   
        }

    };
    mythread.start();
}

public void particleFilter()
    {
            move();     
    }   

    public void move()
    {
        setXPosition_robot(robot_x);
        setYPosition_robot(robot_y);
        setXPosition_particle(particle_x);
        setYPosition_particle(particle_y);
    }


推荐答案

睡眠功能在单独的线程,因此它不会导致事件调度线程(EDT)进入休眠状态,因此EDT会继续执行。

The sleep function executes in a separate thread so it does not cause the Event Dispatch Thread (EDT) to sleep, so the EDT keeps on executing.

这是一件好事,因为你永远不希望EDT睡觉,因为这会阻止GUI重新绘制。

This is a good thing as you never want the EDT to sleep since this will prevent the GUI from repainting itself.

要做动画,你需要使用 Swing Timer 。阅读如何使用Swing Timers 的Swing教程中的部分。欲获得更多信息。

To do animation you need to use a Swing Timer. Read the section from the Swing tutorial on How to Use Swing Timers for more information.

我发现教程中的示例有点复杂,所以这是我能想到的最基本的例子:

I find the example in the tutorial a little complicated so here is the most basic example I can think of:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class TimerTime extends JPanel implements ActionListener
{
    private JLabel timeLabel;

    public TimerTime()
    {
        timeLabel = new JLabel( new Date().toString() );
        add( timeLabel );

        Timer timer = new Timer(1000, this);
        timer.setInitialDelay(1);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        timeLabel.setText( new Date().toString() );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("TimerTime");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TimerTime() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

这篇关于即使在单独的线程中实现睡眠之后,GUI执行也没有延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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