TA的每日心情 | 开心 2021-3-12 23:18 |
|---|
签到天数: 2 天 [LV.1]初来乍到
|
|
- 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(), "
- ", null);
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
- text.setCaretPosition(doc.getLength());
- }
- }
-
- } catch (Exception e) {
-
- }
-
- }
-
- }
复制代码 jar包的清单文件,保存为manifest.txt
Manifest-Version: 1.0
Created-By: 1.7.0_04-b22 (Sun Microsystems Inc.)
Main-Class: Test//注意这里有个回车换行
编译成jar文件命令
jar cvfm ping.jar manifest.txt Test.class
好了,双击ping.jar看看。
源码下载:http://file.javaxxz.com/2014/11/3/000155828.zip |
|