SwingUtilities.invokeLater(
  new Runnable() {
    public void run() {
      aJTextField.requestFocus();
      aJTextField.getCaret().setVisible(true);
    }
  }
);

解决方案 »

  1.   

    >>
    >>Another solution than this is to add a FocusListener to the JDialog, and when >>the window gains focus, transfer the focus to child component (the TextField) 
      

  2.   

    to skyyoung(路人甲):
    你太棒了!!谢谢!!!! 
      

  3.   

    注意控件是可见的就行了,然后用requestfocus().
      

  4.   

    我使用了下面的语句,光标在JTextField上,但焦点不在,需要用鼠标点击一下,才可以进行输入,是怎么回事呀?JPanel panCenter = new JPanel();
    panCenter.add(txtUserName, null);
    SwingUtilities.invokeLater(
      new Runnable() {
        public void run() {
          aJTextField.requestFocus();
          aJTextField.getCaret().setVisible(true);
        }
      }
    );