JavaFX中的背景图片 [英] Background image in JavaFX

查看:142
本文介绍了JavaFX中的背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取要在javaFX场景中加载的背景图像.我在这里找到的答案不起作用.窗口打开,但为空白(无图像).

I am trying to get a background image to load in a javaFX scene. The answers I have found on here are not working. The window opens, but it is blank (no image).

public class FirstFX extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        StackPane root = new StackPane();
        BackgroundImage myBI= new BackgroundImage(new Image("SnLBoard.png"),
            BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
        root.setBackground(new Background(myBI));
        stage.show();
    }
}

推荐答案

您尚未将根与任何内容相关联,因此该阶段中没有任何显示内容:

You haven't associated the root to anything, so nothing is showing within the stage:

StackPane root = new StackPane();
Scene scene = new Scene(root, 640, 480);
... //Display code and logic
stage.setScene(scene);
stage.show();

您还可以考虑设置窗格的最小/首选/最大大小.在这段代码之后,您将要启动一个线程,该线程开始您的应用程序逻辑.

You might also consider setting the min/preferred/max size of your pane. After this code, you'll want to start a thread that begins your application logic.

这篇关于JavaFX中的背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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