实现起来不难,不过要做到精简就难点啦
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lefttoright extends JFrame implements Runnable ,ActionListener
{
int x,y,a;
String s="I Love JAVA";
JLabel l1=new JLabel();
JTextField t1=new JTextField(5);
JButton b1=new JButton("OK");
public lefttoright()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
l1.setText("输入速度(表示线程的sleep时间)");
b1.addActionListener(this);
add(l1); add(t1);
add(b1);
setSize(400,300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
a=Integer.parseInt(t1.getText());
}
public void run()
{
y = ((this.getHeight())/2);
x=0;
while(true)
{
try {
Thread.sleep(a);
} catch (Exception e) {}
if( x++>(this.getWidth()))
{
x=0;
}
System.out.println(this.getWidth());
this.repaint();
}
}
public void paint(Graphics g)
{
super.paint(g);
g.drawString(s,x,y);
}
public static void main(String[] args) {
new Thread(new lefttoright()).start();
}
}