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

时间:2016-09-24 20:32:13

标签: java swing textfield

我用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);
}

That looks as following:

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

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

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

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

这没什么区别。

2 个答案:

答案 0 :(得分:1)

  

这是显示的第二张卡片。

听起来就像是在使用CardLayout。 CardLayout存在一个问题,即它不会将焦点放在新显示的面板上。

查看Card Layout Focus以获得改进的CardLayout,它会自动将焦点放在第一个组件上。

如果您不想使用此代码,则需要在更换卡片后将焦点设置在组件上,并且现在可以在GUI上看到面板。

答案 1 :(得分:0)

告诉您是否要放置所有代码会更容易。基本上,您需要确保在显示帧之前请求焦点。因此,如果您要在框架中初始化这些组件,那么它应该是

frame.pack();  
textFieldScore.requestFocusInWindow(); 
frame.setVisible(true);