gpt4 book ai didi

java - 从 JFrame 中删除图像

转载 作者:行者123 更新时间:2023-12-01 18:18:40 25 4
gpt4 key购买 nike

我一直在尝试让这个程序显示一个图像,然后替换,然后一秒钟后它会删除该图像并在一秒钟后用另一个图像替换它。显示哪个图像由随机 int i 控制。除了图像彼此相邻堆叠且未被删除之外,一切正常。我已经到处寻找,但仍然找不到任何适合我的东西。

public Frame() {
this.setTitle("Fast Number");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();

while(true){
System.out.println("Random Int: "+ i);
if(i == 1){
ImageIcon pic = new ImageIcon("//Users//evan//Documents//workspace//Fast Number//res//ArrowUp.png");
panel1.add(new JLabel(pic));
} else if (i == 2){
ImageIcon pic = new ImageIcon("//Users//evan//Documents//workspace//Fast Number//res//ArrowDown.png");
panel1.add(new JLabel(pic));
}else if (i == 3){
ImageIcon pic = new ImageIcon("//Users//evan//Documents//workspace//Fast Number//res//ArrowLeft.png");
panel1.add(new JLabel(pic));
}else if (i == 4){
ImageIcon pic = new ImageIcon("//Users//evan//Documents//workspace//Fast Number//res//ArrowRight.png");
panel1.add(new JLabel(pic));
}

try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}

this.add(panel1);
this.pack();
this.setSize(256, 276); //Makes it so only one image is visible at once
this.setResizable(false);
this.setLocationRelativeTo(null);
i = RandomRange.Range(1, 4);

this.remove(panel1);
panel1.revalidate();
repaint();
}

最佳答案

取出add(panel1)remove(panel1),并使用setContentPane(panel1)相反。

关于java - 从 JFrame 中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28202763/

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