按下javafx按钮时不执行
我的问题是,当我点击我的登录按钮时,它什么也不做。没有抛出异常,try-catch似乎没有捕获任何错误/异常。一直在寻找线索,但没有发现任何有效的 我在代码中添加了print语句,它似乎在第一个try块的开头就停止了,并且在两个catch块中都添加了stackTrace,而没有添加任何内容按下javafx按钮时不执行,java,javafx,netbeans,javafx-8,Java,Javafx,Netbeans,Javafx 8,我的问题是,当我点击我的登录按钮时,它什么也不做。没有抛出异常,try-catch似乎没有捕获任何错误/异常。一直在寻找线索,但没有发现任何有效的 我在代码中添加了print语句,它似乎在第一个try块的开头就停止了,并且在两个catch块中都添加了stackTrace,而没有添加任何内容 void getLoginAction(ActionEvent event) throws IOException, Exception { String username = tfUsern
void getLoginAction(ActionEvent event) throws IOException, Exception {
String username = tfUsername.getText();
String password = tfPassword.getText();
loggingUsers.setUserName(username);
loggingUsers.setPassword(password);
FileHandler userFh = new FileHandler("UserLog.txt", true);
SimpleFormatter sf = new SimpleFormatter();
userFh.setFormatter(sf);
uLog.addHandler(userFh);
uLog.setLevel(Level.INFO);
try {
ObservableList<User> loginInfo = DatabaseMan.getActiveUsers();
loginInfo.forEach((u) -> {
try {
assert loggingUsers.getUserName().equals(u.getUserName()) && loggingUsers.getPassword().equals(u.getPassword()) : "Incorrect login info!";
loggingUsers.setUserId(u.getUserId());
try {
Appointment upcomingAppt = DatabaseAppointments.getUpcomingAppt();
if (!(upcomingAppt.getAppointmentId() == 0)) {
Alert apptAlert = new Alert(Alert.AlertType.INFORMATION);
apptAlert.setTitle("Upcoming Appointment Reminder");
apptAlert.setHeaderText("You have an upcoming appointment!");
apptAlert.setContentText("You have an appointment scheduled"
+ "\non " + upcomingAppt.getStart().format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL))
+ "\nat " + upcomingAppt.getStart().format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL))
+ " with client " + upcomingAppt.getCustomer().getCustomerName() + ".");
apptAlert.showAndWait();
if (apptAlert.getResult() == ButtonType.OK) {
userLog.log(Level.INFO, "User: {0} logged in.", loggingUsers.getUserName());
Stage loginStage = (Stage) btnLogin.getScene().getWindow();
loginStage.close();
FXMLLoader apptCalLoader = new FXMLLoader(AppointmentCalendarController.class.getResource("MainAppointment.fxml"));
Parent calScreen = calLoader.load();
Scene calScene = new Scene(calScreen);
Stage calStage = new Stage();
calStage.setTitle("Appointment Calendar");
calStage.setScene(apptCalScene);
calStage.show();
}
else {
apptAlert.close();
}
}
else {
userLog.log(Level.INFO, "User: {0} logged in.", loggingUsers.getUserName());
FXMLLoader apptCalLoader = new FXMLLoader(AppointmentCalendarController.class.getResource("MainAppointment.fxml"));
Parent calScreen = calLoader.load();
Scene calScene = new Scenec(calScreen);
Stage calStage = new Stage();
calStage.setTitle("Appointment Calendar");
calStage.setScene(apptCalScene);
calStage.show();
Stage loginStage = (Stage) btnLogin.getScene().getWindow();
loginStage.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (AssertionError e) {
System.out.println(e.getMessage());
this.lblAlert.setText(this.rb.getString("lblErrorAlert") + ".");
this.lblAlert.setTextFill(Paint.valueOf("RED"));
userLog.log(Level.WARNING, "Invalid credentials entered! User: {0}", loggingUsers.getUserName());
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
void getLoginAction(ActionEvent事件)引发IOException,Exception{
字符串username=tfUsername.getText();
字符串密码=tfPassword.getText();
loggingUsers.setUserName(用户名);
loggingUsers.setPassword(密码);
FileHandler userFh=newfilehandler(“UserLog.txt”,true);
SimpleFormatter sf=新的SimpleFormatter();
userFh.setFormatter(sf);
uLog.addHandler(userFh);
uLog.setLevel(Level.INFO);
试一试{
ObservableList loginInfo=DatabaseMan.getActiveUsers();
登录信息forEach((u)->{
试一试{
断言loggingUsers.getUserName().equals(u.getUserName())和&loggingUsers.getPassword().equals(u.getPassword()):“不正确的登录信息!”;
loggingUsers.setUserId(u.getUserId());
试一试{
约会upcomingAppt=数据库约会。getUpcomingAppt();
如果(!(upcomingAppt.GetAppointId()==0)){
Alert apptAlert=新警报(Alert.AlertType.INFORMATION);
apptAlert.setTitle(“即将到来的约会提醒”);
setHeaderText(“您有一个即将到来的约会!”);
apptAlert.setContentText(“您已安排了约会”
+“\non”+UpComingApp.getStart().format(DateTimeFormatter.OfCalizedDate(FormatStyle.FULL))
+“\nat”+upcomingAppt.getStart().format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL))
+“带客户端”+upcomingAppt.getCustomer().getCustomerName()+”;
apptAlert.showAndWait();
if(apptAlert.getResult()==ButtonType.OK){
log(Level.INFO,“用户:{0}已登录。”,loggingUsers.getUserName());
Stage loginStage=(Stage)btnLogin.getScene().getWindow();
loginStage.close();
FXMLLoader apptCalLoader=新的FXMLLoader(AppointmentCalendarController.class.getResource(“MainAppointment.fxml”);
父calScreen=calLoader.load();
场景calScene=新场景(calScreen);
阶段calStage=新阶段();
calStage.setTitle(“预约日历”);
calStage.setScene(apptCalScene);
calStage.show();
}
否则{
apptAlert.close();
}
}
否则{
log(Level.INFO,“用户:{0}已登录。”,loggingUsers.getUserName());
FXMLLoader apptCalLoader=新的FXMLLoader(AppointmentCalendarController.class.getResource(“MainAppointment.fxml”);
父calScreen=calLoader.load();
场景calScene=新场景(calScreen);
阶段calStage=新阶段();
calStage.setTitle(“预约日历”);
calStage.setScene(apptCalScene);
calStage.show();
Stage loginStage=(Stage)btnLogin.getScene().getWindow();
loginStage.close();
}
}
捕获(IOE异常){
e、 printStackTrace();
}
}
捕获(断言错误){
System.out.println(e.getMessage());
this.lblAlert.setText(this.rb.getString(“lblErrorAlert”)+”;
这个.lblAlert.setTextFill(Paint.valueOf(“红色”));
userLog.log(Level.WARNING,“输入的凭据无效!用户:{0}”,loggingUsers.getUserName());
}
});
}
捕获(例外e){
e、 printStackTrace();
}
}
按下按钮时不会出现错误消息,也不会发生任何事情我们放弃了按钮点击上标记的ActionEvent
下面是我们如何命名动作事件和代码示例
屏幕截图来自场景生成器
@FXML
private void onBack() throws IOException{
stage = (Stage)childTVPane.getScene().getWindow();// pane you are ON
paneStart = FXMLLoader.load(getClass().getResource("start.fxml"));// pane you are GOING TO
Scene scene = new Scene(paneStart);// pane you are GOING TO
scene.getStylesheets().add(getClass().getResource("diary.css").toExternalForm());
stage.setScene(scene);
stage.setTitle("Diary");
stage.show();
stage.sizeToScene();
stage.centerOnScreen();
}
享受并欢迎来到SO我假设getLoginAction()方法中的代码是在单击按钮时执行的。请提供方法体的按钮单击事件insetad的代码处理执行。请..如何将该方法添加为事件处理程序?如果是通过fxml,则可能缺少
@fxml
注释。。。(或者可能导入了错误的ActionEvent
,谁知道呢…)我有一个问题,为什么要删除action事件。如果您注意到我的代码不需要初始化或分配任何操作,那么我只需要转到initialize方法并在那里分配操作?@NewVegasProgrammer。在我们的例子onBack()中,您只是使用了OnAction的一个术语。语法更易于阅读和使用