我想设置一个与我的窗口/屏幕大小相同的背景图像。
我更愿意在我的 CSS
文件中执行此操作,但我未找到一种方法完成这个。
我必须在 javafx 类文件中执行此操作吗?
感谢您的帮助;)
最佳答案
您必须确定 Java 代码中的屏幕大小,如 JavaFX window sizing 中所示,无法在 CSS 中确定它。
对于图像,在你的java代码中你可以使用一些东西作为
ImageView imageView = new ImageView(image);
imageView.setFitWidth(Screen.getPrimary().getVisualBounds().getWidth());
imageView.setFitHeight(Screen.getPrimary().getVisualBounds().getHeight());
如果要将背景图像设置为场景,则:
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 样式表:
.root{
-fx-background-image: url("background_image.jpg");
-fx-background-size: cover;
}
关于java - 将背景图像设置为与 Java 应用程序中的窗口/屏幕大小相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23515172/