2014-02-21 35 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)中看到的圖像。 –

相關問題