How to add an image to a JPanel?

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

    This answer is a complement to @shawalli's answer...

    I wanted to reference an image within my jar too, but instead of having a BufferedImage, I simple did this:

     JPanel jPanel = new JPanel();      
     jPanel.add(new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resource/images/polygon.jpg"))));
    
    0 讨论(0)
  • 2020-11-22 00:58

    You can avoid using own Components and SwingX library and ImageIO class:

    File f = new File("hello.jpg");
    JLabel imgLabel = new JLabel(new ImageIcon(file.getName()));
    
    0 讨论(0)
提交回复
热议问题