使用 Time delay 将 Swing 组件中 String 数组的内容显示为迭代.JAVA

Displaying contents of String array in Swing component as iterations using Time delay. JAVA(使用 Time delay 将 Swing 组件中 String 数组的内容显示为迭代.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.

推荐答案

ActionListenerjavax.Swing.Timer 结合使用.分配给 TimerActionListener 将以指定的延迟定期调用.

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

基础教程推荐