@FXML
private void login(Event event) throws IOException {
String userName = emailId.getText().trim();
String password = passwordId.getText().trim();
try {
if(isValidCredentials(userName, password)){
System.out.println("Login Successfull");
Parent mainScreen = FXMLLoader.load(getClass().getResource("MainPage.fxml"));
Scene mainScene = new Scene(mainScreen);
Stage mainStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
mainStage.hide();
mainStage.setScene(mainScene);
mainStage.setTitle("Main Screen");
mainStage.show();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
通过鼠标单击登录按钮来调用此登录方法。我想要的是在登录页面上按下回车键时调用相同的方法。
最佳答案
您可以将登录按钮设置为默认按钮:这样,按下回车键时它将触发操作事件:
<Button fx:id="loginButton" text="Login" onAction="#login" defaultButton="true" />
除非使用 Enter 按键的控件具有键盘焦点,否则这将起作用,这可能正是您所需要的。例如,如果您还有文本字段,并且您希望在这些文本字段上按下 Enter 键时执行登录操作,只需为文本字段设置 onAction
处理程序:
<TextField fx:id="emailId" onAction="#login" />
关于java - 如何在javafx中为鼠标单击事件调用相同的方法并输入按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46284261/