Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以将动画gif文件添加到JButton吗?_Java_Eclipse_Swing_Jbutton_Animated Gif - Fatal编程技术网

Java 我可以将动画gif文件添加到JButton吗?

Java 我可以将动画gif文件添加到JButton吗?,java,eclipse,swing,jbutton,animated-gif,Java,Eclipse,Swing,Jbutton,Animated Gif,我正试图将一个.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.J

我正试图将一个.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文件


那我该怎么修呢?非常感谢:“(

嗯,你可以在你的动画中试试这个

File file = new File("img/ani.gif");
URL url = file.toURI().toURL();
Icon icon = new ImageIcon(url);
JButton aniButt = new JButton(icon);
也许您还必须设置鼠标悬停和按下的图标

但请阅读…(如mKorbel所述!)

或 完整示例:

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);
    }
}

它不能回答我的问题-gif文件可以很容易地显示在JLabel上,但不能显示在我的JButton中。by@MadProgrammerdon不要子类JButton,如果是,则覆盖Api中实现的set(Xxx)图标
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);
    }
}