Changing ImageIcon to another picture with button click

后端 未结 1 1506
野趣味
野趣味 2020-12-19 14:12

So I want to replace an ImageIcon in a JLabel every time a button is pressed. I made it so the image, label, and GridBagConstraints are public. When I try to change it thoug

相关标签:
1条回答
  • 2020-12-19 14:51

    Rather then trying to create and add a new label each time, simply call setIcon on the label itself

    Something more like...

    image = new ImageIcon(imageList[1]);
    label.setIcon(image);
    

    Check out How to use lables for more details.

    I might also suggest that you load the images first, so you don't need to keep reloading them/creating new objects on each actionPerformed

    I'd also recommend ImageIO over ImageIcon

    0 讨论(0)
提交回复
热议问题