public void run() {
frame = new JFrame("JFrame 1");
Container contentPane = frame.getContentPane();
JLabel label = new JLabel("labelling.....");
frame.setPreferredSize(new Dimension(400,600));
frame.pack();
frame.setVisible(true);
// TODO Auto-generated method stub
try
{
for(int i = 0; i < 4; i++)
{
frame.add(new JLabel(new ImageIcon("image1.jpg")));
Thread.sleep(000);
frame.add(new JLabel(new ImageIcon("image1.jpg")));
Thread.sleep(2000);
frame.add(new JLabel(new ImageIcon("image1.jpg")));
Thread.sleep(2000);
}
}
catch(InterruptedException e)
{
}
}
however it is not updating the JFrame. I have the Jframe set just outside of this class...
This silly thing is asking me for more details so this is just waffle................................................
You shouldn't use Thread.sleep with Swing GUIs as you're doing as you'll put the GUI to sleep. Use a Swing Timer to help you swap images without tying up the Swing event thread. Also, don't keep adding JLabels, but rather swap the ImageIcon of a single stable JLabel.
Something like
int timerDelay = 500;
new Timer(timerDelay, new ActionListener() {
private boolean firstIcon = true;
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (count >= MAX_COUNT) {
((Timer) e.getSource()).stop(); // stop the timer
return;
}
// swap icons
Icon icon = firstIcon ? icon1 : icon2;
label.setIcon(icon);
firstIcon = !firstIcon;
count++;
}
}).start();
eg,
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
public class SwapImages extends JPanel {
public static final String PATH1 = "https://duke.kenai.com/iconSized/duke.gif";
public static final String PATH2 = "https://duke.kenai.com/iconSized/penduke-transparent.gif";
public static final String[] PATHS = {PATH1, PATH2};
protected static final int MAX_COUNT = 8;
private JLabel label = new JLabel();
private List<Icon> icons = new ArrayList<>();
private int index = 0;
public SwapImages() throws IOException {
for (String path : PATHS) {
URL url = new URL(path);
BufferedImage img = ImageIO.read(url);
ImageIcon icon = new ImageIcon(img);
icons.add(icon);
}
label.setIcon(icons.get(index));
add(label);
int timerDelay = 500;
new Timer(timerDelay, new ActionListener() {
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (count >= MAX_COUNT) {
((Timer) e.getSource()).stop();
return;
}
index++;
index %= icons.size();
Icon icon = icons.get(index);
label.setIcon(icon);
count++;
}
}).start();
}
private static void createAndShowGUI() {
SwapImages paintEg = null;
try {
paintEg = new SwapImages();
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame("SwapImages");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
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.