单击actionEvent,想要输入密钥java fx

时间:2015-04-26 13:43:19

标签: java eclipse javafx javafx-2

我是javafx的新手,我在使用鼠标单击登录按钮时使用动作属性,我希望在按下回车键时使用相同的代码,是否有更有效的方法这样做除了复制相同的代码。下面是我的javafx登录控制器中的代码:

@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();
        }
    } 

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

1 个答案:

答案 0 :(得分:1)

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

  

默认按钮是接收键盘VK_ENTER的按钮   如果场景中没有其他节点消耗它,请按。