How to add an image to a JPanel?

前端 未结 14 1530
陌清茗
陌清茗 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:38

    If you are using JPanels, then are probably working with Swing. Try this:

    BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
    JLabel picLabel = new JLabel(new ImageIcon(myPicture));
    add(picLabel);
    

    The image is now a swing component. It becomes subject to layout conditions like any other component.

提交回复
热议问题