gpt4 book ai didi

java - 将焦点设置到输入文本字段

转载 作者:行者123 更新时间:2023-12-01 09:28:02 24 4
gpt4 key购买 nike

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

这没有什么区别。

最佳答案

This is the second card displayed.

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

查看Card Layout Focus改进的 CardLayout 将自动为您将焦点设置在第一个组件上。

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

关于java - 将焦点设置到输入文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com