background to BorderPane javafx

问题: I made a UI stage. and I have a BorderPane in the stage. now, I want to add a bacgGround Image to layout I want to use CSS but it doesn't work public void start(Sta...

问题:

I made a UI stage. and I have a BorderPane in the stage. now, I want to add a bacgGround Image to layout

I want to use CSS but it doesn't work

    public void start(Stage primaryStage) throws Exception {
    carStage = new Stage();
    BorderPane layout = new BorderPane();
    carScene = new Scene(layout);
    carStage.setTitle("רכב");
    GridPane center = new GridPane();
    enterFieldsToCenter(center);
    right(layout);
    layout.setCenter(center);
    layout.setStyle("style.css");
    carStage.setScene(carScene);
    layout.getCenter().setStyle("-fx-background-image: url("C:\Users\itayz\eclipse-workspace\Itay'sCar\src\CrystalClear.jpg");"
            + "-fx-background-size: 500, 500;"
            + "-fx-background-repeat: no-repeat;");

    sizeOfStage();
    carStage.show();

}

the background is from : https://uigradients.com/#CrystalClear how can I import it easily?

Note: the file exist in the path


回答1:

The Problem is that your URI has no scheme.

From to the docs:

url ( ["']? <address> ["']? )

<address> can be an absolute URI, for example:

  • url(http://example.com)

  • url('http://example.com')

  • url("http://example.com")

or it can be relative to the location of the CSS file.

So you have to use an absolute URL with scheme:

layout.getCenter().setStyle("-fx-background-image: url("file:///C:/Users/itayz/eclipse-workspace/Itay'sCar/src/CrystalClear.jpg");"
        + "-fx-background-size: 500, 500;"
        + "-fx-background-repeat: no-repeat;");

Beside that I would recommend to use a relative URL and place the image in your project resources.


回答2:

This pattern worked for me:

-fx-background-image: url('file:C:/Data/example.jpg');

So, in your case it would be:

layout.getCenter().setStyle("-fx-background-image: url('file:C:/Users/itayz/eclipse-workspace/Itay'sCar/src/CrystalClear.jpg');"
                                    + "-fx-background-size: 500, 500;"
                                    + "-fx-background-repeat: no-repeat;");
  • 发表于 2019-03-22 04:14
  • 阅读 ( 227 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除