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


//主类
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) {

}

}

}


jar包的清单文件,保存为manifest.txt

Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)
Main-Class: Test //注意这里有个回车换行


编译成jar命令

jar cvfm ping.jar manifest.txt Test.class

此文主要用来记录如何自制一个Jar小程序,程序本身处理的可能并不是很恰当

第二个附件是bat脚本,可以直接测试当前网络的延时
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值