在JLabel中按字母“动画”显示字符串?

时间:2013-09-17 01:56:50

标签: java string swing loops

有没有办法显示一个短语,本身,“欢迎!”,他们之间有一个非常小的延迟?我会提供我尝试过的东西,但我甚至没有接近几乎没有工作,没有什么值得一提的。我想我必须使用包含扫描仪的循环,是吗?任何帮助表示感谢,谢谢:)

1 个答案:

答案 0 :(得分:3)

<强>注意事项

Swing是一个单线程框架,也就是说,对UI的所有更新和修改都应该在Event Dispatching Thread的上下文中执行。

同样地,任何阻止EDT的操作都会阻止它处理(除其他事项外)绘制更新,这意味着在删除块之前不会更新UI。

示例

有几种方法可以实现这一目标。你可以使用SwingWorker虽然这是一个很好的学习练习,但对于这个问题来说可能会有点过分。

相反,您可以使用javax.swing.Timer。这允许您定期安排回调,这些回调在EDT的上下文中执行,您可以安全地更新UI。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 AnimatedLabel {

    public static void main(String[] args) {
        new AnimatedLabel();
    }

    public AnimatedLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setSize(100, 100);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private String text = "Hello";
        private JLabel label;
        private int charIndex = 0;

        public TestPane() {
            setLayout(new GridBagLayout());
            label = new JLabel();
            add(label);
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String labelText = label.getText();
                    labelText += text.charAt(charIndex);
                    label.setText(labelText);
                    charIndex++;
                    if (charIndex >= text.length()) {
                        ((Timer)e.getSource()).stop();
                    }
                }
            });
            timer.start();
        }
    }
}

请查看Concurrency in Swing了解详情

从评论中更新

主要问题是您的text值包含在<html>

static String text = "<html>Welcome! I will ask simple, two-answer questions, and you will answer them. Simple as that. </html>";

然后你将它应用到你的标签......

final JLabel centerText = new JLabel(text);

因此,当计时器运行时,它最终会再次附加文本......

"<html>Welcome! I will ask simple, two-answer questions, and you will answer them. Simple as that. </html><html>Welcome! I will ask simple, two-answer questions, and you will answer them. Simple as that. </html>"

哪个无效,因为</html>之后的所有内容都将被忽略。

而是从<html>

中删除text标记
static String text = "Welcome! I will ask simple, two-answer questions, and you will answer them. Simple as that.";

并使用<html>

设置标签的初始文本
final JLabel centerText = new JLabel("<html>);

别担心,Swing会照顾它......