gpt4 book ai didi

java - 我可以向 JButton 添加动画 gif 文件吗?

转载 作者:行者123 更新时间:2023-11-30 04:06:39 25 4
gpt4 key购买 nike

我正在尝试将 .gif 文件添加到 JButton 中,但似乎不起作用 - gif 文件没有移动,它像普通图像一样显示。

这是我的代码:

+ImageButton 类:

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
* Button which display a png or gif image.
* */

public class ImageButton extends JButton {

private static final long serialVersionUID = 1L;

private ImageIcon defaultIcon;
private ImageIcon hoverIcon;

/**
* Create a normal JButton.
* */

public ImageButton(){
super();
}

/**
* Create a new ImageButton Object.
* @param img1 url of normal image of the button
* @param img2 url hover image of the button
* @param width button width
* @param height button height
* */

public ImageButton(String img1, String img2, int width, int height){
super();
BufferedImage defaultIcon = null;
BufferedImage hoverIcon = null;
try {
defaultIcon = ImageIO.read(getClass().getResource(img1));
hoverIcon = ImageIO.read(getClass().getResource(img2));
} catch (IOException e) {
e.printStackTrace();
}
this.defaultIcon = new ImageIcon(defaultIcon);
this.hoverIcon = new ImageIcon(hoverIcon);
setIcon(this.defaultIcon);
setPreferredSize(new Dimension(width,height));
setBorder(null);
setOpaque(false);
setContentAreaFilled(false);
setBorderPainted(false);
}

/**
* Hover the button.
* */

public void hover(){
setIcon(hoverIcon);
}

/**
* Return the button to normal.
* */

public void down(){
setIcon(defaultIcon);
}
}

+测试类: 导入 java.awt.FlowLayout;

import javax.swing.JFrame;

public class TestGifButton {

public static void main(String[] args) {
ImageButton button = new ImageButton("level3.gif","level3.gif", 150, 150);
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.add(button);
frame.pack();
frame.setVisible(true);
}

}

我使用了循环 .gif 文件。

那么我该如何解决这个问题呢?非常感谢:'(

最佳答案

JButton button = new JButton(new ImageIcon(getClass().getClassLoader().getResource("Images/BBishopB.gif")));

或者完整示例:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.swing.*;

public class ImageSwapOnButton {

public static void main( String[] args ) throws Exception {
URL url = new URL("http://1point1c.org/gif/thum/plnttm.gif");

Image image = Toolkit.getDefaultToolkit().createImage(url);
ImageIcon spinIcon = new ImageIcon(image);
JOptionPane.showMessageDialog(null, new JLabel(spinIcon));

// create a static version of this icon
BufferedImage bi = new BufferedImage(150,150,BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.getGraphics();
g.drawImage(image,0,0,null);
g.dispose();
ImageIcon staticIcon = new ImageIcon(bi);

JButton button = new JButton(staticIcon);
button.setRolloverIcon(spinIcon);
JOptionPane.showMessageDialog(null, button);
}
}

关于java - 我可以向 JButton 添加动画 gif 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20592719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com