如何在JavaFX中打开另一个屏幕(不是FXML文件)

时间:2019-01-03 23:38:57

标签: java javafx

我对JavaFX非常陌生。我想在程序中的两个窗口之间通信,但是我不知道该怎么做。例如:成功登录后,将弹出另一个屏幕(但是我不想加载文件FXML,因为我使用循环在此窗口中生成切换按钮,而不是拖放到FXML文件中)。我不知道这是否有意义。如果没有,请原谅我:(

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{

Parentroot=FXMLLoader.load(getClass().getResource("sample.fxml"));

    primaryStage.setTitle("Login");
    primaryStage.setScene(new Scene(root, 400,300));
    primaryStage.show();
}


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

Controller.java

import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;

public class Controller {
@FXML
private TextField usernameField;
@FXML
private TextField passwordField;

@FXML
public void login(){
    if(usernameField.getText().equals("admin") && passwordField.getText().equals("123")){
        successLogin();
    }
}

public void successLogin(){
    try {
        Parent root = FXMLLoader.load(getClass().getResource("anotherscene.fxml"));
        Stage stage = new Stage(StageStyle.DECORATED);
        stage.show();

    }catch (IOException e){
        e.printStackTrace();
    }

}
}

AnotherScene.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class AnotherScene implements Initializable {
    ToggleButton[][] matrix;
    private static final int SIZE = 10;
    private static int length = SIZE;
    private static int width = SIZE;
    @FXML
    private BorderPane borderPane;



@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    Parent root = null;
    try{
        root = FXMLLoader.load(getClass().getResource("anotherscene.fxml"));
    }catch (IOException e){
        e.getStackTrace();
    }
    Stage stage = new Stage();

    VBox vBox = new VBox();
    vBox.getChildren().add(new Label("Seat Number: "));
    borderPane.setLeft(vBox);

    GridPane gridPane = new GridPane();
    gridPane.setHgap(10);
    gridPane.setVgap(10);
    borderPane.setCenter(gridPane);

    matrix = new ToggleButton[length][width];


    for(int row = 0; row < length; row++){
        for(int col = 0; col < width; col++){
            matrix[row][col] = new ToggleButton();
            matrix[row][col].setText(row + "" +col);
            matrix[row][col].setPrefWidth(40);
            gridPane.add(matrix[row][col], row, col);

        }
    }

    //      Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    stage.setTitle("Hello World");
    Scene scene = new Scene(root, 700, 500);
    stage.setScene(scene);
    stage.show();
    }
}

0 个答案:

没有答案