学校的网络比较差,过一段时间就掉线,我玩游戏 想
写一个只要掉线就帮我自动关机的程序,也就是想捕获 二个以上的 Request timed out请大家帮我看代码,谢谢了 package com.Every;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;public class ProcessTest { private static JTextArea text;
private static JButton buttonOk, buttonQuit;
private static JPanel panel, panel1, panel2;
private static JFrame frame; public ProcessTest() { panel1 = new JPanel();
buttonOk = new JButton("开始");
buttonOk.addActionListener(new ButtonTest());
buttonQuit = new JButton("退出");
buttonQuit.addActionListener(new ButtonTest());
panel1.add(buttonOk);
panel1.add(buttonQuit); panel2 = new JPanel();
text = new JTextArea(10, 40);
panel2.add(new JScrollPane(text)); JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1,
panel2);
split.setContinuousLayout(true);
split.setOneTouchExpandable(true); panel = new JPanel();
panel.add(split, BorderLayout.CENTER); } private static class ButtonTest implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonOk) {
Timer time = new Timer();
time.schedule(new TimeMack(), 1000); } else if (e.getSource() == buttonQuit)
System.exit(0);
} static class TimeMack extends TimerTask { static BufferedReader br = null;
static BufferedWriter bw = null; String s = ""; public void run() { // TODO Auto-generated method stub
try {
String str = "ping www.baidu.com -t";
Process p = Runtime.getRuntime().exec(str); InputStream in = p.getInputStream();
br = new BufferedReader(new InputStreamReader(in)); String str2 = "Request timed out"; StringBuilder str1 = new StringBuilder();
while ((s = br.readLine()) != null) {
str1.append(s + '\n');
text.setText(str1.toString());
int num = (str1.toString().length() - str2.replaceAll(
str2, "").length())
/ str2.length();
System.out.println(num);
if (num == 6) {
Runtime run = Runtime.getRuntime();
run.exec("shutdown -s -t 3600");
}
} text.setText(str1.toString()); } catch (IOException e) { JOptionPane.showMessageDialog(null,
"Please Input aEffective String!!"); } finally {
try {
if (br != null) {
br.close();
br = null;
}
if (bw != null) {
bw.close();
bw = null;
}
} catch (IOException e) {
e.printStackTrace();
}
} } } } public static void main(String[] args) { new ProcessTest();
frame = new JFrame("Ping 127.0.0.1 -t");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
写一个只要掉线就帮我自动关机的程序,也就是想捕获 二个以上的 Request timed out请大家帮我看代码,谢谢了 package com.Every;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;public class ProcessTest { private static JTextArea text;
private static JButton buttonOk, buttonQuit;
private static JPanel panel, panel1, panel2;
private static JFrame frame; public ProcessTest() { panel1 = new JPanel();
buttonOk = new JButton("开始");
buttonOk.addActionListener(new ButtonTest());
buttonQuit = new JButton("退出");
buttonQuit.addActionListener(new ButtonTest());
panel1.add(buttonOk);
panel1.add(buttonQuit); panel2 = new JPanel();
text = new JTextArea(10, 40);
panel2.add(new JScrollPane(text)); JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1,
panel2);
split.setContinuousLayout(true);
split.setOneTouchExpandable(true); panel = new JPanel();
panel.add(split, BorderLayout.CENTER); } private static class ButtonTest implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonOk) {
Timer time = new Timer();
time.schedule(new TimeMack(), 1000); } else if (e.getSource() == buttonQuit)
System.exit(0);
} static class TimeMack extends TimerTask { static BufferedReader br = null;
static BufferedWriter bw = null; String s = ""; public void run() { // TODO Auto-generated method stub
try {
String str = "ping www.baidu.com -t";
Process p = Runtime.getRuntime().exec(str); InputStream in = p.getInputStream();
br = new BufferedReader(new InputStreamReader(in)); String str2 = "Request timed out"; StringBuilder str1 = new StringBuilder();
while ((s = br.readLine()) != null) {
str1.append(s + '\n');
text.setText(str1.toString());
int num = (str1.toString().length() - str2.replaceAll(
str2, "").length())
/ str2.length();
System.out.println(num);
if (num == 6) {
Runtime run = Runtime.getRuntime();
run.exec("shutdown -s -t 3600");
}
} text.setText(str1.toString()); } catch (IOException e) { JOptionPane.showMessageDialog(null,
"Please Input aEffective String!!"); } finally {
try {
if (br != null) {
br.close();
br = null;
}
if (bw != null) {
bw.close();
bw = null;
}
} catch (IOException e) {
e.printStackTrace();
}
} } } } public static void main(String[] args) { new ProcessTest();
frame = new JFrame("Ping 127.0.0.1 -t");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
解决方案 »
- 关于java的一个算法。
- 技术!求助!命令行javac编译失败..提示内部引用类无法标识
- 怎么样把中文转成%4e9a%%9a6c%%900a%这样的格式存储
- FrameView窗体下,Jpanel中的图像为什么随着窗体的改变而消失,如何解决?急
- Eclipse 安装什么插件能自动生成界面程序?
- 求Rational xde 2003 for java 的License.
- java 方法返回值
- 麻烦解释一下Class类的getDeclaredConstructor(Class[] parameterTypes)方法!
- 新手提问,谢谢解答!
- how can i get jdbc driver?
- JAVA 的异常处理问题!找不到原因! 后台程序
- javax.script.AbstractScriptEngine
上面这句话有异常,你的操作系统是哪个?WINDOWS2000还是WINDOWSXPCreateProcess: shutdown -s -t 3600 error=2
这句话没看懂,既然要把str2全部替换成空,为什么不直接用"",还费这么大劲