我遇到了从控制器中分离业务逻辑的问题(除了我在程序中验证数据的方式)。
这是逻辑类:
public class LogInMechanism {
LogInController logInController = new LogInController();
private static int howManyAttempts=0;
public void logIn()
{
howManyAttempts++;
if(howManyAttempts <3)
{
if(logInController.getLogInField().getText().equals("nick") && logInController.getPasswordField().getText().equals("password"))
{
System.out.println("Button clicked");
try {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/MainWindow.fxml"));
logInController.getLogInField().getScene().setRoot(root);
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
logInController.getInvalidUserOrPasswordLabel().setText("Invalid user or password "+(3-howManyAttempts)+ " attempts left!");
logInController.getInvalidUserOrPasswordLabel().setVisible(true);
}
}
else
{
logInController.getInvalidUserOrPasswordLabel().setDisable(true);
logInController.getInvalidUserOrPasswordLabel().setText("Your program has been blocked. Please contact manager!");
}
}
}
我希望在我的一个控制器中使用此功能
public class LogInController {
private LogInMechanism logInMechanism = new LogInMechanism();
@FXML
private Label invalidUserOrPasswordLabel;
@FXML
private TextField logInField;
@FXML
private Button logInButton;
@FXML
private PasswordField passwordField;
@FXML
void logInButtonClicked(ActionEvent event) {
logInMechanism.logIn();
}
@FXML
public void initialize()
{
EventHandler<KeyEvent> handlerLambda = e ->{
if(e.getCode()==KeyCode.ENTER)
{
// logInMechanism.logIn();
}
};
logInButton.addEventHandler(KeyEvent.KEY_PRESSED, handlerLambda);
}
public Label getInvalidUserOrPasswordLabel() {
return invalidUserOrPasswordLabel;
}
public TextField getLogInField() {
return logInField;
}
public PasswordField getPasswordField() {
return passwordField;
}
但不幸的是ifter执行这样的代码我收到了一个错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
Caused by: java.lang.RuntimeException: Exception in Application start method
Caused by: java.lang.StackOverflowError
错误出现在LogInMechanism类的第19行。我还在学习,如果你有什么建议请分享。