使用 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.
有关详细信息,请参阅计时器教程
相关文章