设置背景图片大小相同的的Java应用程序窗口/屏幕 [英] Set background image the same size as the window/screen in Java app

查看:1487
本文介绍了设置背景图片大小相同的的Java应用程序窗口/屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置背景图片大小相同我的窗口/屏幕。

I would like to set a background image the same size as my window/screen.

我会的 preFER 的做这在我的 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?

感谢每一个帮助;)

推荐答案

您必须确定在Java code在屏幕尺寸如示的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 code你可以使用的东西为

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应用程序窗口/屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆