import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
public class Test extends JPanel {
private static final long serialVersionUID = 4767050156491994899L;
private JLabel label;
private String scrolledText;
public Test() {
scrolledText = "滚动文字 Demo";
label = new JLabel(new String(scrolledText));
this.add(label);
Thread thread = new Thread(new TextChanger(label));
thread.start();
}
// @Override
// protected void paintComponent(Graphics g) {
// super.paintComponent(g);
//
// Graphics2D g2d = (Graphics2D) g;
//
// }
private static void createAndShowGUI() {
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.getContentPane().add(new Test());
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Test.createAndShowGUI();
}
});
}
}
class TextChanger implements Runnable {
private JLabel label;
public TextChanger(JLabel label) {
this.label = label;
}
public void run() {
try {
while (true) {
String text = label.getText();
if (text.length() > 1) {
text = text.substring(1, text.length()) + text.charAt(0);
label.setText(text);
// Get the frame
Component frame = SwingUtilities.getRoot(label);
if (frame != null && (frame instanceof JFrame)) {
((JFrame)frame).setTitle(text);
}
label.repaint();
}
Thread.sleep(300);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}