Java 堆垛机

Java 堆垛机,java,javafx,Java,Javafx,这是我的密码: import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill;

这是我的密码:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class NotesApplication extends Application {
    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();
        root.setPrefSize(700,700);

        StackPane leftPane = new StackPane();
        leftPane.setPrefSize(100, 700);
        Button button = new Button("Button 1");
        leftPane.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
        leftPane.getChildren().add(button);
        leftPane.setAlignment(Pos.CENTER_LEFT);

        StackPane rightPane = new StackPane();
        rightPane.setPrefSize(200,700);
        Button button1 = new Button("Button 2");
        rightPane.getChildren().add(button1);
        rightPane.setAlignment(Pos.CENTER_RIGHT);

        root.getChildren().addAll(leftPane, rightPane);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
如您所见,我有一个类,它有一个根StackPane,在根StackPane中还有另外两个StackPane(leftPane和righpane)。我只想将leftPane的背景色设置为黄色,但结果是整个窗口都是黄色背景,我做错了什么


提前感谢。

如果您没有特别的理由使用StackPane作为根目录,您可以使用AnchorPane。您可以为左侧和右侧堆栈窗格设置3个侧锚,并将堆栈窗格的宽度与根宽度绑定,因此,如果调整窗口大小,堆栈窗格将适应它

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class NotesApplication extends Application {
    @Override
    public void start(Stage stage) {
        AnchorPane root = new AnchorPane(); // Changed stackpane to anchor pane
        root.setPrefSize(700,700);

        StackPane leftPane = new StackPane();
        leftPane.minWidthProperty().bind(root.widthProperty().divide(2)); // Binding leftPane's width with the half of your root

        Button button = new Button("Button 1");
        leftPane.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
        leftPane.getChildren().add(button);
        leftPane.setAlignment(Pos.CENTER_LEFT);

        StackPane rightPane = new StackPane();
        rightPane.minWidthProperty().bind(root.widthProperty().divide(2)); // Binding rightPane's width with the half of your root

        Button button1 = new Button("Button 2");
        rightPane.getChildren().add(button1);
        rightPane.setAlignment(Pos.CENTER_RIGHT);
        rightPane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));

        root.getChildren().addAll(leftPane, rightPane);

        // Setting the anchors 
        root.setLeftAnchor(leftPane, 0.0);
        root.setTopAnchor(leftPane, 0.0);
        root.setBottomAnchor(leftPane, 0.0);

        root.setRightAnchor(rightPane, 0.0);
        root.setTopAnchor(rightPane, 0.0);
        root.setBottomAnchor(rightPane, 0.0);


        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

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

StackPane
将调整其子项的大小,以填充所有可用空间,直至其最大大小。最重要的是,默认情况下,
StackPane
将使其子对象居中,因此将“左”和“右”分开是没有意义的。对根目录使用
HBox
是否会对您正在尝试的工作起作用?或
边框窗格
?或者是一个
SplitPane
?我对javafx相当陌生,所以整个系统中的VBox、HBox、StackPane等都是我正在努力学习的东西。我想做的是使窗口的左侧(可能20%宽度)和右侧(可能20%宽度)包含一些按钮和文本,然后稍后在中间添加一些内容。这听起来像是
边框窗格
()的一个很好的用例。还有,这里有一个和。目前正在调查,非常感谢:)