CoolAbu, chenyaoguo23: 谢谢 但是好象还是不可以 下面的程序是改thinking in java 里的代码, 运行时 System.out.println(line);可以输出 textArea.append(line);要等到chkdsk结束才有输出 setText也不可以呀 repaint试了也不可行//: c13:TextArea.java // From 'Thinking in Java, 2nd ed.' by Bruce Eckel // www.BruceEckel.com. See copyright notice in CopyRight.txt. // Using the JTextArea control. // <applet code=TextArea width=475 height=425> // </applet> import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; import com.bruceeckel.swing.*; import java.io.*;public class TextArea extends JApplet { JButton b = new JButton("Add Data"), c = new JButton("Clear Data"); JTextArea t = new JTextArea(20, 40); Map m = new HashMap(); Runtime rt=Runtime.getRuntime();
public void init() { // Use up all the data: b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ try { Process runjava=rt.exec("chkdsk"); //Process runjava=rt.exec("java"); InputStream ins=runjava.getInputStream(); InputStreamReader iSR = new InputStreamReader(ins); BufferedReader in = new BufferedReader(iSR); String line=null; String str=null; while ((line = in.readLine()) != null) { System.out.println(line); //t.append(line+"\n"); str=str+line+"\n"; t.setText(str); t.repaint(); } } catch(IOException mm) {mm.printStackTrace();} } }); c.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ t.setText(""); } }); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(new JScrollPane(t)); cp.add(b); cp.add(c); } public static void main(String[] args) { Console.run(new TextArea(), 475, 425); } } ///:~
我是用的append呀
进程完了回有输出
{
line.append();
--^^^^line是String型的?
System.out.println(line);//你每次在这地方打印一下看看
}
如果要实时更新GUI的内容,需要建一个线程专门做这种事的。
是呀需要专门的线程,怎么复杂可是命令行下就可以实时的啊,怎么加上一个GUI就变化这么大
JTextArea textArea=new JTextArea(40,50);
while((line=bufferedReader.readLine())!=null)
{
textArea.append(line);
System.out.println(line);
}
它就可以实时输出!
System.out.println(line);可以
textArea.append(line);不行的
repaint();//这里加一个repaint试试,GUI需要重画的。
System.out.println(line);
谢谢
但是好象还是不可以
下面的程序是改thinking in java 里的代码,
运行时
System.out.println(line);可以输出
textArea.append(line);要等到chkdsk结束才有输出
setText也不可以呀
repaint试了也不可行//: c13:TextArea.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Using the JTextArea control.
// <applet code=TextArea width=475 height=425>
// </applet>
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import com.bruceeckel.swing.*;
import java.io.*;public class TextArea extends JApplet {
JButton
b = new JButton("Add Data"),
c = new JButton("Clear Data");
JTextArea t = new JTextArea(20, 40);
Map m = new HashMap();
Runtime rt=Runtime.getRuntime();
public void init() {
// Use up all the data:
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try
{
Process runjava=rt.exec("chkdsk");
//Process runjava=rt.exec("java");
InputStream ins=runjava.getInputStream();
InputStreamReader iSR = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(iSR);
String line=null;
String str=null;
while ((line = in.readLine()) != null)
{
System.out.println(line);
//t.append(line+"\n");
str=str+line+"\n";
t.setText(str);
t.repaint();
}
}
catch(IOException mm)
{mm.printStackTrace();}
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
t.setText("");
}
});
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JScrollPane(t));
cp.add(b);
cp.add(c);
}
public static void main(String[] args) {
Console.run(new TextArea(), 475, 425);
}
} ///:~
我试过
不可以啊
是这样用吧
我在demo/jfc/swing/里面找到了
好象用javax.swing.timer可以解决