简单的java小程序之一键测网速
来源:互联网 发布:张子萱的淘宝店 编辑:程序博客网 时间:2024/05/16 19:18
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTextPane;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.DefaultStyledDocument;
- import javax.swing.text.MutableAttributeSet;
- import javax.swing.text.SimpleAttributeSet;
- import javax.swing.text.StyleConstants;
- public class Test {
- public static void main(String[] args) {
- try {
- JFrame frame = new JFrame();
- JTextPane text = new JTextPane();
- frame.getContentPane().setLayout(new BorderLayout());
- frame.getContentPane().add(new JScrollPane(text));
- frame.setTitle("网速测试");
- frame.setSize(800, 600);
- frame.setVisible(true);
- String[] cmd = new String[]{"cmd.exe","/c","ping www.baidu.com -t"};
- Process process = Runtime.getRuntime().exec( cmd);
- BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
- String info = "";
- DefaultStyledDocument doc = (DefaultStyledDocument)text.getStyledDocument();
- MutableAttributeSet attr = new SimpleAttributeSet();
- StyleConstants.setForeground(attr,new Color(0,102,0));
- while((info = br.readLine()) != null){
- if(!"".equals(info)){
- try {
- doc.insertString(doc.getLength(), info, attr);
- doc.insertString(doc.getLength(), "\r\n", null);
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
- text.setCaretPosition(doc.getLength());
- }
- }
- } catch (Exception e) {
- }
- }
- }
//主类import java.awt.BorderLayout;import java.awt.Color;import java.io.BufferedReader;import java.io.InputStreamReader;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextPane;import javax.swing.text.BadLocationException;import javax.swing.text.DefaultStyledDocument;import javax.swing.text.MutableAttributeSet;import javax.swing.text.SimpleAttributeSet;import javax.swing.text.StyleConstants;public class Test {public static void main(String[] args) {try {JFrame frame = new JFrame();JTextPane text = new JTextPane();frame.getContentPane().setLayout(new BorderLayout());frame.getContentPane().add(new JScrollPane(text));frame.setTitle("网速测试");frame.setSize(800, 600);frame.setVisible(true);String[] cmd = new String[]{"cmd.exe","/c","ping www.baidu.com -t"};Process process = Runtime.getRuntime().exec( cmd);BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));String info = "";DefaultStyledDocument doc = (DefaultStyledDocument)text.getStyledDocument();MutableAttributeSet attr = new SimpleAttributeSet();StyleConstants.setForeground(attr,new Color(0,102,0));while((info = br.readLine()) != null){if(!"".equals(info)){try {doc.insertString(doc.getLength(), info, attr);doc.insertString(doc.getLength(), "\r\n", null);} catch (BadLocationException e) {e.printStackTrace();}text.setCaretPosition(doc.getLength());}}} catch (Exception e) {}}}