2014-02-21 37 views
1

我不得不在netbeans gui中制作一种动画。所以我正在研究关于互联网上的摇摆计时器,以及我发现的一种方法,它会在一段时间后改变jLabel中的图像。在一段时间后在jLabel中更改图像

public void animation() throws InterruptedException { 
    ActionListener taskPerformer = new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent evt) { 
      //...Perform a task... 
      t++; 
      System.out.printf("Reading SMTP Info. %d\n",t); 
      if(t%2==1){ 
       jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/oncsreen_keypad/a.jpg"))); 
      } 
      else{ 
       jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/oncsreen_keypad/b.jpg"))); 
      } 
     } 
    }; 
    Timer timer = new Timer(1000 , taskPerformer); 
    //timer.setRepeats(false); 
    timer.start(); 

    Thread.sleep(5000); 
} 

这种方法被称为无处。但是,如果System.out.printf工作,那么在jLabel中更改图像也应该可以工作。但实际上,这些线对jLabel没有任何影响。

那么应该采取什么样的正确方法。

+0

哪种方法是行不通叫什么名字? – vanza

+1

请编辑您的问题以包含展示您描述的问题的[*最小,完整,测试和可读示例*](http://stackoverflow.com/help/mcve);一些例子被引用[这里](http://stackoverflow.com/a/14432646/230513)。 – trashgod

+0

请看看这个[示例](http://stackoverflow.com/a/10837751/1057230)。一些额外的[链接](http://stackoverflow.com/a/9866659/1057230)可能可以帮助:-) –

回答

2

不要使用Thread.sleep停止主线程,使用Swing Timer并给它延迟,当图像发生变化时。

我为你做了一个小例子。

这是使用包含JLabel的JPanel生成JFrame的类。

package timerdemo; 

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

/** 
* 
* @author ottp 
* @version 1.0 
*/ 
public class Gui extends JFrame { 

    private JLabel jLabel; 
    private Timer timer; 
    private boolean chromeShown; 

    public Gui() { 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setSize(800, 600); 
     JPanel panel = new JPanel(new BorderLayout()); 
     jLabel = new JLabel(new ImageIcon("/home/ottp/Downloads/chrome.png")); 
     chromeShown = true; 

     panel.add(jLabel); 
     timer = new Timer(5000, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if(chromeShown) { 
        jLabel.setIcon(new ImageIcon("/home/ottp/Downloads/ok.png")); 
        chromeShown = false; 
       } else { 
        jLabel.setIcon(new ImageIcon("/home/ottp/Downloads/chrome.png")); 
        chromeShown = true; 
       } 
      } 
     }); 
     timer.start(); 

     this.getContentPane().add(panel); 
     this.setVisible(true); 
    } 
} 

并启动它...

package timerdemo; 

import javax.swing.SwingUtilities; 

/** 
* 
* @author ottp 
*/ 
public class TimerDemo { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Gui(); 
      } 
     }); 

    } 
} 

开始在你的GUI类定时器后,在JLabel中的图像将每5秒钟改变,条件是这样的布尔标志。你也可以使用你的if ... else构造。

希望这有助于

帕特里克

+1

作为一个Swing开发者,我们不关心的主线程,我们关心的是什么事件调度线程。你应该看看[初始线程](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) – MadProgrammer

+1

一个方法来获取图像(S)的例子(这适用于每个人)是热链接到[此答案](http://stackoverflow.com/a/19209651/418556)中看到的图像。 –

相关问题