首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StackPane setBackground

StackPane setBackground
EN

Stack Overflow用户
提问于 2020-12-06 05:28:56
回答 1查看 40关注 0票数 0

下面是我的代码:

代码语言:javascript
复制
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中还有另外两个StackPanes(leftPane和rightPane)。我想只将leftPane的背景颜色设置为黄色,但结果是整个窗口的背景都是黄色的,我做错了什么?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-07 20:36:57

如果您没有特殊的理由使用StackPane作为根目录,那么可以使用AnchorPane。你可以为你的左侧和右侧StackPanes设置3个侧边锚点,并将你的stackpanes的宽度与你的根宽度绑定在一起,所以如果你调整你的窗口大小,你的stackpanes将会适应它

代码语言:javascript
复制
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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65162164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档