标签:
package swing.jtextarea; import java.util.concurrent.TimeUnit; public class Data { // 假设数据为一个自增数,不断输出数据 public void outData() { try { for (int i = 0; i < 10000; i++) { System.out.println("当前数据为:" + i); Frame.jtextarea.append("当前数据为:" + i + "\n"); // 调用主窗口的jtextarea进行添加数据,显示 // TimeUnit.SECONDS.sleep(1); } } catch (Exception ex) { ex.printStackTrace(); } } }
package swing.jtextarea; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class Frame extends JFrame { private static final long serialVersionUID = 8390788205754221692L; public static JTextArea jtextarea; public Frame() { jtextarea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(jtextarea); this.add(scrollPane, BorderLayout.CENTER); // showData();// 假设创建主窗体时,就要获取,并显示数据 } public void showData() { SwingUtilities.invokeLater(new Runnable() {// 启动数据获取,更新显示进程 public void run() { Data d = new Data(); d.outData(); } }); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Frame frame = new Frame(); frame.showData(); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setLocationRelativeTo(null); } }); } }
标签:
原文地址:http://www.cnblogs.com/softidea/p/4662041.html