在java中,如何实现数据依次输出到JTextArea中?
--------------------编程问答--------------------
import javax.swing.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.BindException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.NoRouteToHostException;
import java.net.PortUnreachableException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.Date;
public class Download {
public static class Worker extends SwingWorker<Integer, Integer>{
private JTextArea ja;
public Worker(JTextArea area){
ja = area;
}
@Override
public Integer doInBackground(){
try{
for(int i = 0; i < 10; i ++){
ja.append("hello world\n");
Thread.sleep(1000);
}
}
catch(Exception e){
}
return 0;
}
}
public static void main(String [] args)
{
JFrame jFrame = new JFrame("威信下载");
jFrame.setSize(600,600);
jFrame.setLocation(100,100);
JPanel jPanel = new JPanel();
JLabel jLabel = new JLabel("测试URL:");
JLabel jLabel2 = new JLabel("测试次数:");
final JTextField jTextField = new JTextField("http://",20);
final JTextField jTextField2 = new JTextField("0",5);
JButton btnTest = new JButton("开始测试");
JButton btnClear = new JButton("清除内容");
jPanel.add(jLabel);
jPanel.add(jTextField);
jPanel.add(jLabel2);
jPanel.add(jTextField2);
jPanel.add(btnTest);
jPanel.add(btnClear);
jFrame.getContentPane().add(jPanel,"North");
final JTextArea jTextArea = new JTextArea();
jTextArea.setLineWrap(true);//激活自动换行功能
jTextArea.setWrapStyleWord(true);//激活断行不断字功能
//为JTextArea添加滚动条
JScrollPane jsp = new JScrollPane(jTextArea);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
jFrame.getContentPane().add(jsp,"Center");
//打印系统配置信息
final String line = System.getProperty("line.seperator");
jTextArea.append("------------------------配置信息-----------------------------"+"\n");
try{
jTextArea.append("本机ip地址: "+ InetAddress.getLocalHost().getHostAddress()+"\n");
}catch (Exception e) {
;
}
jTextArea.append("本机os名字: "+ System.getProperty("os.name")+"\n");
jTextArea.append("jre 版本: "+ System.getProperty("java.version")+"\n");
jTextArea.append("测试时间 :"+(new Date()).toString()+"\n");
jTextArea.append("------------------------------------------------------------------"+"\n");
//设置buttun属性
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextArea.setText("");
}
});
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String strurl = jTextField.getText();
String strnum = jTextField2.getText();
int testNum = Integer.valueOf(strnum).intValue();
}
});
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jFrame.show();
Worker task = new Worker(jTextArea);
task.execute();
}
}
补充:Java , Java SE