展开全部
*********************************************************************
你的代码有点乱,没太看懂,先636f70793231313335323631343130323136353331333262356231单就你提出的问题我编了个小程序,你可以看看。
一下程序实现功能:当在username(用户名)输入框中输入字符超过5个时,光标自动跳转到password(密码)输入框,类FocusTest.java的代码如下:
*********************************************************************
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FocusTest extends JFrame {
JTextField usernameText;
JTextField passwordText;
public FocusTest() {
// User name
JLabel usernameLabel = new JLabel("User name: ");
usernameText = new JTextField(20);
JPanel usernamePanel = new JPanel(new FlowLayout());
usernamePanel.add(usernameLabel);
usernamePanel.add(usernameText);
// Password
JLabel passwordLabel = new JLabel("Password: ");
passwordText = new JTextField(20);
JPanel passwordPanel = new JPanel(new FlowLayout());
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordText);
// Username and Password
JPanel inputPanel = new JPanel(new GridLayout(2, 1));
inputPanel.add(usernamePanel);
inputPanel.add(passwordPanel);
this.add(inputPanel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// usernameText addKeyListener
usernameText.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (((JTextField) (e.getSource())).getText().length() >= 4) {
passwordText.requestFocus();
}
}
});
}
public static void main(String[] args) {
new FocusTest();
}
}
*********************************************************************
运行结果如下:
*********************************************************************