简单的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脚本,可以直接测试当前网络的延时

你可能感兴趣的:(java)