把Runtime.getRuntime().exec("chkdsk")的输出包装到一个BufferedReader中while((line=bufferedReader.readLine())!=null)
{
line.append();
System.out.println();
}在命令行下有实时的输出,而GUI中要等到

解决方案 »

  1.   

    你不是调用它的setText方法么??怎么赋值的呀?
      

  2.   

    sorry续上“而GUI中要等到”+“chkdsk运行结束后才有输出“ps:chkdsk是dos下检查磁盘的命令,用它是为了时间长些
      

  3.   


    我是用的append呀
    进程完了回有输出
      

  4.   

    我明白你的意思了,你是想把chkdsk的执行过程、结果放到GUI上显示,对吧?while((line=bufferedReader.readLine())!=null)
    {
      line.append();
    --^^^^line是String型的?
      System.out.println(line);//你每次在这地方打印一下看看
    }
    如果要实时更新GUI的内容,需要建一个线程专门做这种事的。
      

  5.   

    CoolAbu:
    是呀需要专门的线程,怎么复杂可是命令行下就可以实时的啊,怎么加上一个GUI就变化这么大
      

  6.   


    JTextArea textArea=new JTextArea(40,50);
    while((line=bufferedReader.readLine())!=null)
    {
    textArea.append(line);
    System.out.println(line);
    }
    它就可以实时输出!
      

  7.   

    我试过了,不可以
    System.out.println(line);可以
    textArea.append(line);不行的
      

  8.   

    textArea.append(line);
    repaint();//这里加一个repaint试试,GUI需要重画的。
    System.out.println(line);
      

  9.   

    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);
      }
    } ///:~
      

  10.   

    用textArea.append(line);
    我试过
    不可以啊
    是这样用吧
      

  11.   


    我在demo/jfc/swing/里面找到了
    好象用javax.swing.timer可以解决