gpt4 book ai didi

java - 单击按钮循环浏览 Jlabel 图像时出现 for 循环问题

转载 作者:行者123 更新时间:2023-12-01 22:26:14 24 4
gpt4 key购买 nike

在java应用程序中,我有一个Jlabel,我想在每次单击按钮时为其分配一个新图像,使用for循环我可以让它只显示最后一个图像,跳过图像之间的所有图像,我知道那里我的逻辑有错误吗也许我不应该使用 for 循环?任何建议

 private String imageList[];
ImageIcon image;
imageList = new String[] {"src\\Tour_Eiffel_Wikimedia_Commons.jpg","src\\Ben.jpg", "src\\Rio.jpg", "src\\Liberty.jpg", "src\\Pyramid.jpg"};

//constructor setting first image to display on load
public GeographyGameGUI() {
image = new ImageIcon(imageList[0]);
imageLbl.setIcon(image);
}

//button method
private void nextBtnActionPerformed(java.awt.event.ActionEvent evt) {


for (imgCount = 1; imgCount < imageList.length; imgCount++) {
image = new ImageIcon(imageList[imgCount]);
imageLbl.setIcon(image);

}

如果我不使用 for 循环,而只是使用一个计数器(如下所示),我在按钮方法之外声明它,它会正确循环显示图像,但会遇到 ArrayIndexOutOfBoundsException。这里的最佳实践是什么?谢谢

 image = new ImageIcon(imageList[imgCount]);
imageLbl.setIcon(image);
imgCount++;

最佳答案

本质上,您阻塞了事件调度线程,阻止它更新 UI。请参阅Concurrency in Swing了解更多详情

相反,您应该使用 javax.swing.Timer 循环图像,允许 UI 在更改到下一个之前更新...

参见How to use Swing Timers了解更多详情。

Java 数组的索引为零,这意味着数组中的第一个元素是位置 0,而不是 1

不要直接在代码中引用 src,一旦构建并打包应用程序,src 目录将不存在

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JLabel label;
private String[] imageList = new String[] {"/Tour_Eiffel_Wikimedia_Commons.jpg","/Ben.jpg", "/Rio.jpg", "/Liberty.jpg", "/Pyramid.jpg"};

public TestPane() {
setLayout(new BorderLayout());
label = new JLabel();
add(label);

JButton btn = new JButton("Play");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn.setEnabled(false);
Timer timer = new Timer(1000, new ActionListener() {
private int count;
@Override
public void actionPerformed(ActionEvent e) {
if (count < imageList.length) {
try {
label.setIcon(
new ImageIcon(
ImageIO.read(
TestPane.this.getClass().getResource(imageList[count]))));
} catch (IOException exp) {
exp.printStackTrace();
}
count++;
} else {
((Timer)e.getSource()).stop();
}
}
});
timer.stop();
}
});
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

}

}

关于java - 单击按钮循环浏览 Jlabel 图像时出现 for 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28740572/

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