按Enter键切换JTextFields [英] switching JTextFields by pressing Enter key
本文介绍了按Enter键切换JTextFields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一些文本字段,我想知道如何通过按回车键来切换字段间的焦点。
另外,Can I控制目标字段?例如,我可以通过在字段A
中按 Enter
来定义焦点,焦点将变为字段C
?
解决方案
简单的例子:
假设你有两个 JTextField
s: textField
和 textField1
textField.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
textField1.requestFocusInWindow();
}
});
textField1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
textField.requestFocusInWindow();
}
});
现在,当您着重于 JTextField
,另外一个将被聚焦,反之亦然。
I have created number of text fields and I'm wonder how can I switch the focus between the fields by pressing the enter key.
In addition, Can I control the Target field ? for example can I defined that by pressing Enter
in field A
the focus will be change to field C
?
解决方案
Simple example:
Suppose you have two JTextField
s: textField
and textField1
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField1.requestFocusInWindow();
}
});
textField1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.requestFocusInWindow();
}
});
Now when you hit Enter
when focused on the first JTextField
, the other one will be focused, and vice versa.
这篇关于按Enter键切换JTextFields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文