本文介绍了在ActionListener中使用Thread.sleep()的简单动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用此代码创建轮盘赌轮时遇到问题。当我点击SPIN!时,目标是旋转车轮按钮。我通过创建一个for循环来实现这一点,该循环应该将轮子的状态从true更改为false,这会更改方向。如果速度足够快,应该会造成运动幻觉。



我有这个问题:是我的车轮只是在重新磨合之后尽管我放置了repaint(),整个for循环都完成了。所以,它只能打一个勾号。



以下是我的ActionListener的示例代码:

  public class spinListener实现ActionListener 
{
RouletteWheel wheel;
int countEnd =(int)(Math.random()+ 25 * 2);
public spinListener(RouletteWheel w)
{
wheel = w;

public void actionPerformed(ActionEvent e)
{
for(int i = 0; i< countEnd; i ++)
{
try
{
Thread.sleep(100);
if(wheel.getStatus()== true)
{
wheel.setStatus(false);
repaint();

if(wheel.getStatus()== false)
{
wheel.setStatus(true);
repaint();
}
}
catch(InterruptedException ex)
{
Logger.getLogger(WheelBuilder.class.getName()).log(Level.SEVERE,null,ex );
}
}
}
}

更新:我找出了问题所在。以下是我为任何有类似问题的人所做的更改。

  public class spinListener implements ActionListener 
{
定时器tm =新定时器(100,this);
int count = 0;

public void actionPerformed(ActionEvent e)
{
tm.start();
changeWheel();

public void changeWheel()
{
int countEnd =(int)(Math.random()+ 20 * 2);
if(count< countEnd)
{
wheel.setStatus(!wheel.getStatus());
repaint();
count ++;




解决方案


actionPerformed 方法中使用 Thread.sleep 会阻止EDT,从而阻止它处理新事件,包括绘制事件,直到退出 actionPerformed 方法。



您应该使用 javax.swing .Timer 改为。



查看和以获取更多详情


I'm having trouble with this code I am using to create a roulette wheel. The goal is to spin the wheel when I click the "SPIN!" button. I have done this by creating a for loop that should change the status of the wheel from true to false, which changes the orientation. This, when done fast enough, should create the illusion of movement.

THE PROBLEM I AM HAVING: is that my wheel is only repainting after the whole for loop is done, despite my placement of the repaint(). So, it only spins one tick.

Here is some sample code of my ActionListener:

public class spinListener implements ActionListener
{
    RouletteWheel wheel;
    int countEnd = (int)(Math.random()+25*2);
    public spinListener(RouletteWheel w)
    {
        wheel = w;
    }
    public void actionPerformed(ActionEvent e)
    {
        for (int i = 0; i <countEnd; i++)
        {
            try
            {
                Thread.sleep(100);
                if (wheel.getStatus() == true)
                {
                    wheel.setStatus(false);
                    repaint();
                }
                if (wheel.getStatus() == false)
                {
                    wheel.setStatus(true);
                    repaint();
                }
            }
            catch (InterruptedException ex)
            {
                Logger.getLogger(WheelBuilder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

UPDATE: I figured out the problem. Here are the changes I made for anyone having a similar problem.

public class spinListener implements ActionListener
{
    Timer tm = new Timer(100, this);
    int count = 0;

    public void actionPerformed(ActionEvent e)
    {
        tm.start();
        changeWheel();
    }
    public void changeWheel()
    {
        int countEnd = (int)(Math.random()+20*2);
        if (count < countEnd)
        {
            wheel.setStatus(!wheel.getStatus());
            repaint();
            count++;
        }
    }
}
解决方案

Swing is a single threaded environment, anything that blocks the Event Dispatching Thread, will prevent it from been able to process new events, including, paint events.

The use of Thread.sleep within the actionPerformed method is blocking the EDT, preventing it from processing new events, including paint events, until the actionPerformed method is exited.

You should use a javax.swing.Timer instead.

Take a look at Concurrency in Swing and How to Use Swing Timers for more details

这篇关于在ActionListener中使用Thread.sleep()的简单动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:18