通过JavaFX代码设置背景图片(不是CSS) [英] Setting background image by javafx code (not css)
本文介绍了通过JavaFX代码设置背景图片(不是CSS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码将图像设置为背景:
I'm trying to set an image as the background using this code:
root.setStyle("-fx-background-image: url('splash.jpg');
-fx-background-position: center center;
-fx-background-repeat: stretch;");
但是它不起作用.如果使用CSS进行设置,则效果很好:
But it doesn't work. If I set it with CSS, it works perfectly:
root.setId("pane");
primaryStage.getScene().getStylesheets().add(JavaFXApplication9.class.getResource("style.css").toExternalForm());
和CSS:
#pane{
-fx-background-image: url('splash.jpg');
-fx-background-repeat: stretch;
-fx-background-position: center center;
}
我所有的文件(主类,CSS和图像)都放在同一包中.
All of my files (main class, CSS and image) placed into the same package.
那么,如何使用代码设置背景图像?或者,如何从应用程序代码中覆盖(替换)有关CSS中某些元素的背景图像的行?谢谢!
So, how can I set the background image using code? Or, how can I override (replace) lines about background-image of some element in CSS from application code? Thanks!
推荐答案
再试一次:
String image = JavaFXApplication9.class.getResource("splash.jpg").toExternalForm();
root.setStyle("-fx-background-image: url('" + image + "'); " +
"-fx-background-position: center center; " +
"-fx-background-repeat: stretch;");
这篇关于通过JavaFX代码设置背景图片(不是CSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文