Java Textfield焦点 [英] Java Textfield focus

查看:117
本文介绍了Java Textfield焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个焦点问题

Hello I have a problem with the focus

mytext= new JTextField();
mytext.requestFocus(true);
gc.fill =GridBagConstraints.HORIZONTAL ;
gc.gridx =3; gc.gridy=4;
gbl.setConstraints(mytext,gc);
jContentPane.add(mytext);

我试过了

I tried

mytext.requestFocus();

太远

- 选择文本字段中的文本,使文本被标记?

and how can I auto-select the text in the textfield so the text is marked?

推荐答案

Swing教程

From the Swing Tutorial


如果要确保特定组件在第一次激活窗口时获得焦点,则可以在组件实现后调用组件的requestFocusInWindow方法,但是在显示该框架之前。以下示例代码显示了如何执行此操作:

If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed. The following sample code shows how this operation can be done:



//...Where initialization occurs...
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new BorderLayout());

//...Create a variety of components here...

//Create the component that will have the initial focus.
JButton button = new JButton("I am first");
panel.add(button);
frame.getContentPane().add(panel);  //Add it to the panel

frame.pack();  //Realize the components.
//This button will have the initial focus.
button.requestFocusInWindow(); 
frame.setVisible(true); //Display the window.

这篇关于Java Textfield焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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