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

标签 java multithreading netbeans

我这里有一段代码,它实例化了一个带有简单动画的 JFrame,您可以在其中单击按钮并使用线程继续运行。我制作了一个带有实例 JFrame 的按钮的 UI,问题是,当我再次单击该按钮并且它实例化另一个 JFrame 时,第二个 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/

相关文章:

java - 通过properties在xml配置中配置spring bean抽象与否

java - 使用 Java 扫描器进行密码屏蔽

java - SharedPreferences 读取相同的值

Java System.out.println() 抛出错误

java - 如何将 Java Servlet 转换为 WAR 文件以部署到 Amazon EC?

java - 是否可以使用 JFileChooser 限制特定目录可用的文件?

java - Nashorn CommandListener$$NashornJavaAdapter 无法转换为 CommandListener

c# - RabbitMQ 只处理 50 条消息然后阻塞

c++ - 可以(由编译器)使用多少线程来初始化全局对象(在函数 main 之前)

快速生产者的 Java "Tiered Queue"实现,慢速消费者