Javafx:在setOnAction中更改场景 [英] Javafx: Change scene in setOnAction

查看:194
本文介绍了Javafx:在setOnAction中更改场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含多个场景的JavaFX应用程序。在setOnAction事件中更改场景时,我的变量范围有问题。这是我的代码:

I'm building a JavaFX application with multiple Scenes. I have a problem with scope of variable when changing scenes within setOnAction event. This is my code:

Stage myStage;

public Scene logInScene(){
   ... all the buttons / textFields

   createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
        public void handle(ActionEvent t){
              **this.getStage().allScene(createAccountPane1);**
        }
   }
}

public Stage getStage(){
      return this.myStage;
}

public void allScene(Pane p){
      this.myStage.setScene(p);
}

我在setOnAction函数中遇到错误。找不到符号getStage ()。我知道这必须是一个范围问题,并且它不能识别该范围之外的任何变量/函数。我如何制作它以便我可以在内部进行更改?我已经尝试通过变量但是这只是让我的代码混乱,我希望有一个更简单的方法。谢谢你们!

I'm getting an error within the setOnAction function. "Cannot Find Symbol" getStage(). I know this must be a scope problem and it doesn't recognize any variables / functions outside of that scope. How do I make it so that I can change within? I've tried passing through the variable but that will just make my code messy and I wish there was a simpler way. Thanks guys!

推荐答案

只要你保持一致,你的代码就可以运行:

Your code works as long as you keep consistency:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Test extends Application{
    private Stage stage;
    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        Scene scene = logInScene();
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public Scene logInScene(){
        Pane root = new Pane();
        Button createAccountButton = new Button("create account");
        createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent t){
                  stage.setScene(CreateAccountScene());
            }
       });
        root.getChildren().add(createAccountButton);
        return new Scene(root);
    }
    protected Scene CreateAccountScene() {
        VBox root = new VBox();
        Label userLabel = new Label("Insert the username:");
        final TextField userField = new TextField();
        Button createAccountButton = new Button("create account");
        createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent t){
                  System.out.println("Account for user " + userField.getText() + " was created succesfully");
            }
       });
        root.getChildren().addAll(userLabel,userField,createAccountButton);
        return new Scene(root);
    }
    public static void main(String[] args) {
        launch(args);
    }

}

这篇关于Javafx:在setOnAction中更改场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆