【发布时间】:2015-01-03 18:17:30
【问题描述】:
我试图让一个 gif 出现在我的一个 JFrame 上,并且程序编译但没有显示我希望它显示的 gif。这与我将gif存储在计算机上的位置有关吗?
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);
}
}
【问题讨论】:
-
问题很可能是图像的位置。请进行搜索。每天都有很多关于添加图像的问题,其中许多问题是由文件相对于正在使用的路径的位置引起的。见here
-
另外你应该在添加你的组件之后打包框架
-
应用程序资源在部署时将成为嵌入式资源,因此明智的做法是立即开始访问它们。 embedded-resource 必须通过 URL 而不是文件访问。请参阅info. page for embedded resource 了解如何形成 URL。
标签: java swing gif embedded-resource imageicon