有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Gif未显示在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);
  }
}

共 (1) 个答案

  1. # 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);
        }
    }