JLabel不动

时间:2017-05-26 17:25:00

标签: java swing animation jlabel

我尝试制作一个程序,将标签向下移动到框架的右侧,但没有任何工作,正在触发while循环,因此代码正在运行,但标签不会移动。对不起,我是一名初学者,并且不太了解发生了什么。

该代码还使用Stopwatch代码下方显示的animation类。

animation

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class animation {

private JFrame frame;

boolean isMoving = false;
int labelX = 0;
int labelY = 0;
int x, y;
private final JButton button = new JButton("Start");
JLabel lblO = new JLabel("O");

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                animation window = new animation();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public animation() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
     x = frame.getWidth();
     y = frame.getHeight();

    lblO.setBounds(0, 0, 15, 15);
    frame.getContentPane().add(lblO);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            button.setText("End");
            if(isMoving){

                movetheball();
            }
            else{
                isMoving = true;
            }
            }
    });
    button.setBounds(168, 219, 84, 31);
    frame.getContentPane().add(button);
    }

public void movetheball(){
    StopWatch tim = new StopWatch();
    tim.start();
    while(isMoving){
        long time = tim.getElapsedtime();
        if(time>1){

            System.out.println("The Timer is Working");
            labelX+= 150;
            labelY += 150;
            lblO.setBounds(labelX,labelY,15,15);
            }
    }
}
}

Stopwatch

    public class StopWatch {
private long elapsedTime;
private long startTime;
private boolean isRunning;

public StopWatch(){

}

public void start(){
    isRunning = true;
    startTime = System.currentTimeMillis();
    }

public void stop(){
    isRunning = false;

}

public long getElapsedtime(){
    if(isRunning){
        long  endTime =System.currentTimeMillis();
        return elapsedTime + endTime - startTime;

    }
    else{
        return elapsedTime;
    }
}
public void reset(){
    elapsedTime = 0;
    isRunning = false;
}
}

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些问题:

1)您需要单击两次按钮才能启动动画。在actionPerformed代码中修改你的逻辑。

2)你在第一步时移动你的标签太远了。使用1的增量而不是150。

3)您的动画在事件派发线程中并冻结GUI。将其移动到单独的线程中并在事件调度线程中更新JLabel。

这是工作代码:

    public static class animation {

private JFrame frame;

boolean isMoving = false;
int labelX = 0;
int labelY = 0;
int x, y;
private final JButton button = new JButton("Start");
JLabel lblO = new JLabel("O");
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                animation window = new animation();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public animation() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
     x = frame.getWidth();
     y = frame.getHeight();

    lblO.setBounds(0, 0, 15, 15);
    frame.getContentPane().add(lblO);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            button.setText("End");
            if(isMoving){

                new Thread( () -> movetheball() ).start();
            }
            else{
                isMoving = true;
            }
            }

    });
    button.setBounds(168, 219, 84, 31);
    frame.getContentPane().add(button);



    }

public void movetheball(){
    StopWatch tim = new StopWatch();
    tim.start();
    while(isMoving){

        long time = tim.getElapsedtime();
        if(time>1){

            System.out.println("The Timer is Working");
            labelX+= 1;
            labelY += 1;

            EventQueue.invokeLater( () -> lblO.setBounds(labelX,labelY,15,15) );

            try {
                Thread.sleep( 100 );
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
    }


}