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

本文介绍了一种使用Java实现的网络速度测试方法,通过执行ping命令获取目标网站的响应信息,并将其显示在JTextPane中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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) {
				
			}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值