简体   繁体   中英

How to update an ImageIcon in JLabel?

import javax.swing.*;
{

JFrame ShoppingMarket=new JFrame();
{
  ShoppingMarket.add(new JLabel(new ImageIcon("C:\\Users\\admin   \\Desktop\\The Da Vinci Code.jpg")));
  ShoppingMarket.pack();
  ShoppingMarket.setVisible(true);  
}
ShoppingMarket.add(new JLabel(new ImageIcon("C:\\Users\\admin   \\Desktop\\Angels And Demons.jpg")));
ShoppingMarket.pack();
ShoppingMarket.setVisible(true);    
}

It doesn't clear the screen on closing the first window. so both pictures are shown on screen.

Instead of adding a new JLabel to the frame, store the original to a field of the enclosing class. Then you can simply call label.setIcon(theNewIcon) , where I'm assuming the field is named label . See the relevant documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM