如何设置场景的背景图片?
发布于 2012-03-16 23:03:43
其中一种方法可能是这样的:
1)创建一个名为"style.css“的CSS文件,并在其中定义一个id选择器:
#窗格{-fx-背景-图像: url("background_image.jpg");-fx-背景-重复:拉伸;-fx-背景-大小: 900 506;-fx-背景-位置:居中;-fx-效果:投射阴影(三通框,黑色,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();
}
}您还可以在FXML文件中为控件提供id:
<StackPane id="pane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="demo.Sample">
<children>
</children>
</StackPane>有关JavaFX样式的更多信息,请参考此guide。
发布于 2015-03-14 22:24:31
我知道这是个老问题
但是,如果您想以编程方式或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死了..
发布于 2012-03-16 23:15:02
您可以使用.root类直接更改场景的样式:
.root {
-fx-background-image: url("https://www.google.com/images/srpr/logo3w.png");
}将其添加到CSS中并加载为"Uluk Biy“,如他的答案所述。
https://stackoverflow.com/questions/9738146
复制相似问题