Change Images on jframe when a button is clicked in Java

前端 未结 2 2006
心在旅途
心在旅途 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.

    0 讨论(0)
  • 2021-01-28 02:36

    You can also use ImageIcon like this

    image = new ImageIcon(imageList[1]);
    

    and when each time button is clicked you can change image like this

    label.setIcon(image);
    
    0 讨论(0)
提交回复
热议问题