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) {
                            }  
                     }
                }); 
            }            
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章