Displaying contents of String array in Swing component as iterations using Time delay. JAVA(使用 Time delay 将 Swing 组件中 String 数组的内容显示为迭代.JAVA)
问题描述
我有一个字符串数组,我试图在 Java Swing 组件中以幻灯片的形式(一个接一个)显示.我也在尝试在迭代之间添加延迟时间.
I have an array of strings which I'm trying to display (one by one) as a slideshow in a Java Swing component. I am also trying to add a delay time between the iterations.
我尝试通过使用 JTextArea 来完成此操作,并添加了一个动作侦听器.这是我现在的代码:
I attempted to do this by using a JTextArea, with an action listener added to it. Here is the code I have right now:
private class myActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// A BUNCH OF TEXT PROCESSING
//NOTE: myInfo.getContents() returns an ArrayList<myType>.
Iterator<myType> iterator = myInfo.getContents().iterator();
int i = 0;
while (iterator.hasNext()) {
myTextArea.setText(iterator.next().toString());
// to add time betweeen iterations i wanted to use the thread
// delay method.
}
}
}
我的代码无法正常工作,因为 JTextArea 没有动作监听器.
My code is not working because JTextArea doesn't have an action listener.
更新注意:许多回复说我应该为 JTextArea 使用 ActionListener;但是,Eclipse 并没有告诉我 JTextArea 有一个名为 addActionListener 的方法.
UPDATE NOTE: Many replies stated that I should use an ActionListener for the JTextArea; However, Eclipse is not showing me that JTextArea has a method called addActionListener.
我有点卡在这里,您认为哪个 Java Swing 组件最适合这种情况?
我的数组中的文本可能很长,所以单行标签不是一个好的选择.
The text in my array may be long, so a one lined label would not be a good choice.
我还有哪些其他选择或方法?
What other alternatives or approaches do I have?
非常感谢,感谢您的任何帮助和建议.
Thank you very much, any help and suggestions are appreciated.
推荐答案
将 ActionListener
与 javax.Swing.Timer
结合使用.分配给 Timer
的 ActionListener
将以指定的延迟定期调用.
Use your ActionListener
in combination with a javax.Swing.Timer
. The ActionListener
assigned to the Timer
will be called on regular intervals with the specified delay.
有关详细信息,请参阅计时器教程
这篇关于使用 Time delay 将 Swing 组件中 String 数组的内容显示为迭代.JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Time delay 将 Swing 组件中 String 数组的内容显示为迭代.JAVA


- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 转换 ldap 日期 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01