java中如何使文本框输入字符之后自动跳到下一文本框
2个回答
展开全部
给你写了一个 Demo,注意 addKeyListener 那段,添加键盘监听器以后用 grabFocus 可以强制转换焦点。
----------------------------------------------
import javax.swing.*;
import java.awt.event.*;
public class Demo extends JFrame {
private JTextField txtInput1, txtInput2;
public Demo () {
super("Demo Program");
setSize(100, 130);
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txtInput1 = new JTextField();
txtInput1.setBounds(10, 10, 80, 30);
txtInput1.addKeyListener(new KeyAdapter() {
public void keyTyped (KeyEvent ke) {
txtInput2.grabFocus();
}
});
txtInput2 = new JTextField();
txtInput2.setBounds(10, 50, 80, 30);
add(txtInput1);
add(txtInput2);
setVisible(true);
}
public static void main (String args[]) {
new Demo();
}
}
----------------------------------------------
import javax.swing.*;
import java.awt.event.*;
public class Demo extends JFrame {
private JTextField txtInput1, txtInput2;
public Demo () {
super("Demo Program");
setSize(100, 130);
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txtInput1 = new JTextField();
txtInput1.setBounds(10, 10, 80, 30);
txtInput1.addKeyListener(new KeyAdapter() {
public void keyTyped (KeyEvent ke) {
txtInput2.grabFocus();
}
});
txtInput2 = new JTextField();
txtInput2.setBounds(10, 50, 80, 30);
add(txtInput1);
add(txtInput2);
setVisible(true);
}
public static void main (String args[]) {
new Demo();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询