Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX-按钮切换到错误的场景_Java_Javafx_Screen_Switching - Fatal编程技术网

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

JavaFX-按钮切换到错误的场景,java,javafx,screen,switching,Java,Javafx,Screen,Switching,在这个程序中,我只使用一个场景,但更改其根节点 最初,程序显示“欢迎”屏幕(welcomeRoot)。但是,当我按下其中的“登录”按钮时,屏幕会变为“选择游戏模式”屏幕,即使在我的代码中,它清楚地说明了“logInRoot” 显然,logInRoot并不像gmodeRoot那样初始化 以下是代码的摘录,为清晰起见,将其缩短 import javafx.application.Application; import javafx.event.ActionEvent; import javafx.e

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

最初,程序显示“欢迎”屏幕(welcomeRoot)。但是,当我按下其中的“登录”按钮时,屏幕会变为“选择游戏模式”屏幕,即使在我的代码中,它清楚地说明了“logInRoot”

显然,logInRoot并不像gmodeRoot那样初始化

以下是代码的摘录,为清晰起见,将其缩短

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);
    }
}
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.TextArea;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类WhatsError扩展应用程序{
public void start(Stage theStage)引发异常{
//田地
VBox welcomeRoot=新的VBox(20);
VBox logInRoot=新的VBox(20);
VBox signupoot=新的VBox(20);
VBox gmodeRoot=新的VBox(20);
VBox SinglePlayerRot=新的VBox(20);
VBox多层箭头=新的VBox(20);
VBox statsRoot=新的VBox(20);
字符串currentUser=“”;
//欢迎屏幕
标签theWelcomeMessage=新标签(“欢迎参加莫尔斯电码学习计划”);
标签welcomeLabel=新标签(“欢迎屏幕”);
按钮登录tn=新按钮(“登录”);
logInBtn.setOnAction(e->theStage.getScene().setRoot(logInRoot));//正确的链接。。。
按钮signUpBtn=新按钮(“登录”);
logInBtn.setOnAction(e->theStage.getScene().setRoot(signupoot));
Button UnlockedBtn=新按钮(“继续Unlocked”);
logInBtn.setOnAction(e->theStage.getScene().setRoot(gmodeRoot));
//此按钮直接链接到选择游戏模式。
welcomeRoot.getChildren().addAll(WelcomeMessage、welcomeLabel、logInBtn、signUpBtn、unloggedBtn);
//登录屏幕
按钮登录backbtn=新按钮(/*此处绘制反向箭头*/);
登录backbtn.setOnAction(e->theStage.getScene().setRoot(welcomeRoot));
标签登录标签=新标签(“登录”);
TextArea logInUsernameInput=新TextArea();
logInUsernameInput.setPrompText(“用户名-仅限字母和数字,最大长度15”);
logInUsernameInput.setId(“用户名”);
TextArea logInPasswordInput=新建TextArea();
logInPasswordInput.setPrompText(“密码-至少一个字母、数字和特殊字符”);
logInPasswordInput.setId(“密码”);
logInPasswordInput.getText();
按钮登录确认按钮n=新按钮(“继续”);
logInConfirmBtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
//验证,如果正确则显示错误消息,如果正确则将用户名保存到currentUsername。
}
});
Label logInUsernameError=新标签(/*此处x符号*/“错误-用户名已获取或无效”);
Label logInPasswordError=新标签(/*x符号此处*/“错误-密码无效”);
//将这些输入到根目录中,并在需要时删除
logInRoot.getChildren().addAll(logingobacktn、logInLabel、logInUsernameInput、logInPasswordInput、logInConfirmBtn);
//注册屏幕
//代码和一些伪代码-删除以节省空间
//选择游戏模式屏幕
按钮gmodeGoBackBtn=新按钮(/*此处绘制反向箭头*/);
gmodeGoBackBtn.setOnAction(/*如果登录,则显示一个关于注销的弹出窗口*/e->stage.getScene().setRoot(welcomeRoot));
Label gmodeLabel=新标签(“选择游戏模式”);
按钮gmodeTo1PlayerBtn=新按钮(“单人播放器”);
gmodetoplayerbtn.setOnAction(e->theStage.getScene().setRoot(singleplayerRoot));
按钮gmodeToMultiBtn=新按钮(“多人游戏”);
gmodeToMultiBtn.setOnAction(e->theStage.getScene().setRoot(multiplayerRoot));
按钮gmodestatsbtn=新按钮(“统计”);
gmodestatsbtn.setOnAction(e->theStage.getScene().setRoot(statroot));
gmodeRoot.getChildren().addAll(gmodeGoBackBtn、gmodeLabel、gmodetoplayerbtn、gmodeToMultiBtn、gmodeToStatsBtn);
//等等。
//处理舞台
setTitle(“摩尔斯电码-教育计划”);
Scene theScene=new Scene(welcomeRoot);//替换为logInRoot,登录屏幕将按预期显示。
第三阶段:设置场景(场景);
stage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}

p.S.“摘录”;希望我能让你笑;)

这里您创建了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));
将另一个侦听器添加到同一按钮将覆盖以前设置的任何侦听器,这就是为什么将root设置为
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));

不,请。。。我怎么会忽视这一点?谢谢你的回答。我现在已经纠正了它,而且它是有效的。它发生了:)我很高兴我能帮助你,而且它现在起作用了。