通过按钮单击循环浏览Jlabel的图像时出现循环问题

编程入门 行业动态 更新时间:2024-10-28 10:22:06
本文介绍了通过按钮单击循环浏览Jlabel的图像时出现循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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的图像时出现循环问题

本文发布于:2023-11-10 01:40:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   图像   Jlabel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!