Java 标签中的 gif 问题

Java problems with gif in label

我尝试放入 JPanel 的 gif 在单击触发它的按钮后没有显示,直到我调整 window 的大小。当它出现时,它不适合 JPanel 并且没有动画。我看了几个处理这个问题的帖子,但我不明白如何在我的案例中使用它们。

/*
 * Author: Raymo111
 * Date: 13/04/2018
 * Description: Wishes happy birthday to a special someone
 */

//Imports java GUI classes
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

// Main class with JFrame and ActionListener enabled
public class Happy_Birthday_GUI extends JFrame implements ActionListener {

    // Class variables
    private static JButton startButton = new JButton("CLICK TO START");
    private static JPanel startPanel = new JPanel(), gifPanel = new JPanel();
    private static Color blue = new Color(126, 192, 238), pink = new Color(255, 192, 203);
    private static GridLayout grid1 = new GridLayout(1, 1);

    // Constructor
    public Happy_Birthday_GUI() {

        // Initial screen
        startButton.addActionListener(this);
        startButton.setFont(new Font("Comic Sans MS", Font.PLAIN, 50));
        startPanel.setLayout(grid1);
        startPanel.add(startButton);
        startPanel.setBorder(BorderFactory.createLineBorder(blue, 100));
        startButton.setBackground(pink);
        getContentPane().add(startPanel);

        // Sets title, size, layout (grid 1x1), and location of GUI window (center)
        setTitle("Happy Birthday from Dolphin");
        setSize(840, 840);
        setLayout(grid1);
        setLocationRelativeTo(null);
        setVisible(true);

    }

    // Main method
    public static void main(String[] args) {
        new Happy_Birthday_GUI();
    }

    // Action Performed method
    public void actionPerformed(ActionEvent event) {

        // Proceed to gif and song
        if (startButton == event.getSource()) {
            getContentPane().removeAll();
            BufferedImage dolphin;
            gifPanel.setLayout(grid1);
            gifPanel.setBorder(BorderFactory.createLineBorder(pink, 100));
            try {
                dolphin = ImageIO.read(new File("C:\Users\raymo\Pictures\dolphin.gif"));
                JLabel gifLabel = new JLabel(new ImageIcon(dolphin));
                gifPanel.add(gifLabel);
            } catch (IOException e) {
                e.printStackTrace();
            }
            getContentPane().add(gifPanel);
        }
    }

}

这里是dolphin.gif。很可爱。


如何让它在单击开始按钮后立即显示为适合 JPanel 的动画 gif?提前致谢。

I tried to put into a JPanel isn't showing up after clicking the button

当您从可见的 GUI 添加(或删除)组件时,基本代码是:

panel.add(...);
panel.revalidate();
panel.repaint();

revalidate() 需要调用布局管理器,以便为组件指定大小。

is not animated.

使用带有 ImageIcon 的 JLabel 来显示图像。 JLabel 将为 gif 动画。

When it does show up, it does not fit the JPanel and

你可以试试Stretch Icon,它是专门用来填充标签可用的space。

我最后做了:

gifPanel.add(new TestPane());
getContentPane().add(gifPanel);
revalidate();
repaint();

使用 camickr 的 revalidate 和 repaint,以及 MadProgrammer 的 TestPane class, 这非常适合让 gif 动画、正确调整大小并立即显示。

BufferedImage 不支持绘制动画 Gif,相反,您需要使用 Image(或者最好是 ImageIcon)。

然后可以将其直接应用于 JLabel,后者将自行执行动画操作。

animated gif that fits he JPanel?

好的,这是一个复杂得多的问题。一种方法是将 Gif 转换为所需的大小,但不用说,这非常非常复杂。

一个更简单的解决方案可能是使用 AffineTransform 并缩放图像以满足组件本身的要求。这将需要一个自定义组件,能够计算比例并绘制图像的每一帧。

幸运的是,JPanel 是一个 ImageObserver,这意味着它能够绘制 gif 动画

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private ImageIcon image;

        public TestPane() {
            image = new ImageIcon("/Users/swhitehead/Downloads/NbENe.gif");
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            int imageWidth = image.getIconWidth();
            int imageHeight = image.getIconHeight();

            if (imageWidth == 0 || imageHeight == 0) {
                return;
            }
            double widthScale = (double)getWidth() / (double)imageWidth;
            double heightScale = (double)getHeight() / (double)imageHeight;
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(image.getImage(), AffineTransform.getScaleInstance(widthScale, heightScale), this);
            g2d.dispose();
        }

    }

}