我正在使用 JavaFX。本质上,我试图让一个按钮在您单击它时打开一个新的舞台、场景和窗格。

I'm working with JavaFX. Essentially, I'm trying to make a button open a new stage, scene, and pane whenever you click on it.

我正在开发 GUI n JavaFX。我想让我的第一个阶段的按钮可以点击,并在被点击后打开一个新阶段,以便它显示另一个场景,其中包含另一组按钮和另一组图像。

package application;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;


public class Main extends Application {
@Override
public void start(Stage primaryStage){

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
    gridpane.setHgap(5.5);
    gridpane.setVgap(5.5);

    //create a label and add it to the "Dispense" stage
    Label lblTotal = new Label("Your total items for today is: ");
    lblTotal.setFont(Font.font("Times New Roman",17));
    lblTotal.setWrapText(true);

    //create new FlowPane to TEST 
    FlowPane flowpane = new FlowPane();
    flowpane.setPrefHeight(10);
    flowpane.setAlignment(Pos.TOP_CENTER);
    flowpane.getChildren().add(lblTotal);

    /** CREATE SECOND STAGE **/
    Stage stage2 = new Stage();
    stage2.setHeight(400);
    stage2.setWidth(350);

    //create buttons
    Button button1 = new Button("Candy");
    Button button2 = new Button("Gum");
    Button button3 = new Button("Drink");
    Button button4 = new Button("Chips");
    Button button5 = new Button("Done");
    Button button6 = new Button("Cancel");

    // add buttons to grid
    // position buttons on grid
    gridpane.add(button1, 0, 2);
    gridpane.add(button2, 0, 3);
    gridpane.add(button3, 0, 4);
    gridpane.add(button4, 0, 5);
    gridpane.add(button5, 0, 6);
    gridpane.add(button6, 0, 7);

    //sets alignment of buttons
    GridPane.setHalignment(button1, HPos.CENTER);
    GridPane.setHalignment(button2, HPos.CENTER);
    GridPane.setHalignment(button3, HPos.CENTER);
    GridPane.setHalignment(button4, HPos.CENTER);
    GridPane.setHalignment(button5, HPos.CENTER);
    GridPane.setHalignment(button6, HPos.CENTER);

    //create images 
    Image imgCandy = new Image("worms.jpg"); //creates image
    ImageView imageViewCandy = new ImageView(imgCandy); //displays the image
    imageViewCandy.setFitHeight(100); // sets the height of the "worms.jpg" image
    imageViewCandy.setFitWidth(100); // sets the width of the "worms.jpg" image

    Image imgGum = new Image("gum.jpg");
    ImageView imageViewGum = new ImageView(imgGum);
    imageViewGum.setFitHeight(100);
    imageViewGum.setFitWidth(100);

    Image imgDrink = new Image("soda.jpg");
    ImageView imageViewDrink = new ImageView(imgDrink);
    imageViewDrink.setFitHeight(100);
    imageViewDrink.setFitWidth(100);

    Image imgChips = new Image("chips.jpg");
    ImageView imageViewChips = new ImageView(imgChips);
    imageViewChips.setFitHeight(100);
    imageViewChips.setFitWidth(100);

    Image imgDone = new Image("done.png");
    ImageView imageViewDone = new ImageView(imgDone);
    imageViewDone.setFitHeight(100);
    imageViewDone.setFitWidth(100);

    // Add images to grid
    gridpane.add(imageViewCandy, 1, 2);
    gridpane.add(imageViewGum, 1, 3);
    gridpane.add(imageViewDrink, 1, 4);
    gridpane.add(imageViewChips, 1, 5);
    gridpane.add(imageViewDone, 1, 6);

    // sets each of the column size
    gridpane.getColumnConstraints().add(new ColumnConstraints(100));
    gridpane.getColumnConstraints().add(new ColumnConstraints(100));
    gridpane.getColumnConstraints().add(new ColumnConstraints(100));
    gridpane.getColumnConstraints().add(new ColumnConstraints(100));


    //creates a scene
    //titles the scene "Vending Machine"
    Scene scene = new Scene(gridpane, 350, 600);
    primaryStage.setTitle("Vending Machine");
    primaryStage.setScene(scene);
    primaryStage.show();

    /**Creates new scene and displays stage**/
    Scene scene2 = new Scene(flowpane, 400, 450);
    stage2.setTitle("Dispenser");
    stage2.setScene(scene2);
    stage2.show();

}

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

使用按钮上的 setOnAction 方法向每个按钮添加事件处理程序:

button1.setOnAction(e -> {
    // Create any hierarchy of nodes you want here
    HBox root = new HBox(new Label("Hello new stage"));

    // Create a new stage and and a scene to hold your root node
    Stage newStage = new Stage();
    newStage.setScene(new Scene(root));

    // Show the new stage
    newStage.show();
});

根节点可以是您想要在新舞台中显示的任何类型的节点。