gpt4 book ai didi

java - 如何使用 java 中的线程实例化多个带有动画的 JFrame?

转载 作者:行者123 更新时间:2023-11-30 09:31:25 24 4
gpt4 key购买 nike

我这里有这段代码,它实例化了一个带有简单动画的 JFrame,您可以在其中单击按钮并使用线程保持运行。我用一个实例化了 JFrame 的按钮制作了一个 UI,问题是,当我再次单击该按钮并且它实例化了另一个 JFrame 时,第二个的动画无法正常工作。即使我实例化了 5 个 JFrame,启动动画的所有按钮也只适用于第一个 JFrame。我想做的是尽可能多的实例,它们都是分开工作的。这是带有动画的类的代码:

public class duduleo extends JFrame implements ActionListener, Runnable{
JButton imagens;
int x=1;
static ImageIcon icon[] = new ImageIcon[11];
static JLabel l;
boolean anima=false;


public static void main(String args[]){
JFrame janela = new duduleo();
janela.show();
}

duduleo(){
icon[0]= new ImageIcon("duken1.jpg");
icon[1]= new ImageIcon("duken2.jpg");
icon[2]= new ImageIcon("duken3.jpg");
icon[3]= new ImageIcon("duken4.jpg");
icon[4]= new ImageIcon("duken5.jpg");
icon[5]= new ImageIcon("duken6.jpg");
icon[6]= new ImageIcon("duken7.jpg");
icon[7]= new ImageIcon("duken8.jpg");
icon[8]= new ImageIcon("duken9.jpg");
icon[9]= new ImageIcon("duken10.jpg");
icon[10]= new ImageIcon("duken11.jpg");
setSize(100,200);
setLocation(200,150);
getContentPane().setLayout(new GridLayout(2,1));
imagens = new JButton(icon[0]);
l= new JLabel(icon[0]);
imagens.addActionListener(this);
getContentPane().add(l);
getContentPane().add(imagens);

}

public void run(){

while(anima){
for(int i=0;i<icon.length;i++){
l.setIcon(icon[i]);
try{
Thread.sleep(100);
}catch(Exception e){}

}}}

public void actionPerformed(ActionEvent e){
anima=!anima;
Thread t;
t = new Thread(this);
t.start();
}}

感谢您的帮助。

最佳答案

关于java - 如何使用 java 中的线程实例化多个带有动画的 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897210/

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