方法有错,查API即可。下面的代码可以作为参考//: JButtonDemo.javaimport java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;public class JButtonDemo extends Applet {
  JButton  b1 = new JButton("Run");
  JTextField tb=new JTextField(10);
  JTextArea ta = new JTextArea(5,20);
  public void init() {
    ActionListener al = new ActionListener() {
      public void actionPerformed(ActionEvent e){
       command pa=new command();  
       ta.setText("");    
       pa.run(tb.getText());          
      }
    };
    b1.addActionListener(al);
    //关键语句
    b1.setToolTipText("执行命令");
    add(b1);   
    add(tb);
    add(ta);
  }
  
  class command{
    public void run(String command){
      String str="";    
      try {
        Process pp= Runtime.getRuntime().exec (command);
        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
        LineNumberReader input = new LineNumberReader (ir);
        while((str=input.readLine())!=null){
          ta.setText(ta.getText()+str+"\n\r");
        }   
      }catch (IOException ex) {}      
    } 
  }
  
  public static void main(String args[]) {
    JButtonDemo applet = new JButtonDemo();
    JFrame frame = new JFrame("TextAreaNew");
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
    frame.getContentPane().add(
      applet, BorderLayout.CENTER);
    frame.setSize(400,300);
    applet.init();
    applet.start();
    frame.setVisible(true);
  }
} ///:~