我看不到圆圈在移动 [英] i can't see circle moving

查看:63
本文介绍了我看不到圆圈在移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中使用Swing时,我试图在单击按钮时将圆从起始位置缓慢移动到结束位置.但是,我看不到圆圈在运动.它只是在瞬间从头到尾移动.

While using Swing in java, I am trying to move a circle slowly from a starting position to an end position when clicking a button. However, I can't see the circle moving. It just moves from start to end in an instant.

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

public class MyApp {

    private int x = 10;
    private int y = 10;
    private JFrame f;
    private MyDraw m;
    private JButton b;

    public void go() {
        f = new JFrame("Moving circle");
        b = new JButton("click me to move circle");
        m = new MyDraw();
        f.add(BorderLayout.SOUTH, b);

        f.add(BorderLayout.CENTER, m);
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        b.addActionListener(new Bute());
    }

    public static void main(String[] args) {
        MyApp m = new MyApp();
        m.go();
    }

    private class Bute implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < 150; i++) {
                ++x;
                ++y;
                m.repaint();
                Thread.sleep(50);
            }
        }
    }

    private class MyDraw extends JPanel {
        @Override
        public void paintComponent(Graphics g) {
            g.setColor(Color.white);
            g.fillRect(0, 0, 500, 500);
            g.setColor(Color.red);
            g.fillOval(x, y, 40, 40);
        }
    }
}

我认为问题在于动作监听器,因为当我在不使用按钮的情况下进行操作时,它就可以正常工作了.有什么建议吗?

I think the problem is with the action listener because when I'm doing it without using button it is working. Any suggestions?

推荐答案

正如安德鲁·汤普森(Andrew Thompson)所说,在不定义第二个线程的情况下调用Thread.sleep()将冻结所有内容,因此解决方案是像这样定义并运行另一个线程:

As Andrew Thompson said, calling Thread.sleep() without defining a second thread freezes everything, so the solution is to define and run another thread like so:

class Bute implements ActionListener, Runnable {
    //let class implement Runnable interface
    Thread t;   // define 2nd thread

    public void actionPerformed(ActionEvent e) {

        t = new Thread(this);   //start a new thread
        t.start();
    }

    @Override               //override our thread's run() method to do what we want 
    public void run() {     //this is after some java-internal init stuff called by start()
        //b.setEnabled(false);
        for (int i = 0; i < 150; i++) {
            x++;
            y++;
            m.repaint();
            try {
                Thread.sleep(50);   //let the 2nd thread sleep
            } catch (InterruptedException iEx) {
                iEx.printStackTrace();  
            }
        }
        //b.setEnabled(true);
    }

}

此解决方案的唯一问题是多次按下按钮将加快圆的速度,但是可以通过在动画过程中通过b.setEnabled(true/false)使按钮不可单击来解决此问题.不是最好的解决方案,但它可以工作.

The only problem with this solution is that pressing the button multiple times will speed up the circle, but this can be fixed by making the button unclickable during the animation via b.setEnabled(true/false). Not the best solution but it works.

这篇关于我看不到圆圈在移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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