简体   繁体   English

Java Textfield焦点

[英]Java Textfield focus

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();

too

and how can I auto-select the text in the textfield so the text is marked? 如何在文本字段中自动选择文本以便标记文本?

From the Swing Tutorial 来自Swing教程

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. 如果要在第一次激活窗口时确保特定组件获得焦点,则可以在实现组件之后但在显示帧之前调用组件上的requestFocusInWindow方法。 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.

As for selecting all the text you should use... 至于选择你应该使用的所有文字......

mytext.selectAll();

As for getting focus, maybe you should try the requestFocus function after everything has been added to jContentPane. 至于获得焦点,也许你应该在将所有内容添加到jContentPane后尝试requestFocus函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM