在java应用程序中我有一个Jlabel,我想在每次单击一个按钮时分配一个新图像,使用for循环我可以让它只显示跳过图像之间的所有最后一个图像,我知道那里我的逻辑中出现错误,也许我不应该使用for循环?任何建议
in a java aplication I have a Jlabel which i want to assign a new image to every time i click a button, using a for loop i can get it to just display the last image skipping all in between images, i know there is a error in my logic here maybe i should not be using a for loop?? any advice
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。这里最好的做法是什么?谢谢
if i dont use a for loop and simply use a counter (displayed below) which i declare outside of the button method it loops correctly displaying the images but runs into a ArrayIndexOutOfBoundsException. what is the best practice here? thanks
image = new ImageIcon(imageList[imgCount]); imageLbl.setIcon(image); imgCount++;推荐答案
从本质上讲,你阻止事件调度线程,防止它更新UI。有关更多详细信息,请参阅 Swing中的并发
You're, essentially, blocking the Event Dispatching Thread, prevent it from updating the UI. See Concurrency in Swing for more details
相反,您应该使用 javax.swing.Timer 来循环覆盖图像,允许UI在更改为下一个之前进行更新...
Instead, you should use a javax.swing.Timer to loop over the images, allowing the UI to update before changing to the next one...
请参阅如何使用Swing Timers 获取更多详细信息。
See How to use Swing Timers for more details.
Java数组是零索引的,这意味着数组中的第一个元素是一个位置 0 ,不是 1
Java arrays are zero indexed, this means that the first element in the array is a position 0, not 1
不要引用 src 直接在您的代码中,一旦构建和打包应用程序, src 目录将不存在
Don't reference src directly within your code, the src directory will not exist once the application is built and packaged
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); } } }更多推荐
通过按钮单击循环浏览Jlabel的图像时出现循环问题
发布评论