【问题标题】:JavaFX How to set scene background imageJavaFX 如何设置场景背景图片
【发布时间】:2012-04-02 01:09:12
【问题描述】:

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

【问题讨论】:

    标签: javafx javafx-2


    【解决方案1】:

    除了@Elltz 的回答,我们可以同时使用填充和图像作为背景:

    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)
                            ))));
    

    (不同的最后一个参数)使图像全窗口大小。

    【讨论】:

      【解决方案2】:

      我知道这是一个老问题

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

      对于图像背景;你可以使用BackgroundImage

      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

      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));
      

      让你的 java 活着 && 你的 css 死了..

      【讨论】:

        【解决方案3】:

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

        .root {
            -fx-background-image: url("https://www.google.com/images/srpr/logo3w.png");
        }
        

        将此添加到 CSS 并将其加载为他的回答中描述的“Uluk Biy”。

        【讨论】:

        • 糟糕!如何使图像充满整个场景?它在左侧和顶部留下空白。
        • -fx-background-repeat: stretch;
        • 它仍然留有空间。场景的根是一个 GridPane,我将网格窗格放在场景的中心。如何防止背景图片居中?
        • -fx-background-position: top left;
        • 我正在使用场景构建器将 css 设置为边框窗格,当我运行应用程序时它可以工作,但我没有看到场景构建器有任何变化,你知道如何解决这个问题吗?跨度>
        【解决方案4】:

        其中一种方法可能是这样的:

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

        #pane{
            -fx-background-image: url("background_image.jpg");
            -fx-background-repeat: stretch;   
            -fx-background-size: 900 506;
            -fx-background-position: center center;
            -fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0); 
        }
        

        2) 将场景中最顶层的控件(或任何控件)的 id 设置为 CSS 中定义的值,并将此 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 样式的更多信息,请参阅guide

        【讨论】:

        • 我之前发布了另一个问题,请帮助链接这里stackoverflow.com/questions/9736393/…
        • 除了 void start 之外,我可以从另一个功能更改场景背景
        • @Ossama 是的,你可以。定义两个不同的 CSS 选择器(具有两个不同的背景属性),并在某些方法中将节点的 id 更改为所需的。
        • 我该怎么做。这是我的代码stackoverflow.com/questions/22572940/…
        • 一个中心还不够吗?
        猜你喜欢
        • 2014-01-09
        • 2016-10-03
        • 1970-01-01
        • 1970-01-01
        • 2022-12-05
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2017-10-30
        相关资源
        最近更新 更多