在此程序中,我仅使用一个场景,但更改其根节点。
最初,程序显示“ Welcome”屏幕(welcomeRoot)。但是,当我按下其中的“登录”按钮时,即使在我的代码中它清楚地指出“ logInRoot”,屏幕也会切换到“选择游戏模式”屏幕。
绝对不是像gmodeRoot那样初始化logInRoot。
以下是代码的摘录,为清楚起见已将其简化。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class WhatsWrong extends Application {
public void start(Stage theStage) throws Exception {
// Fields
VBox welcomeRoot = new VBox(20);
VBox logInRoot = new VBox(20);
VBox signUpRoot = new VBox(20);
VBox gmodeRoot = new VBox(20);
VBox singleplayerRoot = new VBox(20);
VBox multiplayerRoot = new VBox(20);
VBox statsRoot = new VBox(20);
String currentUser = "";
// Welcome screen
Label theWelcomeMessage = new Label("Welcome to the Morse code learning program");
Label welcomeLabel = new Label("Welcome screen");
Button logInBtn = new Button("Log in");
logInBtn.setOnAction(e -> theStage.getScene().setRoot(logInRoot)); // correct link...
Button signUpBtn = new Button("Sign in");
logInBtn.setOnAction(e -> theStage.getScene().setRoot(signUpRoot));
Button unloggedBtn = new Button("Continue unlogged");
logInBtn.setOnAction(e -> theStage.getScene().setRoot(gmodeRoot));
// This button links directly to choosing the game mode.
welcomeRoot.getChildren().addAll(theWelcomeMessage, welcomeLabel, logInBtn, signUpBtn, unloggedBtn);
// Log in screen
Button logInGoBackBtn = new Button(/*back arrow drawing here*/);
logInGoBackBtn.setOnAction(e -> theStage.getScene().setRoot(welcomeRoot));
Label logInLabel = new Label("Log in");
TextArea logInUsernameInput = new TextArea();
logInUsernameInput.setPromptText("Username - only letters and numbers, max length 15");
logInUsernameInput.setId("username");
TextArea logInPasswordInput = new TextArea();
logInPasswordInput.setPromptText("Password - at least one letter, number and special character");
logInPasswordInput.setId("password");
logInPasswordInput.getText();
Button logInConfirmBtn = new Button("Continue");
logInConfirmBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// verify, display error message if correct, save username to currentUsername if correct.
}
});
Label logInUsernameError = new Label(/*x symbol here*/"Error - username taken or invalid");
Label logInPasswordError = new Label(/*x symbol here*/"Error - password invalid");
// input these into the root and delete if need be
logInRoot.getChildren().addAll(logInGoBackBtn, logInLabel, logInUsernameInput, logInPasswordInput, logInConfirmBtn);
// Sign up screen
// code and some pseudocode here - deleted to save space
// Choose game mode screen
Button gmodeGoBackBtn = new Button(/*back arrow drawing here*/);
gmodeGoBackBtn.setOnAction(/*If logged in, display a pop-up about logging-off*/e -> theStage.getScene().setRoot(welcomeRoot));
Label gmodeLabel = new Label("Choose the game mode");
Button gmodeTo1PlayerBtn = new Button("Singleplayer");
gmodeTo1PlayerBtn.setOnAction(e -> theStage.getScene().setRoot(singleplayerRoot));
Button gmodeToMultiBtn = new Button("Multiplayer");
gmodeToMultiBtn.setOnAction(e -> theStage.getScene().setRoot(multiplayerRoot));
Button gmodeToStatsBtn = new Button("Statistics");
gmodeToStatsBtn.setOnAction(e -> theStage.getScene().setRoot(statsRoot));
gmodeRoot.getChildren().addAll(gmodeGoBackBtn, gmodeLabel, gmodeTo1PlayerBtn, gmodeToMultiBtn, gmodeToStatsBtn);
// etc.
// Dealing with stage
theStage.setTitle("Morse code - educational program");
Scene theScene = new Scene(welcomeRoot); // Replace with logInRoot, and login screen shows up, as expected.
theStage.setScene(theScene);
theStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
P.S。 “摘抄”;希望我让你笑;)
答案 0 :(得分:4)
您将在此处创建3个按钮,但仅将动作监听器设置为logInBtn
:
Button logInBtn = new Button("Log in");
logInBtn.setOnAction(e -> theStage.getScene().setRoot(logInRoot));
Button signUpBtn = new Button("Sign in");
logInBtn.setOnAction(e -> theStage.getScene().setRoot(signUpRoot));
Button unloggedBtn = new Button("Continue unlogged");
logInBtn.setOnAction(e -> theStage.getScene().setRoot(gmodeRoot));
将其他侦听器添加到同一按钮将覆盖以前设置的所有侦听器,这就是为什么将根设置为gmodeRoot
以及为什么看到错误消息的原因。
我猜您想将其更改为:
Button logInBtn = new Button("Log in");
logInBtn.setOnAction(e -> theStage.getScene().setRoot(logInRoot));
Button signUpBtn = new Button("Sign in");
signUpBtn.setOnAction(e -> theStage.getScene().setRoot(signUpRoot));
Button unloggedBtn = new Button("Continue unlogged");
unloggedBtn.setOnAction(e -> theStage.getScene().setRoot(gmodeRoot));