想实现一个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();
}
}
}
}
}
并在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();
}
}
}
}
}
PS:ping 192.168.1.1是肯定有输出的。
这是CSDN,不是微软的
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();
}
}
}
}
}
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();
}
}
}
}
}
我加分100了。。怎么办.
要不下次吧cursor弟兄~