将焦点设置为输入文本字段

问题描述:

我使用 netbeans 创建了以下屏幕:

I have created the following screen with netbeans:

private void initComponents()
{
    java.awt.GridBagConstraints gridBagConstraints;

    panelWest = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    textFieldScore = new javax.swing.JTextField();
    textFieldScore.setColumns(3);
    textFieldCourseRating = new javax.swing.JTextField();
    textFieldCourseRating.setColumns(4);
    textFieldCourseSlope = new javax.swing.JTextField();
    textFieldCourseSlope.setColumns(3);
    comboBoxCourse = new javax.swing.JComboBox<>();
    dateChooserAddScoresDate = new com.toedter.calendar.JDateChooser();
    chckbxAddScoresNineHoleScore = new javax.swing.JCheckBox();
    chckbxAddScoresTournamentScore = new javax.swing.JCheckBox();
    btnAddScoresAdd = new javax.swing.JButton();
    btnAddScoreDelete = new javax.swing.JButton();
    btnAddScoreDelete.setVisible(false);        // Initially turn "Delete" button off
    panelSouth = new javax.swing.JPanel();
    btnAddScoresExit = new javax.swing.JButton();

    setLayout(new java.awt.BorderLayout());

    textFieldScore.requestFocusInWindow();
    java.awt.GridBagLayout panelWestLayout = new java.awt.GridBagLayout();
    panelWestLayout.columnWidths = new int[] {0, 15, 0, 15, 0, 15, 0, 15, 0};
    panelWestLayout.rowHeights = new int[] {0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0};
    panelWest.setLayout(panelWestLayout);

    jLabel1.setText("Score");
    jLabel1.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel1, gridBagConstraints);

    jLabel2.setText("Date");
    jLabel2.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel2, gridBagConstraints);

    jLabel3.setText("Course");
    jLabel3.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel3, gridBagConstraints);

    jLabel4.setText("Course Rating");
    jLabel4.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 6;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel4, gridBagConstraints);

    jLabel5.setText("Course Slope");
    jLabel5.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 8;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel5, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(textFieldScore, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 6;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(textFieldCourseRating, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 8;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(textFieldCourseSlope, gridBagConstraints);

    comboBoxCourse.setEditable(true);
    comboBoxCourse.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            comboBoxCourseActionPerformed(evt);
        }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(comboBoxCourse, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(dateChooserAddScoresDate, gridBagConstraints);
    dateChooserAddScoresDate.setDateFormatString("MM/dd/yy");

    chckbxAddScoresNineHoleScore.setText("Nine Hole Score");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 0;
    panelWest.add(chckbxAddScoresNineHoleScore, gridBagConstraints);

    chckbxAddScoresTournamentScore.setText("Tournament Score");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 0;
    panelWest.add(chckbxAddScoresTournamentScore, gridBagConstraints);

    btnAddScoresAdd.setText("Add");
    btnAddScoresAdd.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            btnAddScoresAddActionPerformed(evt);
        }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 14;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(btnAddScoresAdd, gridBagConstraints);

    btnAddScoreDelete.setText("Delete");
    btnAddScoreDelete.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            btnAddScoreDeleteActionPerformed(evt);
        }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 16;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(btnAddScoreDelete, gridBagConstraints);

    add(panelWest, java.awt.BorderLayout.CENTER);

    btnAddScoresExit.setIcon(IconUtils.getNavigationIcon("Back", 24));
    btnAddScoresExit.setText("Back");
    btnAddScoresExit.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            btnAddScoresExitActionPerformed(evt);
        }
    });
    panelSouth.add(btnAddScoresExit);

    add(panelSouth, java.awt.BorderLayout.PAGE_END);
}

当屏幕显示时,我希望焦点位于分数字段 (textFieldScore) 中.我无法做到这一点.我试过 textFieldScore.requestFocusInWindow();和 textFieldScore.requestFocus;,但都不起作用.如何将焦点移至分数输入字段?

When the screen is displayed I want focus to be in the score field (textFieldScore). I am unable to do this. I have tried textFieldScore.requestFocusInWindow(); and textFieldScore.requestFocus;, but neither work. How can I get focus to the score input field?

***** 编辑 *****

***** EDITED *****

最初,我没有 frame.pack() 语句.我在上次 getContentPane().add(as, ADDSCORES) 之后添加了以下代码;(对于我所有的卡片.

Originally, I had no frame.pack() statement. I added the following code after my last getContentPane().add(as, ADDSCORES); (for all my cards.

this.pack();
AddScores.textFieldScore.requestFocusInWindow();
this.setVisible(true);

这没什么区别.

这是显示的第二张卡片.

This is the second card displayed.

听起来您正在使用 CardLayout.CardLayout 有一个问题,它没有将焦点放在新显示的面板上.

Sound like you are using a CardLayout. A CardLayout has a problem in that it does not place focus on the newly displayed panel.

查看卡片布局焦点以获得改进的卡片布局这将自动为您设置第一个组件的焦点.

Check out Card Layout Focus for an improved CardLayout that will set focus on the first component for you automatically.

如果您不想使用此代码,那么您需要在更改卡片并且面板现在在 GUI 上可见后将焦点设置在组件上.

If you don't want to use this code then you would need to set the focus on the component after you have changed cards and the panel is now visible on the GUI.