2014-11-07 168 views
1

我試圖讓一個gif出現在我的一個JFrame上,程序編譯但沒有顯示我想要顯示的gif。這是我在電腦上存儲gif的地方嗎?Gif沒有顯示在JFrame

import javax.swing.*; 
import java.awt.*; 
import java.util.*; 


public class iWorkoutScreen 
{ 
    public void iWorkoutScreen() 
    {  
    String calories = "this many"; 

    this.setBackground(Color.WHITE); 
    this.setLayout(new BorderLayout()); 
    this.setPreferredSize(new Dimension(800, 400)); 
    this.pack(); 

    JButton button = new JButton("Press to Start Workout"); 
    this.add(button, BorderLayout.PAGE_START); 

    JLabel timer = new JLabel("this timer will be better"); 
    timer.setPreferredSize(new Dimension(400, 10)); 
    ImageIcon timerIcon = new ImageIcon("7TaK4G8TA.gif"); 
    timer.setIcon(timerIcon); 
    this.add(timer, BorderLayout.CENTER); 

    button = new JButton("Button 3 (LINE_START)"); 
    this.add(button, BorderLayout.LINE_START); 

    button = new JButton("Long-Named Button 4 (PAGE_END)"); 
    this.add(button, BorderLayout.LINE_END); 

    JLabel caloriesBurned = new JLabel("You have burned " + calories + " calories!!"); 
    this.add(caloriesBurned, BorderLayout.PAGE_END); 
    } 
} 
+0

問題很可能是圖像的位置。請做一個搜索。這裏每天都會問到一些關於添加圖像的問題,並且許多問題是由文件相對於正在使用的路徑的位置引起的。請參閱[此處](http://stackoverflow.com/a/9866659/2587435) – 2014-11-07 02:42:11

+0

此外,您應該打包框架_after_添加組件 – 2014-11-07 02:51:53

+0

應用程序資源在部署時將成爲嵌入式資源,所以啓動像現在一樣訪問它們。 [tag:embedded-resource]必須通過URL而不是文件訪問。請參閱[信息。頁面爲嵌入式資源](http://stackoverflow.com/tags/embedded-resource/info)如何形成的URL。 – 2014-11-07 04:11:51

回答

2

以下MCVE的作品。它不僅將該方法更改爲構造函數,還糾正了其他問題。它熱鏈接到圖像,以便它適用於任何人。

import java.awt.*; 
import java.net.MalformedURLException; 
import java.net.URL; 
import javax.swing.*; 

public class iWorkoutScreen extends JFrame { 

    public iWorkoutScreen() throws MalformedURLException { 
     String calories = "this many"; 

     this.setBackground(Color.WHITE); 
     this.setLayout(new BorderLayout()); 

     JButton button = new JButton("Press to Start Workout"); 
     this.add(button, BorderLayout.PAGE_START); 

     JLabel timer = new JLabel("this timer will be better"); 
     ImageIcon timerIcon = new ImageIcon(
       new URL("http://i.imgur.com/T8x0I29.png")); 
     timer.setIcon(timerIcon); 
     this.add(timer, BorderLayout.CENTER); 

     button = new JButton("Button 3 (LINE_START)"); 
     this.add(button, BorderLayout.LINE_START); 

     button = new JButton("Long-Named Button 4 (PAGE_END)"); 
     this.add(button, BorderLayout.LINE_END); 

     JLabel caloriesBurned = new JLabel(
       "You have burned " + calories + " calories!!"); 
     this.add(caloriesBurned, BorderLayout.PAGE_END); 
     this.pack(); 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       try { 
        JFrame f = new iWorkoutScreen(); 
        f.setLocationByPlatform(true); 
        f.setVisible(true); 
       } catch (MalformedURLException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
}