我正在尝试编写一个程序,将标签向下移动到框架的右侧,但没有任何效果,while 循环被触发,因此代码正在运行,但标签不会移动。抱歉,我是初学者,不太明白发生了什么。
该代码还使用动画
代码下方显示的Stopwatch
类。
动画
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);
}
}
}
}
秒表
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) 您需要单击按钮两次才能启动动画。重新设计 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();
}
}
}
}
关于java - JLabel 没有移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207101/