Change Images on jframe when a button is clicked in Java

前端 未结 2 2005
心在旅途
心在旅途 2021-01-28 01:57

I have been trying to figure this out why not the next picture showing on the same panel after click the button. I want to separate those classes not into one class and used rep

2条回答
  •  借酒劲吻你
    2021-01-28 02:24

    Why not change your approach and make use of a JLabel instead? Set your image as an icon on the label and add it to your JPanel:

    BufferedImage image = ImageIO.read(new File("image-path"));
    JLabel label = new JLabel(new ImageIcon(image));
    panel.add(label);
    

    You can then make subsequent calls to JLabel#setIcon(...) each time you want the image to change.

提交回复
热议问题