如何取消焦点JTextField [英] How to UnFocus a JTextField

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

问题描述

当我的应用程序加载,这是使用netbeans,第一个JTextField自动聚焦,并在这个JTextField,我写了输入您的用户名,它会消失,当用户点击这个领域,但是当应用程序被加载,这个领域的重点,意味着我不能看到输入您的用户名,如何在启动时不重视它?

解决方案

登录最好在模态对话框中完成,但是会引入一些问题,因为 requestFocusInWindow()方法必须在 >组件是可见的,但是由于对话框是模态的而被阻塞了!

这个例子使用Rob Camick的 RequestFocusListener (如



注意:在用户执行任何操作之前就是这样的。密码字段默认为重点。

  package test.t100.t001; 

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
导入javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class LoginRequired {

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

f.setSize(400,300);
f.setResizable(false);
f.setLocationByPlatform(true);
f.setVisible(true);

showLogin(f);

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

JPanel标签= new JPanel(new GridLayout(0,1,2,2));
labels.add(新的JLabel(用户名,SwingConstants.RIGHT));
labels.add(new JLabel(Password,SwingConstants.RIGHT));
p.add(labels,BorderLayout.WEST);

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

// LayoutManager l = new GroupLayout(p);
//p.setLayout(l);
JOptionPane.showMessageDialog(
frame,p,Log In,JOptionPane.QUESTION_MESSAGE);

$ b $ **
* @param args none
* /
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
New LoginRequired();
}
});
}

}

/ **
*方便课程要求重点关注一个组件。
*
*当组件被添加到已实现的窗口时,组件将立即
*请求焦点,因为ancestorAdded事件立即被触发
*。
*
*当组件被添加到一个未实现的窗口中时,一旦窗口被实现,焦点
*请求将被创建,因为
* ancestorAdded事件不会直到那时才被解雇。
*
*一旦生成AncestorEvent,使用默认构造函数将导致从组件中移除
*的侦听器。第二个构造函数
*允许您指定一个布尔值false,以防止在生成事件时移除
* AncestorListener。这将
*允许您在每次生成事件时重用侦听器。
* /
class RequestFocusListener实现AncestorListener
{
private boolean removeListener;

/ *
*方便的构造函数。监听器只使用一次,然后从组件中删除
*。
* /
public RequestFocusListener()
{
this(true);
}

/ *
*构造函数控制这个监听是否可以被使用一次或
*多次。
*
* @param removeListener当为true时,这个侦听器只被调用一次
*否则可以多次调用。
* /
public RequestFocusListener(boolean removeListener)
{
this.removeListener = removeListener;
}

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

if(removeListener)
component.removeAncestorListener(this);
b
$ b @Override
public void ancestorMoved(AncestorEvent e){}
$ b $ @Override
public void ancestorRemoved(AncestorEvent e){ }
}


When my application loads, which is made using netbeans, the first JTextField is automatically focused, and in this JTextField, I have written "Enter your Username", it will go away when the user clicks on this field, but when the application is loaded, this field is focused, means I can't see "Enter your Username", how to unfocus it on startup ?

解决方案

A log-in would be best done in a modal dialog, but that introduces problems in that the method requestFocusInWindow() must be called after the component is visible, but that is blocked by the fact the dialog is modal!

This example uses Rob Camick's RequestFocusListener (as presented in Dialog Focus) to manage focus after the dialog is visible.

Note: That is how it appears before the user does anything. The password field is focused by default.

package test.t100.t001;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class LoginRequired {

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

        f.setSize(400, 300);
        f.setResizable(false);
        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.RIGHT));
        labels.add(new JLabel("Password", SwingConstants.RIGHT));
        p.add(labels, BorderLayout.WEST);

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

        //LayoutManager l = new GroupLayout(p);
        //p.setLayout(l);
        JOptionPane.showMessageDialog(
            frame, p, "Log In", JOptionPane.QUESTION_MESSAGE);
    }

    /**
     * @param args  none
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                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) {}
}

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

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