1

I'm trying to get a gif to appear on one of my JFrame and the program compiles but doesn't show the gif I want it to show. Is it a matter of where I have the gif stored on my computer?

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);
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alex
  • 11
  • 1
  • Problem is most likely location of the image. Please do a search. There are a number question asked here everyday regarding adding images, and many of the problems are caused by location of the file relative to the path being used. See [here](http://stackoverflow.com/a/9866659/2587435) – Paul Samsotha Nov 07 '14 at 02:42
  • Also you should be packing the frame _after_ adding your components – Paul Samsotha Nov 07 '14 at 02:51
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Nov 07 '14 at 04:11

1 Answers1

2

The following MCVE works. Not only does it change the method into a constructor, but also corrects other problems. It hot-links to an image so that it should work for anyone.

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433