簡單的java小程式之一鍵測網速
阿新 • • 發佈:2019-02-06
//主類 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指令碼,可以直接測試當前網路的延時