简单的java小程序之一键测网速

Java代码 复制代码 收藏代码
  1. //主类
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. import javax.swing.JFrame;
  7. import javax.swing.JScrollPane;
  8. import javax.swing.JTextPane;
  9. import javax.swing.text.BadLocationException;
  10. import javax.swing.text.DefaultStyledDocument;
  11. import javax.swing.text.MutableAttributeSet;
  12. import javax.swing.text.SimpleAttributeSet;
  13. import javax.swing.text.StyleConstants;
  14. public class Test {
  15. public static void main(String[] args) {
  16. try {
  17. JFrame frame = new JFrame();
  18. JTextPane text = new JTextPane();
  19. frame.getContentPane().setLayout(new BorderLayout());
  20. frame.getContentPane().add(new JScrollPane(text));
  21. frame.setTitle("网速测试");
  22. frame.setSize(800, 600);
  23. frame.setVisible(true);
  24. String[] cmd = new String[]{"cmd.exe","/c","ping www.baidu.com -t"};
  25. Process process = Runtime.getRuntime().exec( cmd);
  26. BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
  27. String info = "";
  28. DefaultStyledDocument doc = (DefaultStyledDocument)text.getStyledDocument();
  29. MutableAttributeSet attr = new SimpleAttributeSet();
  30. StyleConstants.setForeground(attr,new Color(0,102,0));
  31. while((info = br.readLine()) != null){
  32. if(!"".equals(info)){
  33. try {
  34. doc.insertString(doc.getLength(), info, attr);
  35. doc.insertString(doc.getLength(), "\r\n", null);
  36. } catch (BadLocationException e) {
  37. e.printStackTrace();
  38. }
  39. text.setCaretPosition(doc.getLength());
  40. }
  41. }
  42. } catch (Exception e) {
  43. }
  44. }
  45. }
//主类
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) {
				
			}

	}

}

你可能感兴趣的:(简单的java小程序之一键测网速)