代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestCase extends JFrame {
  private JPanel contentPane;
  private BorderLayout borderLayout1 = new BorderLayout();
  private JPanel jPanel1 = new JPanel();
  private JPanel jPanel2 = new JPanel();
  private JPanel jPanel3 = new JPanel();
  private JLabel jLabel1 = new JLabel();
  private JTextField url = new JTextField(30);
  private JLabel jLabel2 = new JLabel();
  private JTextField times = new JTextField(10);
  private JButton jButton1 = new JButton();  //Construct the frame
  public TestCase() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(TestCase.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    //this.setSize(new Dimension(400,200));
    this.setSize(400,200);
    this.setTitle("TestCase");
    this.setVisible(true);
    //this.show();
    jLabel1.setText("HTTP:");
    jLabel2.setText("Times:");
    jButton1.setText("Execute");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    times.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusLost(FocusEvent e) {
        times_focusLost(e);
      }
    });
    contentPane.add(jPanel1, BorderLayout.NORTH);
    jPanel1.add(jLabel1, null);
    jPanel1.add(url, null);
    contentPane.add(jPanel2, BorderLayout.CENTER);
    jPanel2.add(jLabel2, null);
    jPanel2.add(times, null);
    contentPane.add(jPanel3,  BorderLayout.SOUTH);
    jPanel3.add(jButton1, null);
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
  public static void main(String args[]){
    new TestCase();
  }  void jButton1_actionPerformed(ActionEvent e) {
    String http_url=null;
    int count;
    http_url = url.getText();
    if (http_url.equals("")){
      JOptionPane.showMessageDialog(this,"Please enter URL address!","Infromation",0);
      return;
    }
    if (!times.getText().equals("")) {
      count = Integer.parseInt(times.getText());
    }
    else{
      JOptionPane.showMessageDialog(this,"Please enter Times!","Infromation",0);
      return ;
    }
    String ls_command=null;
    ls_command="C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE " + http_url;
    try {
      for (int i=0;i<count;i++){
        Runtime.getRuntime().exec(ls_command);
      }
    }
    catch (Exception ex){ex.printStackTrace();}  }  void times_focusLost(FocusEvent e) {
    try{
      Integer.parseInt(times.getText());
    }
    catch (NumberFormatException exx){    }
  }
}

解决方案 »

  1.   

    //this.show();
    怎么show被注释掉了?
      

  2.   

    ........
        contentPane.add(jPanel3,  BorderLayout.SOUTH);
        jPanel3.add(jButton1, null);
        
        this.show();
        .......
      

  3.   

    呵呵,你的组件是在显示之后再加入的,当然要刷新之后才能显示,我把你的程序试了一下,就是这个问题。修改如下:private void jbInit() throws Exception  {
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(borderLayout1);
        
        jLabel1.setText("HTTP:");
        jLabel2.setText("Times:");
        jButton1.setText("Execute");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButton1_actionPerformed(e);
          }
        });
        times.addFocusListener(new java.awt.event.FocusAdapter() {
          public void focusLost(FocusEvent e) {
            times_focusLost(e);
          }
        });
        contentPane.add(jPanel1, BorderLayout.NORTH);
        jPanel1.add(jLabel1, null);
        jPanel1.add(url, null);
        contentPane.add(jPanel2, BorderLayout.CENTER);
        jPanel2.add(jLabel2, null);
        jPanel2.add(times, null);
        contentPane.add(jPanel3,  BorderLayout.SOUTH);
        jPanel3.add(jButton1, null);
        
        this.setSize(400,200);
        this.setTitle("TestCase");
        this.setVisible(true);
        this.show();
        
      }
      

  4.   

    用setVisible()不就可以显示了吗?为什么要用一个this.show()?