JTextArea 动态显示信息

 这几天刚刚学习Swing,写了个JTextArea动态显示信息的例子,,,,拿出来晒晒,大虾们见笑了....

 

主要思路:

           窗体类实现Runnable接口,实现run方法

 

 

 

package com.aiy.test;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Test{
	public static void main(String[] args) {
		Window app=new Window();
		app.setMillis(2000L);
		new Thread(app).start();
	}

}
class Window extends JFrame implements Runnable{
	private Long millis=1000L;

	public Long getMillis() {
		return millis;
	}

	public void setMillis(Long millis) {
		if(millis<0)
			millis=1000L;
		this.millis = millis;
	}
	private JTextArea textArea=new JTextArea();

	public JTextArea getTextArea() {
		return textArea;
	}
	public Window(){
		setVisible(true);
		setTitle("Test Window Aiy");
		setLayout(new BorderLayout());
		setSize(new Dimension(600,600));
		textArea.setText("Please Wait...");
		JScrollPane sp=new JScrollPane(textArea);
		sp.setPreferredSize(new Dimension(600,600));
		add(sp);
	}
	@Override
	public void run() {
		try {
			Thread.sleep(millis);
			textArea.setText("Starting...\n");
			for (int i = 1; i < 11; i++) {
				textArea.append("setp:"+i+"\n");
				Thread.sleep(millis);
			}
			textArea.append("Finished");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}

 

你可能感兴趣的:(java)