【问题标题】:Gif not showing up in JFrameGif 没有出现在 JFrame 中
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-12-15
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多