标签中的gif的Java问题 [英] Java problems with gif in label

查看:141
本文介绍了标签中的gif的Java问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图放入JPanel的gif在点击触发它的按钮之后没有显示,直到我调整窗口大小。当它出现时,它不适合JPanel并且不是动画。我查看了几篇涉及此问题的帖子,但我不明白如何在我的案例中使用它们。

A gif that I tried to put into a JPanel isn't showing up after clicking the button that triggers it until I resize the window. When it does show up, it does not fit the JPanel and is not animated. I looked at several posts that dealt with this but I don't understand how to use them in my case.

/*
 * 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。这很可爱。


Here is dolphin.gif. It's cute.

如何在单击开始按钮作为适合JPanel的动画gif后立即显示它?在此先感谢。

How do I get it to show up immediately after clicking the start button as an animated gif that fits the JPanel? Thanks in advance.

推荐答案


我试图放入JPanel后点击后没有显示按钮

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

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

When you add (or remove) components from a visible GUI the basic code is:

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

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

The revalidate() is need to invoke the layout manager so the component is given a size.


不是动画。

is not animated.

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

Use a JLabel with an ImageIcon to display images. A JLabel will animated the gif.


当它出现时,它不适合JPanel和

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

您可以尝试拉伸图标,用于填充标签可用的空间。

You can try the Stretch Icon which is designed to fill the space available to the label.

这篇关于标签中的gif的Java问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆