我有一个简单的JavaFX类,该问题的结构如下。

BorderPane
     StackPane
         FlowPane


BorderPane包含StackPane,其中包含FlowPane。

现在,我已经尝试了好几天,以使StackPane填充其父项BorderPane,但似乎没有任何效果。最终结果应为StackPane,其背景设置为“红色”。

代码:

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author A4XX-COLCRI
 */
public class LogicGatesSimulator extends Application {
    /**
     * The root node of the application.
     */
    Group root;
    /**
     * This is the top-level container child of root.
     */
    BorderPane mainContainer;
    /**
     * Contains the canvas and represents the drawing area.
     */
    Pane canvasContainer;
    /**
     * Defines where all the shapes will be drawn.
     */
    Canvas canvas;
    /**
     * Defines the toolbox pane.
     */
    StackPane toolboxPane;
    /**
     * Defines the toolbox flowpane.
     */
    FlowPane toolbox;
    /**
     * Defines the top bar menu.
     */
    MenuBar menu;


    @Override
    public void start(Stage primaryStage) {
        // Inizializzazione degli elementi grafici
        root = new Group();
        mainContainer = new BorderPane();
        canvasContainer = new Pane();
        canvas = new Canvas();
        toolboxPane = new StackPane();
        toolboxPane.setStyle("-fx-background-color: red; ");
        toolbox = new FlowPane(Orientation.VERTICAL);
        toolbox.setPrefWrapLength(250);
        toolbox.minWidth(Region.USE_COMPUTED_SIZE);
        toolbox.minHeight(Region.USE_COMPUTED_SIZE);
        toolbox.maxWidth(Region.USE_COMPUTED_SIZE);
        toolbox.maxHeight(Region.USE_COMPUTED_SIZE);
        toolbox.prefHeight(Region.USE_COMPUTED_SIZE);
        //toolbox.setStyle("-fx-background-color: red;");

        // Inizializzazione scena
        Scene scene = new Scene(root, 1280, 800);
        primaryStage.setTitle("Logic Gates Simulator");
        primaryStage.setScene(scene);

        // Menu
        menu = new MenuBar();
        Menu menuFile = new Menu("File");
        Menu menuEdit = new Menu("Edit");
        Menu menuView = new Menu("View");
        Menu menuAbout = new Menu("About");
        menu.getMenus().addAll(menuFile, menuEdit, menuView, menuAbout);

        // Porte logiche nella toolbox
        ImageView andImage = new ImageView(getClass().getResource("images/and.png").toExternalForm());
        ImageView nandImage = new ImageView(getClass().getResource("images/nand.png").toExternalForm());
        ImageView orImage = new ImageView(getClass().getResource("images/or.png").toExternalForm());
        ImageView norImage = new ImageView(getClass().getResource("images/nor.png").toExternalForm());
        ImageView xorImage = new ImageView(getClass().getResource("images/xor.png").toExternalForm());
        ImageView xnorImage = new ImageView(getClass().getResource("images/xnor.png").toExternalForm());
        ImageView notImage = new ImageView(getClass().getResource("images/not.png").toExternalForm());

        // Strutturazione
        toolboxPane.getChildren().add(toolbox);
        mainContainer.setLeft(toolboxPane);
        mainContainer.setRight(canvasContainer);
        toolbox.getChildren().addAll(andImage, nandImage, orImage, norImage, xorImage, xnorImage, notImage);
        canvasContainer.getChildren().add(canvas);
        root.getChildren().add(mainContainer);

        // Mostra la scena
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}


我的应用程序正在运行的照片

java - 如何使StackPane填充JavaFX中的BorderPane-LMLPHP

因此,基本上,此左侧区域应完全填充BorderPane的左侧,因此背景应正确覆盖其区域。
我还尝试将其从StackPane更改为AnchorPane,并将其TopAnchor,LeftAnchor等设置为0,但是它不起作用。
谢谢。

最佳答案

您从结构描述中省略了导致行为的部分:

Group

Group将其子元素的大小调整为他们喜欢的大小。它不可调整大小,因此调整舞台大小不会影响Group或其任何后代...

只需删除Group并使用mainContainer BorderPane作为场景根。

10-08 11:45