gpt4 book ai didi

java - 单击时的actionEvent,希望与输入键java fx相同

转载 作者:行者123 更新时间:2023-11-30 08:14:18 24 4
gpt4 key购买 nike

我是javafx新手,当鼠标单击登录按钮时我正在使用操作属性,我希望在按下回车键时使用相同的代码,是否有更有效的方法除了复制相同的代码之外,还可以执行此操作。下面是我的 javafx 登录 Controller 中的代码:

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
System.out.println("Login button selected");

/*
* All Exceptions caught at the GUI not at the other two layers.
*/
try {
/*
* Reference 'bal' to Object BusinessAccessLogin sends parameters to Business Layer
* calling method login() which passes the TextBox parameters usernameBox and
* passwordBox down the layers.
*/
if (bal.login(usernameBox, passwordBox) && (count > 0)) {

/*
* Switch statement so that: user cat 1 -> Reception screen;
* user cat 2 'Nurses' -> triage screen ; & user cat 3 'Doctors -> treatment room
*/
switch(bal.staffAccess(usernameBox, passwordBox)){
case 1:
System.out.println("Staff Category ONE");
homePageParent = FXMLLoader.load(getClass().getResource("/views/FXMLReceptionistPage.fxml"));
homePageScene = new Scene(homePageParent);
appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
break;
case 2:
System.out.println("Staff Category TWO");
homePageParent = FXMLLoader.load(getClass().getResource("/views/FXMLTriageNurseHomePage.fxml"));
homePageScene = new Scene(homePageParent);
appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
break;
case 3:
System.out.println("Staff Category THREE");
homePageParent = FXMLLoader.load(getClass().getResource("/views/FXMLDoctorAssessmentPage.fxml"));
homePageScene = new Scene(homePageParent);
appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
break;
case 4:
System.out.println("Staff Category FOUR");
homePageParent = FXMLLoader.load(getClass().getResource("/views/FXMLHospitalManagerPage.fxml"));
homePageScene = new Scene(homePageParent);
appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
break;
}

appStage.setScene(homePageScene);
appStage.show();
appStage.centerOnScreen();
appStage.setMaximized(true);

} else {
usernameBox.clear();
passwordBox.clear();
--count;
invalidLabel.setText("Sorry, invalid details");

if (count < 1) {
invalidLabel.setText("You have been locked out of system");
appStage.close();

}
attemptLabel.setText("ATTEMPTS LEFT : " + count);
System.out.println("ATTEMPTS LEFT : " + count);
}

} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}

这是我希望在按下回车键时调用的代码。任何帮助将不胜感激

最佳答案

问题有点不清楚,但我猜测你想要的是方法 setDefaultButton .

A default Button is the button that receives a keyboard VK_ENTER press, if no other node in the scene consumes it.

关于java - 单击时的actionEvent,希望与输入键java fx相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29878414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com