JavaFX - 按钮切换到错误的场景



在这个程序中,我只使用一个场景,但更改了它的根节点。

最初,程序显示"欢迎"屏幕(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);
}
}

附言"摘录";希望我让你发笑;)

在这里,您正在创建 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));

最新更新