JavaFX显示场景背景图像 [英] JavaFX display scene background image

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

问题描述

我正在使用Java SDK1.7.0_45中的内置FX库在Eclipse Kepler上开发JavaFX应用程序。我想在场景中显示背景图像。按照Java文档中提供的教程,以下代码应该可以工作:

  public class Main extends Application {
public static void main(String [] args){
Application.launch(args);
}

@Override
public void start(Stage primaryStage){
GridPane grid = new GridPane();
场景场景=新场景(网格,300,275);
primaryStage.setScene(scene);
scene.getStylesheets()。add(Main.class.getResource(Login.css)。toExternalForm());
primaryStage.show();
}

}



我的CSV文件如下所示:

  .root {
-fx-background-image:url(background。 JPG);

}



但我得到的是而是空白屏幕。我在src / application文件夹中有3个文件:background.jpg,Main.java和Login.css。



我尝试添加反斜杠,将图像放入单独的文件夹,提供绝对路径,提供几种类型的图像,使用../application/background.jpg,将代码更改为file:background.jpg,使用imageview直接将URL提供给代码并解除CSS文件相反,.....但没有任何作用。



我看了几个其他stackoverflow链接,似乎都失败了:



'ul>
  • 的JavaFX如何设置场景背景图像(呈现空白屏幕)

  • 通过javafx代码设置背景图像(不是css)异常)

  • 无法在JavaFX中加载图像

  • 等等。



  • 奇怪的是,当我从服务器提供图像作为超链接时,一切正常。提供本地文件的路径永远不会工作。我究竟做错了什么?有人可以告诉我如何显示本地图像吗?这是一个错误?


    解决方案

    部分由于从WillBD答案,我决定放弃Eclipse的开普勒和Netbeans的一切重新开始。我使用了我在问题中提供的完全相同的代码,现在一切正常。我想这是JavaFX和Eclipse Kepler之间的一个错误。


    I’m developing a JavaFX application on Eclipse Kepler using the built-in FX library from Java SDK1.7.0_45. I want to display a background image in a scene. Following the tutorial provided in the Java documentation, following code should work:

    public class Main extends Application {
        public static void main(String[] args) {
        Application.launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
        GridPane grid = new GridPane();
        Scene scene = new Scene(grid, 300, 275);
        primaryStage.setScene(scene);
        scene.getStylesheets().add(Main.class.getResource("Login.css").toExternalForm());
        primaryStage.show();
        }
    

    }

    My CSV file looks like this:

    .root {
     -fx-background-image: url("background.jpg");
    

    }

    But I just get a blank screen instead. I have 3 files in the src/application folder: background.jpg, Main.java and Login.css.

    I have tried adding a backslash, putting the image into a separate folder, providing an absolute path, providing several types of images, using ../application/background.jpg, changing the code to file:background.jpg, providing the URL directly into the code and dismissing the CSS file, using an imageview instead, ..... but nothing works.

    I've taken a look at several other stackoverflow links, all seemed to fail:

    The strange thing is, when I supply an image from a server as a hyperlink, everything works fine. Supplying the path to a local file never works though. What am I doing wrong? Can somebody show me how to display a local image? Is this a bug?

    解决方案

    Partly thanks to the answer from WillBD, I decided to ditch Eclipse Kepler and start all over in Netbeans. I used the exact same code I provided in my question and now everything works just fine. I guess this is a bug between JavaFX and Eclipse Kepler.

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

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