2013-03-08 28 views
0

我有以下簡單的例子;try和catch語句裏面的代碼不能執行,怎麼了?

public class TES extends JFrame { 

private JPanel contentPane; 
private JTextField textField; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       TES frame = new TES(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public TES() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 100, 100); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 

    textField = new JTextField(); 
    textField.setColumns(10); 

    JButton btnNewButton = new JButton("New button"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 


      try { 
       System.out.println("Before "); 
       textField.setText("Before"); 
       Thread.sleep(4000); 
      } catch (InterruptedException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      textField.setText("After"); 
     } 
    }); 



    GroupLayout gl_contentPane = new GroupLayout(contentPane); 
    gl_contentPane.setHorizontalGroup(
     gl_contentPane.createParallelGroup(Alignment.TRAILING) 
      .addGroup(gl_contentPane.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(btnNewButton) 
       .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
      .addGroup(gl_contentPane.createSequentialGroup() 
       .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 
       .addContainerGap()) 
    ); 
    gl_contentPane.setVerticalGroup(
     gl_contentPane.createParallelGroup(Alignment.LEADING) 
      .addGroup(gl_contentPane.createSequentialGroup() 
       .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(ComponentPlacement.RELATED, 61, Short.MAX_VALUE) 
       .addComponent(btnNewButton) 
       .addContainerGap()) 
    ); 
    contentPane.setLayout(gl_contentPane); 


} 

}

的textField.settext( 「前」)不是正在執行線。

  try { 
       System.out.println("Before "); 
       textField.setText("Before"); 
       Thread.sleep(4000); 
      } catch (InterruptedException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      textField.setText("After"); 
     } 

雖然System.out.println(「之前」)行被執行。我放入thread.sleep(4000)只是爲了讓我可以看到JtextField中的「Before」和「After」文本。在我原來的代碼中,我正在那個地方開辦另一堂課;

        try { 
       System.out.println("Before "); 
       textField.setText("Before"); 
       Functions.dbSet(); 
      } catch (InterruptedException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      textField.setText("After"); 
     } 

所以最主要的問題是,textField.setText(「Before」);行不會執行。

回答