简体   繁体   English

使用 JavaFx,如何仅在填充内更改面板背景颜色?

[英]Using JavaFx, How do I change a panels background color only inside the padding?

I have a panel in JavaFx, I am trying to change a panel color only inside applied padding.我在 JavaFx 中有一个面板,我正在尝试仅在应用的填充内更改面板颜色。 Using this code...使用此代码...

control_box.setStyle(
                "-fx-background-color: #f4f4f4;" + 
                "-fx-background-padding: 10;" +     
                "-fx-padding: 10;" + 
                "-fx-border-style: solid inside;" + 
                "-fx-border-width: 2;" +
                "-fx-border-insets: 5;" + 
                "-fx-border-radius: 5;" + 
                "-fx-border-color: lightgrey;"
               );

I get this result...我得到这个结果...

在此处输入图像描述

I am trying to get the blue to come all the way up to the padding border.我试图让蓝色一直到填充边界。 I am new to using css so any help would be appreciated.我是使用 css 的新手,因此我们将不胜感激。

Firstly, there is no css property "-fx-background-padding" in JavaFX CSS.首先,在 JavaFX CSS 中没有 css 属性“-fx-background-padding”。

You can set multiple backgrounds and position them using the css property "-fx-background-insets".您可以使用 css 属性“-fx-background-insets”设置多个背景和 position。 This is a very powerful/useful property that almost the entire predefined css (in modena.css) relies on this for styling the controls.这是一个非常强大/有用的属性,几乎整个预定义的 css(在 modena.css 中)都依赖于它来设置控件的样式。

Having said that, there will not be single concrete solution when it comes to styling.话虽如此,在造型方面不会有单一的具体解决方案。 You can get the same effect in multiple ways (as specified by @n247s & @jewelsea).您可以通过多种方式获得相同的效果(由@n247s 和@jewelsea 指定)。
Apart from those two, another way to get the desired behavior is using "-fx-background-insets".除了这两个之外,获得所需行为的另一种方法是使用“-fx-background-insets”。

Approach 1方法一

If you are very specific in using "-fx-border-insets", you can include an extra color to the background and control the two colors using -fx-background-padding.如果您非常具体地使用“-fx-border-insets”,您可以在背景中添加额外的颜色,并使用 -fx-background-padding 控制两个 colors。

In the below code, I included "transparent" color and managed it to render for required space and then started rendering the desired color(#f4f4f4) from there.在下面的代码中,我包含了“透明”颜色并管理它以渲染所需的空间,然后从那里开始渲染所需的颜色(#f4f4f4)。

controlBox1.setStyle(
            "-fx-background-color: transparent, #f4f4f4;" +
                    "-fx-background-insets:0, 5;" +
                    "-fx-background-radius: 5;" +
                    //"-fx-background-padding: 10;" +
                    "-fx-padding: 10;" +
                    "-fx-border-style: solid inside;" +
                    "-fx-border-width: 2;" +
                    "-fx-border-insets: 5;" +
                    "-fx-border-radius: 5;" +
                    "-fx-border-color: lightgrey;"
    );

Approach 2方法二

I dont recommend using "-fx-border-insets" if you have one border color.如果您有一种边框颜色,我不建议使用“-fx-border-insets”。 If you managed to set margins (as mentioned by @jewelsea) you can just remove "-fx-border-insets".如果您设法设置边距(如@jewelsea 所述),您可以删除“-fx-border-insets”。

HBox.setMargin(controlBox2, new Insets(5));
controlBox2.setStyle(
        "-fx-background-color: #f4f4f4;" +
                "-fx-background-radius: 5;" +
                //"-fx-background-padding: 10;" +
                "-fx-padding: 10;" +
                "-fx-border-style: solid inside;" +
                "-fx-border-width: 2;" +
                // "-fx-border-insets: 5;" +
                "-fx-border-radius: 5;" +
                "-fx-border-color: lightgrey;"
);

Notice that I included "-fx-background-radius" in both approaches to avoid edged background corners.请注意,我在两种方法中都包含“-fx-background-radius”以避免边缘背景角。

Working Example:工作示例:

在此处输入图像描述

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class BackgroundPadding_Demo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        StackPane root = new StackPane();
        root.setStyle("-fx-background-color:lightblue;");
        Scene scene = new Scene(root, 500, 200);
        stage.setScene(scene);
        stage.show();

        StackPane controlBox1 = new StackPane(new Label("Box1"));
        controlBox1.setAlignment(Pos.TOP_LEFT);
        controlBox1.setMinSize(200, 100);
        controlBox1.setMaxSize(200, 100);
        controlBox1.setStyle(
                "-fx-background-color: transparent, #f4f4f4;" +
                        "-fx-background-insets:0, 5;" +
                        "-fx-background-radius: 5;" +
                        //"-fx-background-padding: 10;" +
                        "-fx-padding: 10;" +
                        "-fx-border-style: solid inside;" +
                        "-fx-border-width: 2;" +
                        "-fx-border-insets: 5;" +
                        "-fx-border-radius: 5;" +
                        "-fx-border-color: lightgrey;"
        );

        StackPane controlBox2 = new StackPane(new Label("Box2"));
        controlBox2.setAlignment(Pos.TOP_LEFT);
        controlBox2.setMinSize(200, 100);
        controlBox2.setMaxSize(200, 100);
        HBox.setMargin(controlBox2, new Insets(5));
        controlBox2.setStyle(
                "-fx-background-color: #f4f4f4;" +
                        "-fx-background-radius: 5;" +
                        //"-fx-background-padding: 10;" +
                        "-fx-padding: 10;" +
                        "-fx-border-style: solid inside;" +
                        "-fx-border-width: 2;" +
                        // "-fx-border-insets: 5;" +
                        "-fx-border-radius: 5;" +
                        "-fx-border-color: lightgrey;"
        );

        HBox hBox = new HBox(controlBox1, controlBox2);
        hBox.setSpacing(15);
        hBox.setAlignment(Pos.CENTER);
        root.getChildren().add(new Group(hBox));
    }

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM