How to add an image to a JPanel?

前端 未结 14 1561
陌清茗
陌清茗 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 00:42

    Create a source folder in your project directory, in this case I called it Images.

    JFrame snakeFrame = new JFrame();
    snakeFrame.setBounds(100, 200, 800, 800);
    snakeFrame.setVisible(true);
    snakeFrame.add(new JLabel(new ImageIcon("Images/Snake.png")));
    snakeFrame.pack();
    

提交回复
热议问题