您好我正在尝试向我的按钮添加一个图标,它似乎对一个图像工作正常但是只要我将图像更改为其他图像我就会收到此错误:唯一的区别是前一个图像有一个分辨率为48x48,新图像的分辨率为32x32
以下是代码:
dashboardButton = new JButton("Dashboard", new ImageIcon("images/dashbd.png"));
dashboardButton.addActionListener(this);
topPanel.add(dashboardButton, topCC.xy(2,2));
这是错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:338)
at in.tcs.dct.ui.custom.UnifiedToolbarButtonUI.paintIcon(UnifiedToolbarButtonUI.java:48)
at javax.swing.plaf.basic.BasicButtonUI.paint(BasicButtonUI.java:216)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:793)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:121)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:221)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:219)
at java.awt.Dialog.show(Dialog.java:1072)
at javax.swing.JOptionPane.showOptionDialog(JOptionPane.java:871)
at javax.swing.JOptionPane.showConfirmDialog(JOptionPane.java:797)
at javax.swing.JOptionPane.showConfirmDialog(JOptionPane.java:759)
at in.tcs.dct.ui.DCTFrame.windowClosing(DCTFrame.java:146)
at java.awt.AWTEventMulticaster.windowClosing(AWTEventMulticaster.java:349)
at java.awt.Window.processWindowEvent(Window.java:2045)
at javax.swing.JFrame.processWindowEvent(JFrame.java:296)
at java.awt.Window.processEvent(Window.java:2003)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
答案 0 :(得分:1)
..一旦我将图像更改为其他图像,我就会收到此错误:唯一的区别是前一张图像的分辨率为48x48,而新图像的分辨率为32x32
所以你说这段代码不适合你?它在这里工作得很好。
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ChangingIconResolution {
public static void main(String[] args) throws Exception {
final BufferedImage img32 = new BufferedImage(
32, 32, BufferedImage.TYPE_INT_RGB);
final BufferedImage img48 = new BufferedImage(
48, 48, BufferedImage.TYPE_INT_RGB);
Runnable r = new Runnable() {
@Override
public void run() {
final JButton btn = new JButton(new ImageIcon(img48));
ActionListener animationAction = new ActionListener() {
int index = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (index++%3==0) {
btn.setIcon(new ImageIcon(img32));
} else {
btn.setIcon(new ImageIcon(img48));
}
}
};
Timer timer = new Timer(1000, animationAction);
timer.start();
JOptionPane.showMessageDialog(null, btn);
timer.stop();
}
};
SwingUtilities.invokeLater(r);
}
}
答案 1 :(得分:0)
猜测图像有问题..我实际上是使用在线编辑器从图像创建了一个图标,这给了我一个.ico文件...我已经转换为.png ..hence得到了错误...当我使用另一个图像编辑器给我一个.jpg / .png格式的outpur图像时,它可以工作