问题:
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:
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;");