问题描述
我有一个字符串数组,我试图(逐个)显示为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的方法。
UPDATENOTE: 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
结合使用。分配给计时器
的 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.
请参阅更多信息
这篇关于使用时间延迟将Swing组件中的String数组的内容显示为迭代。 JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!