JAVA 輸入流 窗體 文本域 運行系統命令

JAVA 輸入流 窗體 按鈕 文本域 運行系統命令

/**
* @JAVA04.For.java
* @author 當前用戶:Endless作者 :Endless
* @version 創建時間:2017年7月27日 下午2:22:33
* 
*/
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

        public class Test {

            static JFrame f;  //窗體
            static JButton b; //按鈕
            public static void main(String [] args){
                f =new JFrame("按鈕");
                f.setSize(500, 335);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setResizable(true);
                f.setLocationRelativeTo(null);
                f.setVisible(true); 
                f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10)); 
                f.add(b =new JButton("Exit"));
                b.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                            System.exit(0);
                    }
                }); 
                f.add(b =new JButton("執行"));
                JTextArea text1 =new JTextArea(10,50);
                text1.setSize(100, 50);
                JTextArea  text2 =new JTextArea(1,10);
                text2.setSize(1, 30); 
                text2.setText("ipconfig");
                f.add(text2);
                f.add(text1);
                b.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                       try {
                           BufferedReader br=  
                              new BufferedReader(  //讀取輸入流到緩存空間
                                  new InputStreamReader(//讀取字節流爲字符輸入流
                                      Runtime.getRuntime().exec(//執行命令
                                          text2.getText()).getInputStream()));//獲取控制檯輸出返回的字節輸入流
                                              String tem ="";
                                              String line = null;
                                              while((line= br.readLine())!=null)    //按行從緩衝空間讀取數據
                                                   {       
                                                        tem+=line+"\n";//拼接數據
                                                   }
                                              text1.setText(tem);    //顯示結果到文本框
                            } catch (IOException e1) {
                            }  
                     }
                }); 
            }            
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章