JavaFX如何设置场景背景图像

JavaFX如何设置场景背景图像,javafx,javafx-2,Javafx,Javafx 2,如何设置场景的背景图像?其中一种方法可能如下所示: 1) 创建一个名为“style.CSS”的CSS文件,并在其中定义一个id选择器: 2) 使用CSS中定义的值设置场景中最顶层控件(或任何控件)的id,并将此CSS文件加载到场景中: public class Test extends Application { public static void main(String[] args) { launch(args); } @Override

如何设置场景的背景图像?

其中一种方法可能如下所示:

1) 创建一个名为“style.CSS”的CSS文件,并在其中定义一个id选择器:

2) 使用CSS中定义的值设置场景中最顶层控件(或任何控件)的id,并将此CSS文件加载到场景中:

  public class Test extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();
        root.setId("pane");
        Scene scene = new Scene(root, 300, 250);
        scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
您还可以在FXML文件中为控件提供id:

<StackPane id="pane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="demo.Sample">
    <children>
    </children>
</StackPane>


有关JavaFX CSS样式的更多信息,请参阅此。

您可以使用
直接更改场景的样式。root
类:

.root {
    -fx-background-image: url("https://www.google.com/images/srpr/logo3w.png");
}
将此添加到CSS中,并将其加载为他在回答中描述的“Uluk Biy”。

我知道这是一个老问题

但如果您想以编程方式或java方式进行此操作

用于图像背景;你可以使用class

用于绘制或填充背景;你可以使用class


保持java的活力和css的活力。

除了@Elltz answer,我们还可以使用填充和图像作为背景:

someNode.setBackground(
            new Background(
                    Collections.singletonList(new BackgroundFill(
                            Color.WHITE, 
                            new CornerRadii(500), 
                            new Insets(10))),
                    Collections.singletonList(new BackgroundImage(
                            new Image("image/logo.png", 100, 100, false, true),
                            BackgroundRepeat.NO_REPEAT,
                            BackgroundRepeat.NO_REPEAT,
                            BackgroundPosition.CENTER,
                            BackgroundSize.DEFAULT))));
使用


(与上一个参数不同)以使图像具有完整的窗口大小。

我之前发布了另一个问题,请帮助链接此处我可以从另一个函数更改场景背景吗,除了voidstart@Ossama是的,你可以。定义两个不同的CSS选择器(具有两个不同的背景属性),并在某些方法中使用所需的id更改节点的id。我如何才能做到这一点。这是我的代码一个中心不够吗?哎呀!如何使图像填充整个场景?在左侧和顶部留下空白。
-fx背景重复:拉伸它仍在保留空间。场景的根是一个网格窗格,我将网格窗格放在场景的中心。如何防止背景图像居中?
-fx背景位置:左上我正在使用scene builder将css设置为一个边界窗格,当我运行应用程序时它会工作,但我在scene builder中没有看到任何更改,你知道如何修复吗?
BackgroundImage myBI= new BackgroundImage(new Image("my url",32,32,false,true),
        BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
          BackgroundSize.DEFAULT);
//then you set to your node
myContainer.setBackground(new Background(myBI));
BackgroundFill myBF = new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1),
         new Insets(0.0,0.0,0.0,0.0));// or null for the padding
//then you set to your node or container or layout
myContainer.setBackground(new Background(myBF));
someNode.setBackground(
            new Background(
                    Collections.singletonList(new BackgroundFill(
                            Color.WHITE, 
                            new CornerRadii(500), 
                            new Insets(10))),
                    Collections.singletonList(new BackgroundImage(
                            new Image("image/logo.png", 100, 100, false, true),
                            BackgroundRepeat.NO_REPEAT,
                            BackgroundRepeat.NO_REPEAT,
                            BackgroundPosition.CENTER,
                            BackgroundSize.DEFAULT))));
setBackground(
                new Background(
                        Collections.singletonList(new BackgroundFill(
                                Color.WHITE,
                                new CornerRadii(0),
                                new Insets(0))),
                        Collections.singletonList(new BackgroundImage(
                                new Image("file:clouds.jpg", 100, 100, false, true),
                                BackgroundRepeat.NO_REPEAT,
                                BackgroundRepeat.NO_REPEAT,
                                BackgroundPosition.DEFAULT,
                                new BackgroundSize(1.0, 1.0, true, true, false, false)
                        ))));