Ulu*_*Biy 38
其中一种方法可能是这样的:
1)创建一个名为"style.css"的CSS文件,并在其中定义一个id选择器:
Run Code Online (Sandbox Code Playgroud)#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)使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以在FXML文件中为控件提供id:
<StackPane id="pane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="demo.Sample">
<children>
</children>
</StackPane>
Run Code Online (Sandbox Code Playgroud)
有关JavaFX CSS样式的更多信息,请参阅本指南.
Ell*_*ltz 32
我知道这是一个古老的问题
但是如果你想以编程方式或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));
Run Code Online (Sandbox Code Playgroud)
对于油漆或填充背景; 你可以使用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));
Run Code Online (Sandbox Code Playgroud)
保持你的java活着&&你的css死了..
Ser*_*nev 12
您可以使用.root类直接为场景更改样式:
.root {
-fx-background-image: url("https://www.google.com/images/srpr/logo3w.png");
}
Run Code Online (Sandbox Code Playgroud)
将其添加到CSS并将其加载为他的答案中描述的"Uluk Biy".