从JTextfields中删除焦点

时间:2014-01-28 23:39:58

标签: java swing focus jtextfield

我有JDialogJTextfields,我想在打开对话框时从第一个文本字段中删除焦点。

我试过了.removeFocus(),但后来又不再使用焦点了。我只想删除它,所以当我打开对话框时,没有选择任何文本字段。

2 个答案:

答案 0 :(得分:2)

How to use the focus subsystem我们得到以下信息:

  

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

//...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.

答案 1 :(得分:0)

在您想要焦点的控件上调用requestFocus()。例如,您可以关注对话框的默认按钮或类似的东西。来自How to Use the Focus Subsystem

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

相关问题