问题描述
我想设置一个与我的窗口/屏幕大小相同的背景图像.
I would like to set a background image the same size as my window/screen.
我希望更喜欢在我的 CSS
文件中执行此操作,但我有 NOT FOUND 一种方法完成这个.
I would prefer to do this in my CSS
file, but I have NOT FOUND a way to accomplish this.
我必须在 javafx 类文件中执行此操作吗?
Must I do this in a javafx class file?
感谢您的每一个帮助;)
Thanks for every help ;)
推荐答案
您必须确定 java 代码中的屏幕尺寸,如 JavaFX 窗口大小,没有办法在 CSS 中确定.
You will have to determine the screen size in java code as demonstrated in JavaFX window sizing, there is no way to determine it in CSS.
对于一个图像,在你的 java 代码中你可以使用一些东西作为
For an Image, in your java code you can use something as
ImageView imageView = new ImageView(image);
imageView.setFitWidth(Screen.getPrimary().getVisualBounds().getWidth());
imageView.setFitHeight(Screen.getPrimary().getVisualBounds().getHeight());
如果要将背景图像设置为场景,则:
If you want to set the background image to a scene then:
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.*;
public class ScreenSizeImage extends Application {
@Override public void start(final Stage stage) {
// uncomment if you want the stage full screen.
//stage.setFullScreen(true);
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
stage.setX(bounds.getMinX());
stage.setY(bounds.getMinY());
stage.setWidth(bounds.getWidth());
stage.setHeight(bounds.getHeight());
StackPane root = new StackPane();
root.setStyle(
"-fx-background-image: url(" +
"'http://icons.iconarchive.com/icons/iconka/meow/256/cat-box-icon.png'" +
"); " +
"-fx-background-size: cover;"
);
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
当然,最好不要使用内联的 setStyle 调用,而是使用单独的 CSS 样式表,如下所示:
Of course, rather than the inline setStyle call you are best off using a separate CSS stylesheet like below:
.root{
-fx-background-image: url("background_image.jpg");
-fx-background-size: cover;
}
这篇关于将背景图像设置为与 Java 应用程序中的窗口/屏幕相同的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!