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);
}
}
問題很可能是圖像的位置。請做一個搜索。這裏每天都會問到一些關於添加圖像的問題,並且許多問題是由文件相對於正在使用的路徑的位置引起的。請參閱[此處](http://stackoverflow.com/a/9866659/2587435) – 2014-11-07 02:42:11
此外,您應該打包框架_after_添加組件 – 2014-11-07 02:51:53
應用程序資源在部署時將成爲嵌入式資源,所以啓動像現在一樣訪問它們。 [tag:embedded-resource]必須通過URL而不是文件訪問。請參閱[信息。頁面爲嵌入式資源](http://stackoverflow.com/tags/embedded-resource/info)如何形成的URL。 – 2014-11-07 04:11:51