专注于CardLayout内的JTextField [英] Get focus on a JTextField inside a CardLayout
问题描述
我在JPanel
A内有一个JTextField
,它是CardLayout
的一部分.当显示此A时,我想将焦点自动设置为JTextField
(即,光标在文本字段中闪烁,因此用户无需单击它即可启用输入).我尝试在初始化时在JTextField
对象上调用requestFocusInWindow()
,但这似乎不起作用.每次显示A时,我都需要调用此方法吗?谢谢.
I have a JTextField
inside a JPanel
A which is a part of CardLayout
. When this A gets shown, I want to set the focus automatically to the JTextField
(i.e. the cursor is flashing in the text field so the user doesn't need to click on it to enable the input). I tried calling requestFocusInWindow()
on the JTextField
object at initialization, but that doesn't seem to work. Do I need to call this method every time when A gets displayed? Thanks.
推荐答案
也许在显示面板时可以尝试requestFocusInWindow()
吗?
Maybe you can try requestFocusInWindow()
when the panel is shown ?
像这样吗?
jPanel.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(java.awt.event.ComponentEvent e)
{
jTextField.requestFocusInWindow();
}
});
这篇关于专注于CardLayout内的JTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!