jt1.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e){
String s1=new String(jt1.getText());
if(s1.length()>0&&s1.length()>8){
JOptionPane.showMessageDialog(null, "用户名由1—8个汉字,数字或英文字母组成");
jt1.setText(null);
}
}
});
正如我上面所写代码,我做了一个文本框的焦点监听,即当鼠标移开该文本框jt1的时候触发监听。问题:当用户输入不正确移开该文本框的时候,如何让它在弹出出错消息提示且文本框清空之后把鼠标的光标移回这个文本框(jt1)?求教高手,最好在我这个代码的基础上进行修改,Thank!
java
import java.awt.BorderLayout;
import java.awt.TextField;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* http://bbs.csdn.net/topics/390340915
*
* @author ceclinux
*
*/
public class MouseExitExample extends JFrame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MouseExitExample();
}
public MouseExitExample() {
setVisible(true);
setSize(300,300);
JButton jb=new JButton("dfsafs");
jb.setSize(200,200);
add(jb,BorderLayout.EAST);
final JTextField tf = new JTextField();
tf.setSize(200,200);
tf.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("a");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
add(tf);
}
}
你的意思应该是mouseExited吧,具体在里面怎么写,可以看以前发的
http://bbs.csdn.net/topics/390336436