当前位置: 技术问答>java相关
如何使这个JTextField获得焦点?
来源: 互联网 发布时间:2015-06-10
本文导语: 请看如下代码: JTextField jt=new JTextField(10); jtt.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent e){ } public void focusLost(FocusEvent e){ //在此处加入代码使其jt再次获取...
请看如下代码:
JTextField jt=new JTextField(10);
jtt.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e){
}
public void focusLost(FocusEvent e){
//在此处加入代码使其jt再次获取焦点请问如何处理?
}
}
);
Thanks!
JTextField jt=new JTextField(10);
jtt.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e){
}
public void focusLost(FocusEvent e){
//在此处加入代码使其jt再次获取焦点请问如何处理?
}
}
);
Thanks!
|
你要在你的程序中加入一个判断!
JTextField jt=new JTextField(10);
jt.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e){
}
public void focusLost(FocusEvent e){
if (e.isTemporary()) return;
//失去焦点时显示一个对话框
JOptionPane.showMessageDialog(null,"test");
jt.releaseFocus();
}
}
);
JTextField jt=new JTextField(10);
jt.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e){
}
public void focusLost(FocusEvent e){
if (e.isTemporary()) return;
//失去焦点时显示一个对话框
JOptionPane.showMessageDialog(null,"test");
jt.releaseFocus();
}
}
);
|
JTextField是在setBounds()时设定长宽的,和Layout无关。
|
你的意思是说不让焦点离开这个jTextField吗?
public void focusLost(FocusEvent e){
jtt.requestFocus();
}
当然应该是有条件控制的,否则这样焦点就回永远在这个jTextField上了
public void focusLost(FocusEvent e){
jtt.requestFocus();
}
当然应该是有条件控制的,否则这样焦点就回永远在这个jTextField上了