简体   繁体   English

将图像插入borderpane作为背景

[英]Insert image into borderpane as background

I want to load image into BorderPane. 我想将图像加载到BorderPane中。 I have a static Java method which makes this task more complicated for me: 我有一个静态Java方法,这使我的工作更加复杂:

private static final Image iv;    
    static {
        iv = new Image(StartPanel.class.getClass().getResource("/images/system-help.png").toExternalForm());
    }

public static void insert(){

     bp.setCenter(iv);
}

Can you tell me how I can load the image properly? 您能告诉我如何正确加载图像吗?

PS I get this PS我明白了

Executing com.javafx.main.Main from /home/rcbandit/Desktop/test/DX-57DC/dist/run1833589211/DX-57DC.jar using platform /opt/jdk1.7.0_25/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.javafx.main.Main.launchApp(Main.java:642)
    at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NullPointerException
    at com.dx57dc.stages.StartPanel.infrastructureIcon(StartPanel.java:241)
    at com.dx57dc.stages.StartPanel.navigationPanel(StartPanel.java:138)
    at com.dx57dc.stages.StartPanel.agentsPanel(StartPanel.java:78)
    at com.dx57dc.stages.Agents.agentsPanel(Agents.java:9)
    at com.dx57dc.main.DX57DC.initMainStage(DX57DC.java:287)
    at com.dx57dc.main.DX57DC.start(DX57DC.java:115)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
    ... 1 more
Java Result: 1

PS 2 I managed to load the image with this code: PS 2我设法用以下代码加载图像:

    private static final ImageView iv;    
        static {
            iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());
        }

borderpane.setCenter(iv);

I got it to work. 我知道了。 You not even need to handle the image load. 您甚至不需要处理图像加载。 The trick is to use the style property: 诀窍是使用style属性:

borderpane.setStyle("-fx-background-image: url(\"/images/system-help.png\");-fx-background-size: 500, 500;-fx-background-repeat: no-repeat;")

For sure you should outsource the css file and not use inline styles. 当然,您应该外包css文件而不使用内联样式。 It's just easier to show. 它更容易显示。

If you just want to set the background of the center area use this: 如果只想设置中心区域的背景,请使用以下命令:

borderpane.getCenter().setStyle("-fx-background-image: url(\"/images/system-help.png\");-fx-background-size: 500, 500;-fx-background-repeat: no-repeat;")

Note: 注意:
The system-help.png is located in the /images directory, which is at the same level as the class file for the application (eg using eclipse it must be stored in the bin folder, which means you add a folder (for example call it resources) to your project and use it as a source folder , in this folder than you will need the image folder. Kinda weird but thats how fx needs it). system-help.png位于/images目录中,该目录与应用程序的类文件位于同一级别(例如,使用eclipse,它必须存储在bin文件夹中,这意味着您添加了一个文件夹(例如,调用它的资源)添加到您的项目中,并将其用作source folder (在该文件夹中,您将需要图像文件夹。有点奇怪,但这就是fx如何需要它)。

Edit: Resize as following: 编辑:调整大小如下:

private static final ImageView iv;    
static {
            iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());
            iv.resize(100, 100);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM