How to add an image to a JPanel?

前端 未结 14 1546
陌清茗
陌清茗 2020-11-22 00:01

I have a JPanel to which I\'d like to add JPEG and PNG images that I generate on the fly.

All the examples I\'ve seen so far in the Swing Tutorials, specially in the

14条回答
  •  花落未央
    2020-11-22 00:46

    JPanel is almost always the wrong class to subclass. Why wouldn't you subclass JComponent?

    There is a slight problem with ImageIcon in that the constructor blocks reading the image. Not really a problem when loading from the application jar, but maybe if you're potentially reading over a network connection. There's plenty of AWT-era examples of using MediaTracker, ImageObserver and friends, even in the JDK demos.

提交回复
热议问题