如何將影象新增到JPanel? - How to add an image to a JPanel?

語言: CN / TW / HK

問題:

I have a JPanel to which I'd like to add JPEG and PNG images that I generate on the fly. 我有一個JPanel ,我想向其中新增即時生成的JPEG和PNG影象。

All the examples I've seen so far in the Swing Tutorials , specially in the Swing examples use ImageIcon s. 到目前為止,我在Swing教程中看到的所有示例,特別是在Swing示例中,都使用ImageIcon

I'm generating these images as byte arrays, and they are usually larger than the common icon they use in the examples, at 640x480. 我將這些影象生成為位元組陣列,它們通常比示例中使用的通用圖示大,尺寸為640x480。

  1. Is there any (performance or other) problem in using the ImageIcon class to display an image that size in a JPanel? 使用ImageIcon類在JPanel中顯示該大小的影象時是否存在任何(效能或其他)問題?
  2. What's the usual way of doing it? 通常的做法是什麼?
  3. How to add an image to a JPanel without using the ImageIcon class? 如何不使用ImageIcon類將影象新增到JPanel?

Edit : A more careful examination of the tutorials and the API shows that you cannot add an ImageIcon directly to a JPanel. 編輯 :對教程和API的更仔細的檢查表明,您不能將ImageIcon直接新增到JPanel。 Instead, they achieve the same effect by setting the image as an icon of a JLabel. 而是通過將影象設定為JLabel的圖示來實現相同的效果。 This just doesn't feel right... 只是感覺不對...


解決方案:

參考一: http://stackoom.com/question/1FuZ
參考二: How to add an image to a JPanel?
「其他文章」