2014-11-07 183 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); 
    } 
}