当前位置: 代码迷 >> Java相关 >> 如何判断JTextField失去焦点
  详细解决方案

如何判断JTextField失去焦点

热度:404   发布时间:2007-01-02 11:06:44.0
如何判断JTextField失去焦点
focus
方法里面怎么才能判断文本输入框失去焦点?
搜索更多相关的解决方案: JTextField  焦点  判断  focus  文本  

----------------解决方案--------------------------------------------------------

FocusListener


----------------解决方案--------------------------------------------------------

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

public class Demo extends JFrame implements FocusListener{
private JTextField jtfDemo1,jtfDemo2;

public Demo(){
jtfDemo1=new JTextField();

jtfDemo2=new JTextField();
jtfDemo2.addFocusListener(this);
getContentPane().add(jtfDemo1,BorderLayout.NORTH);
getContentPane().add(jtfDemo2,BorderLayout.SOUTH);

setSize(100,100);
setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void focusGained(FocusEvent e){

}

public void focusLost(FocusEvent e){
System.out.println("失去焦点");
}

public static void main(String[] args){
new Demo();
}

}


----------------解决方案--------------------------------------------------------

谢谢版主
我想用内部类实现这样把我把我的代码发上来你帮我看下好吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class UserEnter extends JFrame
{
JLabel clewLabel = new JLabel("请输入用户名和密码");
JLabel nameLabel = new JLabel("姓名:");
JLabel pwdLabel = new JLabel("密码:");

JTextField nameText = new JTextField();
JTextField pwdText = new JTextField();

JButton okButton = new JButton("确定");
JButton resetButton = new JButton("重置");


public UserEnter()
{
Container vessel = this.getContentPane();
vessel.setLayout(null);

clewLabel.setBounds(80,20,120,25);
nameLabel.setBounds(30,65,60,25);
pwdLabel.setBounds(30,105,60,25);
nameText.setBounds(65,65,190,25);
pwdText.setBounds(65,105,190,25);
okButton.setBounds(30,160,70,25);
resetButton.setBounds(180,160,70,25);

vessel.add(clewLabel);
vessel.add(nameLabel);
vessel.add(pwdLabel);
vessel.add(nameText);
vessel.add(pwdText);
vessel.add(okButton);
vessel.add(resetButton);

okButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)
{
e.getActionCommand();
System.out.println(e.getActionCommand());
new JPanel(); //当我点击这个按钮的时候怎么才能弹出一个新的面版
System.exit(0); //dispose在这怎么用?

}
});

resetButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)
{
nameText.setText("");
pwdText.setText("");

}
});
//
// nameText.addFocusListener( new FocusListener(){ //当文本框失去焦点的时候我要调用Text文档做比较
// public void focusLost(FocusEvent e)
// {
//
// }
//
// });



this.setSize(300,230);
this.setVisible(true);
this.setTitle("用户登陆");

}


public static void main(String [] args)
{
new UserEnter();

}
}

[此贴子已经被作者于2007-1-2 13:54:41编辑过]


----------------解决方案--------------------------------------------------------
你想弹出一个新的面板,可以使用
javax.swing.JOptionPane来实现,你把它放到你的事件处理函数里面就可以了

它提供了很多静态方法供你使用,具体请查看API
----------------解决方案--------------------------------------------------------