簡體   English   中英

線程“AWT-EventQueue-0”中的異常 java.lang.IllegalArgumentException: 寬度 (-1) 和高度 (-1) 不能 <= 0

[英]Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0

嗨,我正在嘗試向我的按鈕添加一個圖標,它似乎適用於一個圖像,但是一旦我將圖像更改為其他圖像,我就會收到此錯誤:唯一的區別是前一張圖像的分辨率為 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)

..一旦我將圖像更改為其他圖像,我就會收到此錯誤:唯一的區別是前一張圖像的分辨率為 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);
    }
}

圖片有問題。 我實際上使用在線編輯器從圖像創建了一個圖標,該編輯器為我提供了一個 *.ico 文件,我已將其轉換為 *.png。 因此得到了錯誤。 當我使用另一個圖像編輯器給我一個 *.jpg/*.png 格式的輸出圖像時,它起作用了。 並檢查您的路徑是否正確。 將圖標設置為框架(任務欄)時出現錯誤。 因此,請檢查您的圖像路徑是否正確。

所以你是說這段代碼對你來說失敗了? 它在這里工作得很好。 - 說“安德魯·湯普森”

所以這是路徑錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM