java jtextfield设置不可见_java – 如何取消聚焦JTextField

在模态对话框中最好的登录,但是引入了一些问题,方法是在组件可见后调用requestFocusInWindow(),但这被阻止了对话框的模态!

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

注意:这是用户在做任何事情之前的出现.密码字段默认为焦点.

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) {}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值