gpt4 book ai didi

java - 标签位置的更改在线程 sleep 之前不会生效

转载 作者:行者123 更新时间:2023-12-01 12:19:03 27 4
gpt4 key购买 nike

我正在尝试制作一个小程序,它将在屏幕上的随机位置显示一个小方 block 。然后,一旦我单击该方 block ,我希望该方 block 消失,并且在一段随机时间后,我希望另一个方 block 出现在屏幕上的另一个随机位置。

这是我的代码:

package mouse;

import org.apache.commons.io.FileUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
* Created by owner on 07/11/2014.
*/
public class MouseTrainer extends JFrame implements MouseListener {
File record = new File("src\\main\\resources\\mouseRecord.txt");
Random rand = new Random();
JLabel lbl;
int x = 0;
int y = 0;
Thread recorder;


public MouseTrainer() throws IOException {
super("GFXAccelerator");
recorder = new Thread(new MouseRecorder(record));

BufferedImage img = ImageIO.read(new File("src\\main\\resources\\square.png"));
ImageIcon icon = new ImageIcon(img);


this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);

lbl = new JLabel();
lbl.setSize(20, 20);
lbl.setIcon(icon);
lbl.setLocation(0, 0);
contentPane.addMouseListener(this);


contentPane.add(lbl);


this.setContentPane(contentPane);
this.setSize(2560, 1390);

this.setVisible(true);
}

public static void main(String[] args) throws InterruptedException, IOException {
MouseTrainer mr = new MouseTrainer();


}

public void randomPosition() throws IOException {
x = rand.nextInt(2540);
y = rand.nextInt(1350);

FileUtils.writeStringToFile(record, "NEW DESTINATION!\n", true);
lbl.setLocation(x, y);
}


private void hideSquare(){
lbl.setBounds(new Rectangle(new Point(3000, 3000), lbl.getPreferredSize()));
}

@Override
public void mouseClicked(MouseEvent e) {
if((e.getX() > x && e.getX() < x+20) && e.getY()>y && e.getY() <y+20){
System.out.println("hello");
hideSquare();
try {

FileUtils.writeStringToFile(record, "Destination clicked: x=" + x + "; y=" + y + "\n", true);
Thread.sleep(500 +rand.nextInt(5000));
randomPosition();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}

}

}

@Override
public void mousePressed(MouseEvent e) {


}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}


}

我监听鼠标点击,点击鼠标后,我将方 block 的位置更改为屏幕区域,并执行 Thread.sleep() 来等待并绘制下一个方 block 。不幸的是,该位置并未更改为屏幕外位置,并且正方形仍保留在同一位置。

有人可以解释为什么会发生这种情况,以及如何让该方 block 在用户单击它之后和下一个方 block 出现之前消失。

最佳答案

Google Thread.sleep 和 Swing GUI 在此站点上,您将看到您的程序中发生了与所有其他类似程序和类似问题中发生的相同情况。

关于java - 标签位置的更改在线程 sleep 之前不会生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26820077/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com