我創建了一個運行良好的小程序,然後我需要添加一個GUI,以便將所有代碼移植到使用NetBeans,使事情變得更簡單。當我使用jpgs時,我的工作很好,但顯然這不是最好的格式。之前,當我使用PNG時,它運行良好,現在我在控制檯中得到一大堆錯誤,這是我以前從未得到的。 本質上,程序讀取用戶選擇的文件作爲bufferedimage,並將GUI中的標籤設置爲所選文件,或者直到創建GUI。正如我所說,它使用jpg工作正常,沒有問題,所以我不太確定發生了什麼。它也閱讀它作爲一個BufferedImage細如IM打印文件路徑和BufferedImage的變量到控制檯,以便能同時看到那些都很好,那麼錯誤的大名單,其有以下幾種:螺紋使用.png作爲標籤圖標
例外「AWT -EventQueue-0「java.lang.ClassCastException: [我不能轉換爲[B java.awt.image.ColorModel.getAlpha(ColorModel.java:833)at java.awt.image.ColorModel.getRGB( ColorModel.java:878)在 sun.awt.image.ImageRepresentation.convertToRGB(ImageRepresentation.java:305) 在 sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:564) 在 個java.awt.image.AreaAveragingScaleFilter.accumPixels(AreaAveragingScaleFilter.java:213) 在 java.awt.image.AreaAveragingScaleFilter.setPixels(AreaAveragingScaleFilter.java:252) 在 sun.awt.image.OffScreenImageSource.sendPixels(OffScreenImageSource的.java:102) 在 sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:187) 在 sun.awt.image.OffScreenImageSource.addConsumer(OffScreenImageSource.java:66) 在 sun.awt。 image.OffScreenImageSource.startProduction(OffScreenImageSource.java:80) at java.awt.image.FilteredImageSource.startProduction(FilteredImageSource.java:183) 在 sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation.java:727) 在 sun.awt.image.ImageRepresentation.drawToBufImage(ImageRepresentation.java:802) 在sun.java2d.pipe.DrawImage.copyImage( (SunGraphics2D.java:3084)at sun.java2d.SunGraphics2D.drawImage(()); SunGraphics2D.java:3069)在 javax.swing.ImageIcon.paintIcon(ImageIcon.java:413)在 sun.swing.plaf.synth.SynthIcon.paintIcon(SynthIcon.java:67)在 javax.swing.plaf中。 Synth.SynthGraphicsUtils.paintText(SynthGraphicsUtils.java:396) 處於javax.swing中。在javax.swing.JComponent.paintComponent(JComponent.java:778)上的plax.synth.SynthLabelUI.paint(SynthLabelUI.java:213) (位於javax.swing.plaf.synth.SynthLabelUI.update(SynthLabelUI.java:177) )在在 javax.swing.JComponent.paint(JComponent.java:1054)在 javax.swing.JComponent.paintChildren(JComponent.java:887)在 javax.swing.JComponent.paint(JComponent.java:1063) javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)at javax.swing.RepaintManager $ PaintManager.paintDoubleBuffered(RepaintManager.java:1512) at javax.swing.RepaintManager $ PaintManager.paint(RepaintManager.java:1443 ) at javax.swing.RepaintManager.paint(RepaintManager.java:1236)at javax.swing.JComponent._paintImmediately(JComponent.java:5169)at javax.swing.JComponent.paintImmediately(JComponent.java:4980)at javax.swing.RepaintManager $ 3。運行(RepaintManager.java:796) javax.swing.RepaintManager $ 3.run(RepaintManager.java:784) java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain的.java:76) 在 javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784) 在 javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757) 在 javax.swing.RepaintManager.prePaintDirtyRegions( RepaintManager.java:706) at javax.swing.RepaintManager.access $ 1000(RepaintManager.java:62)at javax.swing.RepaintManager $ ProcessingRunnable.run(RepaintManager.java:1651) at j ava.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)at java.awt.EventQueue.access $ 200(EventQueue.java:103)at java.awt.EventQueue $ 3.run(EventQueue.java:688)at java.awt.EventQueue $ 3.run(EventQueue.java:686)at java.security.AccessController.doPrivileged(Native Method)at java。 security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:697)在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 在 的Java。 awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138 ) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
隨着我對加載圖標之中代碼:
image1 = ImageIO.read(file);
ImageIcon imageIcon1 = new ImageIcon(image1);
Image redInput = imageIcon1.getImage().getScaledInstance(300, 300, Image.SCALE_SMOOTH);
imageIcon1.setImage(redInput);
redLabel.setIcon(imageIcon1);
我只有一個類叫NewJFrame
,任何幫助都會被大量讚賞!
要硬沒有一些代碼來診斷...... – MadProgrammer
噢,抱歉只是認爲我自己,編輯了這篇文章 – user2517280
你是否在試圖描繪你自我的形象?你能提供一個PNG例子嗎? – MadProgrammer