动画/滚动文本

Din*_*ari 1 java user-interface swing text marquee

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

mKo*_*bel 5

可能不是OP的答案,但我看不出原因,非常简单的工具Swing Timer,(可能是半透明容器)并放在那里JLabel,(JLabel可能来自Array的更新,Chars以避免容器的大小调整),例如

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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)