在JTextField上设置光标 [英] Set Cursor On A JTextField

查看:538
本文介绍了在JTextField上设置光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JTextField的Java中创建一个小应用程序。现在,我希望,只要我运行应用程序,光标自动放在那,使用户不必点击它,然后写入文本。我已经尝试了几乎所有我在网上找到的:setCaretPosition(0),grabFocus(),requestFocusInWindow()或requestFocus(),但他们都没有工作!我很绝望,你能帮我解决这个问题吗?非常感谢

解决方案

默认情况下,焦点将转到Window上的第一个组件。 b $ b

如果文本字段不是第一个组件,那么您可以使用:

  textField.requestFocusInWindow(); 

但是您必须在窗口可见之后调用此方法。



如果要在窗口可见之前指定具有焦点的组件,则可以使用 RequestFocusListener .wordpress.com / 2010/03/14 / dialog-focus /rel =noreferrer> Dialog Focus


I am making a small application in Java that uses a JTextField. Now, I want, as soon as I run the app, the cursor to be put automatically in that so that the user doesn't have to click on it and then write the text. I have tried pretty much everything that I found on the net: setCaretPosition(0), grabFocus(), requestFocusInWindow() or requestFocus() but none of them worked! I am desperate, can you please help me solve this? Thanks a lot

解决方案

By default focus will go to the first component on the Window.

If the text field is not the first component then you can use:

textField.requestFocusInWindow();

however you must invoke this method AFTER the window is visible.

If you want specify the component with focus before the window is visible then you can use the RequestFocusListener approach from Dialog Focus.

这篇关于在JTextField上设置光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆