使用JavaFX加载新屏幕

使用JavaFX加载新屏幕,java,javafx,screen,Java,Javafx,Screen,我正在设计一个应用程序,需要一个登录页面,然后如果登录成功,它应该加载一个新的屏幕。这是这里的代码: import java.util.ArrayList; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import

我正在设计一个应用程序,需要一个登录页面,然后如果登录成功,它应该加载一个新的屏幕。这是这里的代码:

import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class LoginPage extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Sign in here");
        GridPane grid = new GridPane();
        grid.setVgap(20);
        grid.setPadding(new Insets(25, 10, 10, 25));

        //Welcome title
        Text scenetitle = new Text("Welcome");
        scenetitle.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
        grid.add(scenetitle, 0, 0, 2, 1);

        //Enter username and label
        Label userName = new Label("Enter your Username:");
        grid.add(userName, 0, 1);

        TextField userTextField = new TextField();
        grid.add(userTextField, 1, 1);

        //Enter Password
        Label pw = new Label("Enter your password:");
        grid.add(pw, 0, 2);

        PasswordField pwBox = new PasswordField();
        grid.add(pwBox, 1, 2);

        //Button to login
        Button btn = new Button("Login");
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn.getChildren().add(btn);
        grid.add(hbBtn, 1, 4);

        final Text actiontarget = new Text();
        grid.add(actiontarget, 1, 6);

        //Set usernames and passwords
        ArrayList<String> userNames = new ArrayList<String>();
        ArrayList<String> passWord = new ArrayList<String>();

        //Add them in here
        userNames.add("A");
        passWord.add("A");

        //display login success when button is pressed
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                String username = userTextField.getText();
                String password = pwBox.getText();

                //Check if correct here
                if(userNames.contains(username)&&passWord.contains(password)){
                    actiontarget.setFill(Color.GREEN);
                    actiontarget.setText("Login Sucessful");
                }else{
                    actiontarget.setFill(Color.RED);
                    actiontarget.setText("Login Unsucessful");
                }
            }
        });

        //Display
        Scene scene = new Scene(grid, 300, 275);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
这行得通吗?还是有别的办法?
谢谢。

提取您的代码并创建一个方法,该方法将为您显示登录屏幕

public boolean displayLoginPage(final Stage owner) {
    Stage stage = new Stage();
    stage.initOwner(owner);
    stage.setTitle("Sign in here");

    GridPane grid = new GridPane();
    grid.setVgap(20);
    grid.setHgap(5);
    grid.setPadding(new Insets(25, 10, 10, 25));

    //Welcome title
    Text sceneTitle = new Text("Welcome");
    sceneTitle.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
    grid.add(sceneTitle, 0, 0, 2, 1);

    //Enter username and label
    Label userName = new Label("Enter your Username:");
    grid.add(userName, 0, 1);

    TextField userTextField = new TextField();
    grid.add(userTextField, 1, 1);

    //Enter Password
    Label pw = new Label("Enter your password:");
    grid.add(pw, 0, 2);

    PasswordField pwBox = new PasswordField();
    grid.add(pwBox, 1, 2);

    //Button to login
    Button btn = new Button("Login");
    HBox hbBtn = new HBox(10);
    hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
    hbBtn.getChildren().add(btn);
    grid.add(hbBtn, 1, 4);

    final Text actiontarget = new Text();
    grid.add(actiontarget, 1, 6);

    //Set usernames and passwords
    List<String> userNames = new ArrayList<String>();
    List<String> passWord = new ArrayList<String>();

    //Add them in here
    userNames.add("A");
    passWord.add("A");

    final AtomicBoolean loginSuccessful = new AtomicBoolean();

    //display login success when button is pressed
    btn.setOnAction((ActionEvent e) -> {
        String username = userTextField.getText();
        String password = pwBox.getText();

        //Check if correct here
        if (userNames.contains(username) && passWord.contains(password)) {
            actiontarget.setFill(Color.GREEN);
            actiontarget.setText("Login Successful");
            loginSuccessful.set(true);
            stage.close();
        } else {
            actiontarget.setFill(Color.RED);
            actiontarget.setText("Login UnSuccessful");
            loginSuccessful.set(false);
            stage.close();
        }
    });

    //Display
    Scene scene = new Scene(grid, 400, 300);
    stage.setScene(scene);
    stage.showAndWait();

    return loginSuccessful.get();
}

显然,这并不完美,但它会让您开始

我建议您从以下方面开始: 你的主要场景是:

public class MainApp extends Application {
   private static Stage primaryStage;

   public MainApp() {
   }

@Override
    public void start(Stage primaryStage) {
        MainApp.primaryStage = primaryStage;
        MainApp.primaryStage.setTitle("test");
        loginScene();
//方法
loginsucessful()

//

还可以创建控制器
HomeSceneControl
等等。。。
希望它能帮助你开始

只需将你的问题标题粘贴到谷歌搜索栏中,你就会得到答案,我不明白你为什么会问这个问题。我不太理解你的代码-它非常高级,而我还是从JavaFX开始-你有什么指南我可以看一下吗?非常好的开始指南是
@Override
public void start(Stage primaryStage) throws Exception {
    BorderPane root = new BorderPane();
    VBox vBox = new VBox(5, new Label("Hello World"));
    root.setCenter(vBox);
    Scene scene = new Scene(root, 400, 300);

    boolean loginSuccessful = displayLoginPage(primaryStage);

    if (loginSuccessful) {
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
public class MainApp extends Application {
   private static Stage primaryStage;

   public MainApp() {
   }

@Override
    public void start(Stage primaryStage) {
        MainApp.primaryStage = primaryStage;
        MainApp.primaryStage.setTitle("test");
        loginScene();
    if (loginSuccessful()) {
        HomeScene();
     }  
  }

   private void loginScene() {
        try {
            FXMLLoader loader = new FXMLLoader();
            //path to your login fxml file
loader.setLocation(MainApp.class.getResource("/fxml/LoginScene.fxml"));
            rootLayout = loader.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  public void HomeScene() throws IOException {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("/fxml/HomeScene.fxml"));
        AnchorPane homeScenePane = loader.load();
        rootLayout.setCenter(homeScenePane );
        HomeSceneController controller = loader.getController();
        controller.setMainApp(this);
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}