我正在尝试创建一个简单的登录系统,我希望用户能够在他们点击输入按钮时继续下一部分,并且我设法让它在注册部分工作,这样当用户已完成输入他们的详细信息,他们可以点击进入,程序进入下一部分,但我想要它,以便当用户输入他们的登录详细信息时,他们可以按Enter键,然后程序将执行代码的下一部分。 / p>
card3 = new JPanel();
usernameFieldone = new JTextField(10);
usernameLabel = new JLabel("Username:");
usernameLabel.setLabelFor(usernameFieldone);
passwordFieldone = new JPasswordField(10);
passwordLabel = new JLabel("Password:");
passwordLabel.setLabelFor(passwordFieldone);
passwordFieldre = new JPasswordField(10);
passwordLabelre = new JLabel("Confirm Password: ");
passwordLabelre.setLabelFor(passwordFieldre);
passwordFieldre.addKeyListener(this);
OKButton = new JButton("OK");
OKButton.addActionListener(this);
backButton = new JButton("Back");
backButton.addActionListener(this);
public void keyReleased(KeyEvent e){
CardLayout cl = (CardLayout)(cards.getLayout());
if(e.getKeyCode()==KeyEvent.VK_ENTER){
if(
Register();
}
}`
答案 0 :(得分:1)
这是一个例子:
(...)
//register a Key listener event
myComponent.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
enter_Pressed(evt);
}
});
(...)
private void enter_Pressed(java.awt.event.KeyEvent evt){
//if ENTER is pressed
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
//do someting
}
}