按回车属于文本框提交事件,监听后聚焦到下一个输入框,就完成了切换
最大字符数后切换,即监听Text长度在聚焦切换
特别写了个回车互相切换的applet例子,
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test extends JApplet {
private static final long serialVersionUID=1802199483736051124L;
public JTextField input_field,result_field;
public JLabel input_label,result_label;
@Override
public void init(){
setSize(400, 200);
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
input_label=new JLabel("表达式输入");
add(input_label);
input_field=new JTextField("",10);
input_field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
result_field.setText(input_field.getText());
result_field.grabFocus();
}
});
add(input_field);
result_label=new JLabel("计算结果");
add(result_label);
result_field=new JTextField("",10);
result_field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input_field.setText(result_field.getText());
input_field.grabFocus();
}
});
add(result_field);
}
}
将控件组成一个列表。侦听按钮事件。
当输入到固定字符,或按回车,使下一个组件,获得焦点。
但这个方法不是很好。
最好的办法是满足上述条件时,让它执行按tab的动作,自动的去找该获得焦点的控件。
这个我再看看。
找到方法了。
可以通过容器的FocusTraversalPolicy 获得当前对象的下一个该获得焦点的对象。
看下边的代码。当输入回车或输入满5个字符就会使下一个控件获得焦点。
如果要自定义这个顺序。需要重写FocusTraversalPolicy 类
这个例子只侦听了按钮释放的事件,所以输入过快,焦点有可能不移动。
------------------------------------------------------------------------------------------------
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FocusTraversalPolicy;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Demo extends JFrame {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
public Demo() {
getContentPane().setLayout(null);
textField = new JTextField();
FocusTraversalPolicy policy = getFocusTraversalPolicy();
textField.addKeyListener(new nextKeyEvent(this, textField, policy));
textField.setBounds(12, 10, 96, 19);
getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(12, 68, 96, 19);
textField_1.addKeyListener(new nextKeyEvent(this, textField_1, policy));
getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(12, 39, 96, 19);
textField_2.addKeyListener(new nextKeyEvent(this, textField_2, policy));
getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(12, 94, 96, 19);
textField_3.addKeyListener(new nextKeyEvent(this, textField_3, policy));
getContentPane().add(textField_3);
textField_3.setColumns(10);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - getWidth()) / 2,
(screenSize.height - getHeight()) / 2);
setResizable(false);
setVisible(true);
}
class nextKeyEvent extends KeyAdapter {
private Container comp = null;
private JTextField text = null;
private FocusTraversalPolicy policy = null;
public nextKeyEvent(Container comp, JTextField text,
FocusTraversalPolicy policy) {
this.comp = comp;
this.text = text;
this.policy = policy;
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
nextFocus();
}
if (text.getText().length() == 5) {
nextFocus();
}
}
private void nextFocus() {
JTextField next = (JTextField) policy.getComponentAfter(comp, text);
next.grabFocus();
}
}
public static void main(String[] args) {
new Demo();
}
}