[英]Can I add an animated gif file to JButton?
I'm trying to add a .gif file into a JButton, but seems like it doesn't work - the gif file didn't move, it displayed like a normal Image. 我正在尝试将.gif文件添加到JButton中,但似乎无法正常工作-gif文件没有移动,显示为普通图像。
Here are my codes: 这是我的代码:
+The ImageButton Class: + 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);
}
}
+The Test Class: import java.awt.FlowLayout; +测试类:import 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);
}
} }
I used a looping .gif file. 我使用了循环的.gif文件。
So how can I fix it ? 那么我该如何解决呢? thanks a lot :'( 非常感谢 :'(
JButton button = new JButton(new ImageIcon(getClass().getClassLoader().getResource("Images/BBishopB.gif")));
OR Full Example : 或完整示例:
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);
}
}
well, you might try this for your animation... 好吧,您可以尝试将其用于动画...
File file = new File("img/ani.gif");
URL url = file.toURI().toURL();
Icon icon = new ImageIcon(url);
JButton aniButt = new JButton(icon);
maybe you must also set the mouseover and pressed icons as well.. 也许您还必须设置鼠标悬停并按下图标。
but read the https://stackoverflow.com/a/18271876/714968 ... (as mKorbel metioned!) 但请阅读https://stackoverflow.com/a/18271876/714968 ...(如mKorbel所言!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.