【问题标题】:Remove Focus from JTextfields从 JTextfields 中删除焦点
【发布时间】:2014-02-20 13:23:08
【问题描述】:

我有一个JDialog 和一些JTextfields,我想在打开对话框时从第一个文本字段中移除焦点。

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

【问题讨论】:

  • 将焦点放在其他一些元素上,例如标签或其他东西...

标签: java swing focus jtextfield


【解决方案1】:

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.

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多