问:对切换屏幕JavaFX感到困惑

时间:2018-03-03 21:10:11

标签: javafx

我不明白如何在当前屏幕和注册屏幕之间切换:

package Computing;

import javafx.scene.layout.*;import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;

public class Computing extends Application {

    Stage stage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        primaryStage.setTitle("Computing Quiz");

        GridPane gridPane = createComputingQuizPane();
        // ui controls to gridpane
        addUIControls(gridPane);
        // scene
        Scene scene = new Scene(gridPane, 800, 500);
        // primary stage scene set
        primaryStage.setScene(scene);

        primaryStage.show();
    }


    private GridPane createComputingQuizPane() {
        // Instantiate a new Grid Pane
        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setPadding(new Insets(40, 40, 40, 40));
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        return gridPane;
    }

    private void addUIControls(GridPane gridPane) {
        // HEADER
        Label headerLabel = new Label("Welcome to the Computing Quiz!");
        headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
        gridPane.add(headerLabel, 0,0,2,1);
        GridPane.setHalignment(headerLabel, HPos.CENTER);
        GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));

        // LOGIN BUTTON
        Button loginButton = new Button("Login");
        loginButton.setPrefHeight(50);
        loginButton.setDefaultButton(true);
        loginButton.setPrefWidth(125);
        gridPane.add(loginButton, 0, 4, 2, 1);
        GridPane.setHalignment(loginButton, HPos.CENTER);
        GridPane.setMargin(loginButton, new Insets(20, 0,20,0));


        // REGISTER BUTTON
        Button registerButton = new Button ("Register");
        registerButton.setOnAction(e-> buttonClick());
        registerButton.setPrefHeight(50);
        registerButton.setDefaultButton(true);
        registerButton.setPrefWidth(125);
        gridPane.add(registerButton,0,5,2,1);
        GridPane.setHalignment(registerButton,HPos.CENTER);
        GridPane.setMargin(registerButton, new Insets(20,0,20,0));

        //registerButton.setOnAction(new EventHandler<ActionEvent>() {
            //@Override
            //public void handle(ActionEvent event) {
            //Register nc = new Register(stage);
            //}
        //});

    }

    public void buttonClick(){
        Register nc = new Register(stage);
    }
    public static void main(String[] args) {
        launch(args);
    }
}

这是注册屏幕:

package Computing;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;

public class Register {
    Stage stage;
    public Register (Stage s){
        stage = s;
    }

    public void start(Stage primaryStage){
        primaryStage.setTitle("Registration");

        GridPane gridPane = createRegisterPane();
        addUIControls(gridPane);
        Scene scene = new Scene(gridPane, 800, 500);
        primaryStage.setScene(scene);

        primaryStage.show();
    }


    private GridPane createRegisterPane() {
        GridPane gridPane = new GridPane();

        gridPane.setAlignment(Pos.CENTER);

        gridPane.setPadding(new Insets(40, 40, 40, 40));

        gridPane.setHgap(10);

        gridPane.setVgap(10);


        return gridPane;
    }

    private void addUIControls(GridPane gridPane) {
        // Add Header
        Label headerLabel = new Label("Registration Form");
        headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
        gridPane.add(headerLabel, 0,0,2,1);
        GridPane.setHalignment(headerLabel, HPos.CENTER);
        GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));

        // FULL NAME
        Label nameLabel = new Label("Full Name : ");
        gridPane.add(nameLabel, 0,1);

        // FULL NAME TEXT
        TextField nameField = new TextField();
        nameField.setPrefHeight(40);
        gridPane.add(nameField, 1,1);


        // USERNAME
        Label emailLabel = new Label("Username : ");
        gridPane.add(emailLabel, 0, 2);

        // EMAIL TEXT
        TextField emailField = new TextField();
        emailField.setPrefHeight(40);
        gridPane.add(emailField, 1, 2);

        // PASSWORD
        Label passwordLabel = new Label("Password : ");
        gridPane.add(passwordLabel, 0, 3);

        // PASSWORD TEXT
        PasswordField passwordField = new PasswordField();
        passwordField.setPrefHeight(40);
        gridPane.add(passwordField, 1, 3);

        // SUBMIT BUTTON
        Button submitButton = new Button("Submit");
        submitButton.setPrefHeight(40);
        submitButton.setDefaultButton(true);
        submitButton.setPrefWidth(100);
        gridPane.add(submitButton, 0, 4, 2, 1);
        GridPane.setHalignment(submitButton, HPos.CENTER);
        GridPane.setMargin(submitButton, new Insets(20, 0,20,0));

        submitButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(nameField.getText().isEmpty()) {
                    showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error", "Please enter your name.");
                    return;
                }
                if(emailField.getText().isEmpty()) {
                    showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error", "Please enter a Username");
                    return;
                }
                if(passwordField.getText().isEmpty()) {
                    showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error!", "Please enter a password");
                    return;
                }

                showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Registration Successful!", "Welcome " + nameField.getText());
            }
        });
    }

    private void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
        Alert alert = new Alert(alertType);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.initOwner(owner);
        alert.show();
    }

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

我已尝试使用此代替buttonClick方法,我也有点混淆为什么它不会像以前一样使用buttonClick将场景发送到下一个屏幕

submitButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {

0 个答案:

没有答案