【问题标题】:Animate/scroll text动画/滚动文本
【发布时间】:2012-04-05 12:10:39
【问题描述】:

我想知道如何制作滚动文本。就像可以从右向左滚动的文本一样。如何在 Java GUI 中为文本设置动画?

【问题讨论】:

  • AFAIK,Java 不一定是为制作文本动画而设计的。您可以做的是将AffineTransforms 应用于Label,但我真的怀疑它看起来会不会好。也许有一些库可以支持这种东西。
  • 您是要移动 JTextField、JLabel 中的文本,还是移动 Textfield/标签,还是移动面板上的文本?在最后一种情况下:graphics.drawString 是你需要的。
  • 移入 JLabel。感谢您的帮助。我希望它的工作

标签: java swing user-interface text marquee


【解决方案1】:

也许不是 OP 的答案,但我看不出原因,通过实现 Swing Timer 非常简单,(可能与半透明容器一起使用)并放一个 JLabel,(对 JLabel 的更新可能是来自CharsArray 以避免调整容器大小),例如

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.Timer;

public class SlideTextSwing {

    private JWindow window = new JWindow();
    private JLabel label = new JLabel("Slide Text Swing, Slide Text Swing, ..........");
    private JPanel windowContents = new JPanel();

    public SlideTextSwing() {
        windowContents.add(label);
        window.add(windowContents);
        window.pack();
        window.setLocationRelativeTo(null);
        final int desiredWidth = window.getWidth();
        window.getContentPane().setLayout(null);
        window.setSize(0, window.getHeight());
        window.setVisible(true);
        Timer timer = new Timer(20, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int newWidth = Math.min(window.getWidth() + 1, desiredWidth);
                window.setSize(newWidth, window.getHeight());
                windowContents.setLocation(newWidth - desiredWidth, 0);
                if (newWidth >= desiredWidth) {
                    ((Timer) e.getSource()).stop();
                    label.setForeground(Color.red);
                    mainKill();
                }
            }
        });
        timer.start();
    }

    public void mainKill() {
        Timer timer = new Timer(500, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        timer.start();
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                SlideTextSwing windowTest = new SlideTextSwing();
            }
        });
    }
}

【讨论】:

  • 几个相关的方法显示here
  • 谢谢你的语法,它对我有帮助。但是,我想用 GUI Java 在 JLabel 中滚动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
相关资源
最近更新 更多