簡體   English   中英

Gif沒有出現在JFrame中

[英]Gif not showing up in JFrame

我試圖讓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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM