java - Gif 未显示在 JFrame 中

标签 java swing gif embedded-resource imageicon

我试图让一个 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);
  }
}

最佳答案

以下 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);
    }
}

关于java - Gif 未显示在 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26793067/

相关文章:

java - 如何为 Netbeans 中运行的 TestNG 测试用例生成 html 报告?

java - 在 apache karaf 中安装时,apache camel 在 bundle 中 Unresolved 约束

java - 聊天程序使用TCP : Only first message reaches other end

Java 关于初始化 swing 组件的困惑

ffmpeg GIF 到 WebM 解码问题

java - 从字节数组写入 GIF(在 Android 中)

CSS优化

java - Google App Engine Flex 在运行 Play Framework 应用程序时出现 No X11 DISPLAY variable was set 错误;我在哪里设置这个变量?

java - Excel 导出中的乱码

java - Jasper 报告图像路径