【问题标题】:How to UnFocus a JTextField如何取消聚焦 JTextField
【发布时间】:2023-03-31 15:29:01
【问题描述】:

当我的应用程序加载时,它是使用 netbeans 制作的,第一个 JTextField 会自动聚焦,并且在这个 JTextField 中,我写了“输入您的用户名”,当用户单击该字段时它会消失,但是当应用程序加载完毕,这个字段被聚焦,意味着我看不到“输入您的用户名”,如何在启动时取消聚焦?

【问题讨论】:

  • 有 2 个 JTextFields,第一个用于用户名,第二个用于密码,还有一个名为 Log in 的 JMenuItem,在我运行应用程序的那一刻,第一个 JTextField 是焦点,我不希望这样
  • BTW - 如果每个应用程序的用户名。 预计安装是不变的,只需使用默认值填充用户名字段,如果正确,让用户选项卡到下一个字段。如果 保证 保持不变,则填充该字段并将其禁用。它不会聚焦,默认情况下焦点将转到第二个字段。

标签: java swing netbeans focus jtextfield


【解决方案1】:

使用requestFocusInWindow() 将焦点设置在其他组件上,而不是首先使用JTextfield

但我建议不要改变本机焦点系统,而是在initComponents() 调用constructor 之后的JTextField 上的setText(String s)(假设在netbeans 中)。

更多可选阅读:How to Use the Focus Subsystem

【讨论】:

  • 如果我必须点击一个 JMenuItem 来登录怎么办?我在另一个元素上使用了 requestFocusInWindow(),但是当我点击那个 JMenuItem 时,JTextField 被聚焦,我第二次点击那个 JMenuItem 时,JTextField 没有聚焦,它稍后工作,但第一次没有
  • 您是否在 NetBeans 属性编辑器中为您的 JMenuItem 尝试 nextFocusableComponent 属性?
  • 我修复了它,我在启动时使用了你的代码两次,当我点击 Jmenuitem 时,谢谢
【解决方案2】:

登录最好在模态对话框中完成,但这会带来问题,因为必须在组件可见之后调用requestFocusInWindow() 方法,但这被事实阻止了对话框是模态的!

此示例使用 Rob Camick 的 RequestFocusListener(如 Dialog Focus 中所述)在对话框可见后管理焦点。

注意:这是在用户执行任何操作之前它的显示方式。默认情况下,密码字段是焦点。

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class LoginRequired {

    LoginRequired() {
        JFrame f = new JFrame("Login Required");
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        f.setResizable(false);
        f.setSize(400, 300); // not recommended, but used here for convenience
        f.setLocationByPlatform(true);
        f.setVisible(true);

        showLogin(f);
    }

    private void showLogin(JFrame frame) {
        JPanel p = new JPanel(new BorderLayout(5,5));

        JPanel labels = new JPanel(new GridLayout(0,1,2,2));
        labels.add(new JLabel("User Name", SwingConstants.TRAILING));
        labels.add(new JLabel("Password", SwingConstants.TRAILING));
        p.add(labels, BorderLayout.LINE_START);

        JPanel controls = new JPanel(new GridLayout(0,1,2,2));
        JTextField username = new JTextField("Joe Blogs");
        controls.add(username);
        JPasswordField password = new JPasswordField();
        password.addAncestorListener(new RequestFocusListener(false));
        controls.add(password);
        p.add(controls, BorderLayout.CENTER);

        JOptionPane.showMessageDialog(
            frame, p, "Log In", JOptionPane.QUESTION_MESSAGE);
        System.out.println("User Name: " + username.getText());
        System.out.println("Password: " + new String(password.getPassword()));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LoginRequired();
        });
    }
}

/**
 *  Convenience class to request focus on a component.
 *
 *  When the component is added to a realized Window then component will
 *  request focus immediately, since the ancestorAdded event is fired
 *  immediately.
 *
 *  When the component is added to a non realized Window, then the focus
 *  request will be made once the window is realized, since the
 *  ancestorAdded event will not be fired until then.
 *
 *  Using the default constructor will cause the listener to be removed
 *  from the component once the AncestorEvent is generated. A second constructor
 *  allows you to specify a boolean value of false to prevent the
 *  AncestorListener from being removed when the event is generated. This will
 *  allow you to reuse the listener each time the event is generated.
 */
class RequestFocusListener implements AncestorListener
{
    private boolean removeListener;

    /*
     *  Convenience constructor. The listener is only used once and then it is
     *  removed from the component.
     */
    public RequestFocusListener()
    {
        this(true);
    }

    /*
     *  Constructor that controls whether this listen can be used once or
     *  multiple times.
     *
     *  @param removeListener when true this listener is only invoked once
     *                        otherwise it can be invoked multiple times.
     */
    public RequestFocusListener(boolean removeListener)
    {
        this.removeListener = removeListener;
    }

    @Override
    public void ancestorAdded(AncestorEvent e)
    {
        JComponent component = e.getComponent();
        component.requestFocusInWindow();

        if (removeListener)
            component.removeAncestorListener( this );
    }

    @Override
    public void ancestorMoved(AncestorEvent e) {}

    @Override
    public void ancestorRemoved(AncestorEvent e) {}
}

【讨论】:

  • @AloneInTheDark 这是一个单独的问题,应该在单独的问题上提出。
【解决方案3】:

我认为将键盘焦点放在用户名字段是正确的行为,假设这是用户首先需要做的事情。与其清除焦点,不如在用户键入内容后才清除?:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.Document;

public class PlaceholderTextField extends JTextField {

    public static void main(final String[] args) {
        final PlaceholderTextField tf = new PlaceholderTextField("");
        tf.setColumns(20);
        tf.setPlaceholder("All your base are belong to us!");
        final Font f = tf.getFont();
        tf.setFont(new Font(f.getName(), f.getStyle(), 30));
        JOptionPane.showMessageDialog(null, tf);
    }

    private String placeholder;

    public PlaceholderTextField() {
    }

    public PlaceholderTextField(
        final Document pDoc,
        final String pText,
        final int pColumns)
    {
        super(pDoc, pText, pColumns);
    }

    public PlaceholderTextField(final int pColumns) {
        super(pColumns);
    }

    public PlaceholderTextField(final String pText) {
        super(pText);
    }

    public PlaceholderTextField(final String pText, final int pColumns) {
        super(pText, pColumns);
    }

    public String getPlaceholder() {
        return placeholder;
    }

    @Override
    protected void paintComponent(final Graphics pG) {
        super.paintComponent(pG);

        if (placeholder.length() == 0 || getText().length() > 0) {
            return;
        }

        final Graphics2D g = (Graphics2D) pG;
        g.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(getDisabledTextColor());
        g.drawString(placeholder, getInsets().left, pG.getFontMetrics()
            .getMaxAscent() + getInsets().top);
    }

    public void setPlaceholder(final String s) {
        placeholder = s;
    }

}

如果你真的只是想移除焦点,一些选项:

  • component.setFocusable(false);
  • KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
  • KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();

【讨论】:

    【解决方案4】:
    textField.setFocusable(false);
    textField.setFocusable(true);
    

    当且仅当 textField 具有焦点时,TAB 顺序中的下一个组件将自动获得焦点。效果与 TAB 按下相同。

    (未在只有一个可聚焦组件的 GUI 中进行测试:))

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 1970-01-01
      • 2021-11-16
      • 2013-06-13
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 2013-12-25
      • 2014-12-13
      相关资源
      最近更新 更多