Java 睡眠不做该做的事

Java 睡眠不做该做的事,java,applet,thread-sleep,jradiobutton,Java,Applet,Thread Sleep,Jradiobutton,所以,这个程序是为学校设计的,我遇到了一些问题,基本前提是用户必须选择形状、大小和颜色,然后绘制形状或动画,使其在屏幕上移动 我的程序在没有thread.sleep的情况下运行良好(它会立即在屏幕上移动)。当我添加thread.sleep时,它不工作,等待了10秒钟,然后立即移动到末尾 在进行测试之后,它似乎与它所处的for循环有一定的相关性,因为当for循环为1000时,它等待10秒,然后立即移动1000个空间,但当for循环为100时,只需1秒即可立即移动100个空间 我的理解是,它应该在再

所以,这个程序是为学校设计的,我遇到了一些问题,基本前提是用户必须选择形状、大小和颜色,然后绘制形状或动画,使其在屏幕上移动

我的程序在没有thread.sleep的情况下运行良好(它会立即在屏幕上移动)。当我添加thread.sleep时,它不工作,等待了10秒钟,然后立即移动到末尾

在进行测试之后,它似乎与它所处的for循环有一定的相关性,因为当for循环为1000时,它等待10秒,然后立即移动1000个空间,但当for循环为100时,只需1秒即可立即移动100个空间

我的理解是,它应该在再次进入for循环之前等待10毫秒

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class AppletAssignemntIsFucked extends Applet
    implements ActionListener
{
int colourChoice;
int shapeChoice;
int location;
int x, y;
TextField height = new TextField ("00");
TextField width = new TextField ("00");
boolean animating = false;

//declare the two types of checkboxes
CheckboxGroup shape, colour;
//declare all the shape checkboxes
Checkbox buttonSquare, buttonCircle, buttonRectangle, buttonOval;
//declare all the colour checkboxes
Checkbox buttonRed, buttonBlue, buttonGreen, buttonYellow;

Button buttonDraw, buttonAnimate, buttonReset;

public void init ()
{
    setBackground (Color.lightGray);

    shape = new CheckboxGroup ();
    colour = new CheckboxGroup ();

    buttonCircle = new Checkbox ("Circle", shape, true);
    add (buttonCircle);
    buttonSquare = new Checkbox ("Square", shape, false);
    add (buttonSquare);
    buttonRectangle = new Checkbox ("Rectangle", shape, false);
    add (buttonRectangle);
    buttonOval = new Checkbox ("Oval", shape, false);
    add (buttonOval);

    buttonRed = new Checkbox ("Red", colour, true);
    add (buttonRed);
    buttonBlue = new Checkbox ("Blue", colour, false);
    add (buttonBlue);
    buttonGreen = new Checkbox ("Green", colour, false);
    add (buttonGreen);
    buttonYellow = new Checkbox ("Yellow", colour, false);
    add (buttonYellow);

    buttonDraw = new Button ("draw");
    buttonDraw.addActionListener (this);
    add (buttonDraw);
    buttonAnimate = new Button ("animate");
    buttonAnimate.addActionListener (this);
    add (buttonAnimate);
    buttonReset = new Button ("reset");
    buttonReset.addActionListener (this);
    add (buttonReset);

    add (height);
    add (width);
}


public void paint (Graphics g)
{
    switch (colourChoice)
    {
        case 1:
            g.setColor (Color.red);
            break;
        case 2:
            g.setColor (Color.green);
            break;
        case 3:
            g.setColor (Color.yellow);
            break;
        case 4:
            g.setColor (Color.blue);
            break;
    }
    switch (shapeChoice)
    {
        case 1:
            g.fillOval (location, 80, x, x);
            break;
        case 2:
            g.fillRect (location, 80, x, x);
            break;
        case 3:
            g.fillRect (location, 80, x, y);
            break;
        case 4:
            g.fillOval (location, 80, x, y);
            break;
    }

}


public void actionPerformed (ActionEvent evt)
{

    y = Integer.parseInt (height.getText ());
    x = Integer.parseInt (width.getText ());
    //set the colour
    if (evt.getSource () == buttonAnimate)
    {
        for (int i = 0 ; i < 1000 ; i++)
        {
            try
            {
                Thread.sleep (10);         
            }
            catch (InterruptedException ex)
            {
                Thread.currentThread ().interrupt ();
            }
            location += 1;
            repaint ();
        }
    }
    if (evt.getSource () == buttonReset)
    {
        location = 10;
        repaint ();
    }
    if (evt.getSource () == buttonDraw)
    {
        if (buttonRed.getState () == true)
        {
            colourChoice = 1;

        }
        else if (buttonGreen.getState () == true)
        {
            colourChoice = 2;

        }
        else if (buttonYellow.getState () == true)
        {
            colourChoice = 3;

        }
        else if (buttonBlue.getState () == true)
        {
            colourChoice = 4;
        }
        //set the shape
        if (buttonCircle.getState () == true)
        {
            shapeChoice = 1;
        }
        else if (buttonSquare.getState () == true)
        {
            shapeChoice = 2;
        }
        else if (buttonRectangle.getState () == true)
        {
            shapeChoice = 3;
        }
        else if (buttonOval.getState () == true)
        {
            shapeChoice = 4;
        }

        repaint ();
    }


}
import java.awt.*;
导入java.awt.event.*;
导入java.applet.*;
公共类AppleTassignmentsApplet
实现ActionListener
{
色彩选择;
内形冰;
int定位;
int x,y;
文本字段高度=新文本字段(“00”);
文本字段宽度=新文本字段(“00”);
布尔动画=假;
//声明这两种类型的复选框
复选框组形状、颜色;
//声明所有形状复选框
复选框按钮方形、按钮圆形、按钮矩形、按钮圆形;
//申报所有颜色复选框
复选框buttonRed、buttonBlue、buttonGreen、buttonYellow;
Button buttonDraw、buttonAnimate、buttonReset;
公共void init()
{
挫折背景(颜色:浅灰色);
shape=新的CheckboxGroup();
颜色=新复选框组();
按钮圆形=新复选框(“圆形”,形状,真);
添加(按钮圆圈);
buttonSquare=新复选框(“方形”,形状,假);
添加(按钮方形);
buttonRectangle=新复选框(“矩形”,形状,假);
添加(按钮矩形);
ButtonVal=新复选框(“椭圆形”,形状,假);
添加(新按钮);
buttonRed=新复选框(“红色”,颜色,真);
添加(按钮red);
按钮蓝色=新复选框(“蓝色”,颜色,假);
添加(按钮蓝色);
按钮绿色=新复选框(“绿色”,颜色,假);
添加(按钮绿色);
buttonYellow=新复选框(“黄色”,颜色,假);
添加(buttonYellow);
buttonDraw=新按钮(“绘制”);
buttonDraw.addActionListener(此);
添加(按钮拖动);
buttonAnimate=新按钮(“动画”);
buttonAnimate.addActionListener(此);
添加(buttonAnimate);
按钮重置=新按钮(“重置”);
buttonReset.addActionListener(此);
添加(按钮设置);
增加(高度);
增加(宽度);
}
公共空间涂料(图g)
{
开关(颜色选择)
{
案例1:
g、 setColor(Color.red);
打破
案例2:
g、 setColor(Color.green);
打破
案例3:
g、 setColor(颜色为黄色);
打破
案例4:
g、 setColor(Color.blue);
打破
}
开关(shapeChoice)
{
案例1:
g、 圆形(位置,80,x,x);
打破
案例2:
g、 fillRect(位置,80,x,x);
打破
案例3:
g、 fillRect(位置,80,x,y);
打破
案例4:
g、 椭圆形(位置,80,x,y);
打破
}
}
已执行的公共无效操作(操作事件evt)
{
y=Integer.parseInt(height.getText());
x=Integer.parseInt(width.getText());
//定色
if(evt.getSource()==buttonAnimate)
{
对于(int i=0;i<1000;i++)
{
尝试
{
睡眠(10);
}
捕获(中断异常例外)
{
Thread.currentThread().interrupt();
}
位置+=1;
重新油漆();
}
}
if(evt.getSource()==buttonReset)
{
位置=10;
重新油漆();
}
if(evt.getSource()==buttonDraw)
{
if(buttonRed.getState()==true)
{
颜色选择=1;
}
else if(buttonGreen.getState()==true)
{
颜色选择=2;
}
else if(buttonYellow.getState()==true)
{
颜色选择=3;
}
else if(buttonBlue.getState()==true)
{
颜色选择=4;
}
//定形
if(buttonCircle.getState()==true)
{
shapeChoice=1;
}
else if(buttonSquare.getState()==true)
{
shapeChoice=2;
}
else if(buttonRectangle.getState()==true)
{
shapeChoice=3;
}
else if(buttonOval.getState()==true)
{
shapeChoice=4;
}
重新油漆();
}
}
}您正在阻止,因此无法更新UI。
例如,见


请改用。

您正在阻止,因此无法更新UI。看,我真的不明白那条线索里发生了什么。我读过它,我理解它为什么现在不起作用,但我不知道如何实际修复它。你能给我一个如何实现它的例子吗?我读了这个页面并上网查看了一下,但我仍然不太明白它是如何工作的以及如何使用它;对于(inti=0;i<1000;i++){timer=new timer(10,this);location+=1;repaint();timer.restart();}timer.stop();'但是当我尝试运行它时,我得到了空指针异常。你完全没有抓住要点。创建计时器时,指定回调方法,计时器将调用它。关键是你不做循环,计时器会一直呼叫你,直到你停止。