将 .gif 图像作为图标添加到输出 window
Add .gif image as icon to the output window
我想在我正在开发的 NetBeans 平台应用程序的输出 window 中添加一个加载动画 gif 图标。我设法添加了一个 png 图标文件。但在这种情况下,添加的 gif 图标不是动画。它保持不变。
private class Loading extends AbstractAction {
public Loading() {
//putValue(SMALL_ICON, ImageUtilities.loadImageIcon("org/netbeans/modules/plsql/execution/loading.gif", true));
putValue(SMALL_ICON, ImageUtilities.loadImage("org/netbeans/modules/plsql/execution/loading.gif",true));
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action performed");
}
}
这是我用作输出的 window。
final InputOutput io = IOProvider.getDefault().getIO("Deploy Dependents", new Action[]{new Loading()});
你可以试试下面的代码,它正在加载带有动画的 gif 图片。
我认为你只需要这部分代码:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("gif.gif").getFile());
Image image = Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(file)));
ImageIcon icon = new ImageIcon(image);
下面添加了完整的代码示例:
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class AnimationTest extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AnimationTest test = new AnimationTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
});
}
public AnimationTest() {
super();
try {
JLabel label = new JLabel();
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("gif.gif").getFile());
Image image =
Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.
toByteArray(new FileInputStream(file)));
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);
icon.setImageObserver(label);
add(label);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
}
将与 jave 项目相同的代码添加到 github,您可以从那里获得完整代码。
loading gif image to java with animation
此外,您需要使用下面的 apache commons 依赖作为示例代码
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
您需要将按钮添加为您使用 ImageUtilities.loadImage()
加载的 Image
的 ImageObserver
。它将为您处理动画。
对按钮本身的访问可能会被 IOProvider
class 隐藏,但如果您设法获得它的句柄,只需调用 image.setImageObserver(button)
即可看到动画运行.
我想在我正在开发的 NetBeans 平台应用程序的输出 window 中添加一个加载动画 gif 图标。我设法添加了一个 png 图标文件。但在这种情况下,添加的 gif 图标不是动画。它保持不变。
private class Loading extends AbstractAction {
public Loading() {
//putValue(SMALL_ICON, ImageUtilities.loadImageIcon("org/netbeans/modules/plsql/execution/loading.gif", true));
putValue(SMALL_ICON, ImageUtilities.loadImage("org/netbeans/modules/plsql/execution/loading.gif",true));
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action performed");
}
}
这是我用作输出的 window。
final InputOutput io = IOProvider.getDefault().getIO("Deploy Dependents", new Action[]{new Loading()});
你可以试试下面的代码,它正在加载带有动画的 gif 图片。
我认为你只需要这部分代码:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("gif.gif").getFile());
Image image = Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(file)));
ImageIcon icon = new ImageIcon(image);
下面添加了完整的代码示例:
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class AnimationTest extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AnimationTest test = new AnimationTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
});
}
public AnimationTest() {
super();
try {
JLabel label = new JLabel();
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("gif.gif").getFile());
Image image =
Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.
toByteArray(new FileInputStream(file)));
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);
icon.setImageObserver(label);
add(label);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
}
将与 jave 项目相同的代码添加到 github,您可以从那里获得完整代码。
loading gif image to java with animation
此外,您需要使用下面的 apache commons 依赖作为示例代码
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
您需要将按钮添加为您使用 ImageUtilities.loadImage()
加载的 Image
的 ImageObserver
。它将为您处理动画。
对按钮本身的访问可能会被 IOProvider
class 隐藏,但如果您设法获得它的句柄,只需调用 image.setImageObserver(button)
即可看到动画运行.