程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 線程-一個簡易Jframe滾動字的實現

線程-一個簡易Jframe滾動字的實現

編輯:編程綜合問答
一個簡易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到達首部為止。

Ace_Gp
Ace_Gp
Ace_Gp
wojiushiwo945you
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved