stackpane背景

ztigrdn8  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(287)

这是我的密码:

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和rightpane)。我只想设置左窗格的背景色为黄色,但结果是整个窗口都得到黄色背景,我做错了什么?
提前谢谢。

raogr8fs

raogr8fs1#

如果您没有特定的理由使用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);
    }
}

相关问题