我想知道如何制作滚动文本。就像可以从右向左滚动的文本一样。如何在 Java GUI 中为文本设置动画?
问问题
13749 次
1 回答
5
可能不是 OP 的答案,但我看不出原因,通过实现非常简单Swing Timer
,(可能与半透明容器一起)并放在那里JLabel
,(更新JLabel
可能来自Array
ofChars
以避免调整容器大小),例如
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();
}
});
}
}
于 2012-04-05T12:37:01.633 回答