一个简易Jframe滚动字的实现

import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class WelcomeJFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;

public WelcomeJFrame (String texts[]) {
    super("滚动字");
    this.setBounds(300, 240, 400, 300);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    if(texts==null||texts.length==0)
         this.getContentPane().add(new GuopengJPanel("welcome"));
     else {
         this.getContentPane().setLayout(new GridLayout(texts.length, 1));
            for(int i=0;i<texts.length;i++)
                this.getContentPane().add(new GuopengJPanel(texts[i]));

    }
    this.setVisible(true);

}
public  WelcomeJFrame() {
    this(null);
}
private class GuopengJPanel extends JPanel implements ActionListener,Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JTextField text_word,text_sleep,text_state=null;
    JButton button_start,button_interrupt;
    Thread thread;
    int sleeptime;
    GuopengJPanel(String text) {
        this.setLayout(new GridLayout(2,1));
        text_word =new JTextField(String.format("%115s", text));
        this.add(text_word);
        JPanel panel_sub=new JPanel();
        this.add(panel_sub);
        panel_sub.add(new Label("sleep"));
        this.sleeptime=(int)(Math.random()*100);
        text_sleep=new JTextField(""+sleeptime,5);
        panel_sub.add(text_sleep);
        text_sleep.addActionListener(this);
        button_start=new JButton("启动");
        panel_sub.add(button_start);
        button_start.addActionListener(this);
        button_interrupt=new JButton("中断");
        panel_sub.add(button_interrupt);
        button_interrupt.addActionListener(this);
        thread =new Thread(this);
        button_interrupt.setEnabled(false);
        panel_sub.add(new JLabel("state"));
        text_state=new JTextField(""+thread.getState(),10);
        text_state.setEditable(false);
        panel_sub.add(text_state);

    }
    public void run() {
        while (true) {
            String str = text_word.getText();
            text_word.setText(str.substring(3)+str.substring(0,3));
            try {
                Thread.sleep(sleeptime);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                break;
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
           if(e.getSource()==button_start){
               thread= new Thread(this);
               thread.start();
               text_state.setText(""+thread.getState());
               button_start.setEnabled(false);
               button_interrupt.setEnabled(true);

           }    
           if (e.getSource()==button_interrupt) {
            thread.interrupt();
            text_state.setText(""+thread.getState());
            button_start.setEnabled(true);
            button_interrupt.setEnabled(false);

        }
           if (e.getSource()==text_sleep) {
               try{
            sleeptime=Integer.parseInt(text_sleep.getText());}
               catch(NumberFormatException nfex)
            {JOptionPane.showMessageDialog(this, "\""+text_sleep.getText()+"\"不能转换成整数,请重新输入!");}
        }
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String texts[]={"welcome","hello","Guopeng","handsome"};
    new WelcomeJFrame(texts);

}

}
text_word.setText(str.substring(3)+str.substring(0,3));求这段滚动实现代码的解释 我有点转不过来

分析代码可以看到,text_word的初始化时是有115个空格+指定字符串的,而这行代码则是将0到3位置处的空格移到字符串尾部,第三个空格以后的信息提到前面,给人的错觉就是非空格字符串前移了。
主要代码是这个初始化代码:

  //以hello为例,初始化值是:115个空格+hello
  text_word =new JTextField(String.format("%115s", text));
    //线程循环run中执行,下面这行代码则将文本值设置为:112个空格+hello
 text_word.setText(str.substring(3)+str.substring(0,3));

这行主要代码给人的感觉就是hello这个字符串每次都往前挪动了3个字符的位置,线程在循环执行该操作,知道hello到达首部为止。

http://blog.csdn.net/shirenfeigui/article/details/38848037