想实现一个dos命令,死循环,每隔一个线程的睡眠时间后再次执行命令。
并在testarea框中打印输出执行结果,小弟刚学j2se,也刚到msdn,希望弟兄们多多帮忙,帮找下错误,和原因,多谢!代码:import java.io.*;import javax.swing.*; 
import java.awt.event.*;
public class TimeCli extends JFrame {
JTextArea t = new JTextArea(20, 60);
JPanel p = new JPanel();
public TimeCli() {
add(p);
p.add(t);
setSize(600, 400);
setLocation(320, 140);
setVisible(true);
pack(); } public static void main(String[] args) {
new TimeCli(); } class BListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
while (true) {
String command = "ping 192.168.1.1";
try {
Process pp = Runtime.getRuntime().exec(command);
DataInputStream in;
in = new DataInputStream(pp.getInputStream());
//System.out.println("test");
while ((in.readUTF()) != null) {
String message = null;
t.append("" + message);
} in.close();
try {
pp.waitFor();
} catch (InterruptedException e2) {
e2.printStackTrace();
}
} catch (IOException e3) {
e3.printStackTrace();
} catch (NumberFormatException e1) { e1.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e4) {
e4.printStackTrace();
}
}
}
}
}

解决方案 »

  1.   

    JScrollPane   p1   =   new   JScrollPane(); 
      

  2.   

    改了这个滚动的,还是无法输出
    PS:ping 192.168.1.1是肯定有输出的。
      

  3.   

    MSDN???
     这是CSDN,不是微软的
      

  4.   

    我试的时候说报错,in.readUTF()说这一句找不到源
      

  5.   

    帮你改好了,不知道是不是这样的.反正textarea能显示东西了.如果是刚学,建议从简单的程序开始写,不要太复杂的.不可能一顿饭吃成胖子.package demo;import java.io.*;import javax.swing.*;
    import java.awt.event.*;public class TimeCli extends JFrame {
    JTextArea t = new JTextArea(20, 60);
    JScrollPane       p1       =       new       JScrollPane();
    JPanel p = new JPanel();
    public TimeCli() {
    p1.getViewport().add(t);
    p.add(p1);
    add(p);
    setSize(600, 400);
    setLocation(320, 140);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    new BListener().run(); } public static void main(String[] args) {
    new TimeCli(); } class BListener implements Runnable {
    public void run() {
    while(true)
    {
    String command = "ping 192.168.1.1";
    try
    {
    Process pp = Runtime.getRuntime().exec(command);
    DataInputStream in= new DataInputStream(pp.getInputStream());
    String s;
    while ((s=in.readLine())!=null) {
    System.out.println(s);
    t.append(s+"\n");
    }
    in.close();
    Thread.currentThread().sleep(1000);
    }
    catch (InterruptedException e)
    {
    throw new RuntimeException(e);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
      

  6.   

    多谢楼上的几位弟兄!程序搞定了。
    cursor兄多谢!看来基础部分还是很多没有理解啊,谢谢你修改的代码。我再次经过修改。完成。如下import java.io.*;import javax.swing.*;
    import java.awt.event.*;public class TimeCli extends JFrame {
    JTextArea t = new JTextArea(20, 60);
    JScrollPane p1 = new JScrollPane();
    JPanel p = new JPanel(); public TimeCli() {
    p1.getViewport().add(t);
    p.add(p1);
    add(p);
    setTitle(" 时间同步程序 Version 0.2 By SDJ ");
    setSize(600, 400);
    setLocation(320, 140);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    new BListener().run(); } public static void main(String[] args) {
    new TimeCli(); } class BListener implements Runnable {
    public void run() {
    while (true) {
    String command = "--dos命令--";
    try {
    Process pp = Runtime.getRuntime().exec(command);
    InputStream is = pp.getInputStream();
    InputStreamReader bi = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(bi);
    String s;
    s = br.readLine();
    // System.out.println(s);
    while (s != null && !"".equals(s)) {
    // System.out.println(s);
    t.append(s + "\n");
    s = br.readLine();
    // System.out.println(s); }
    t.append(" test" + "\n");
    t.append(" test " + "\n");
    is.close();
    Thread.currentThread().sleep(3600000);
    } catch (InterruptedException e) {
    throw new RuntimeException(e);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
      

  7.   

    我晕倒
    我加分100了。。怎么办.
    要不下次吧cursor弟兄~