gpt4 book ai didi

java - for循环JFrame图像替换

转载 作者:行者123 更新时间:2023-12-02 04:47:15 24 4
gpt4 key购买 nike

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)
{

}
}

但是它没有更新 JFrame。我在这个类之外设置了 Jframe...

这个愚蠢的东西要求我提供更多细节,所以这只是华夫饼...................................... …………

最佳答案

您不应该将 Thread.sleep 与 Swing GUI 一起使用,因为您将使 GUI 进入休眠状态。使用Swing Timer帮助您交换图像而不占用 Swing 事件线程。另外,不要继续添加 JLabel,而是交换单个稳定 JLabel 的 ImageIcon。

类似于

  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();

例如,

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();
}
});
}
}

关于java - for循环JFrame图像替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29564227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com