java - 在 JLabel 中逐字母显示字符串 "animation"?

标签 java string swing loops

有没有办法逐个字母地显示短语“欢迎!”,并且它们之间的延迟很小?我会提供我尝试过的东西,但我什至还没有接近勉强工作,没有什么值得一提的。我想我必须使用包含扫描仪的循环,是吗?感谢任何帮助,谢谢:)

最佳答案

注意事项

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

同样,任何阻止 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 会解决这个问题...

关于java - 在 JLabel 中逐字母显示字符串 "animation"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18840120/

相关文章:

java - 从类的完全限定名中获取类

java - 一次只打开一个JInternalFrame

java - 重新绘制具有不同渐变的 Swing 按钮

java - 在 Java 桌面应用程序中使用 Google map 的替代方案

java - 使用 GregorianCalendar 并添加 4 年时,为什么日期显示得更远?

java - mule4/java 将 Sql ddl 模式转换为 json 模式

java - 在 Tomcat6 中部署 webapp

python - Pandas 和应用函数来匹配一个字符串

c - 修改字符串数组

python - 拆分字符串的最佳方法