[英]Insert image into borderpane as background
我想將圖像加載到BorderPane中。 我有一個靜態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);
}
您能告訴我如何正確加載圖像嗎?
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我設法用以下代碼加載圖像:
private static final ImageView iv;
static {
iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());
}
borderpane.setCenter(iv);
我知道了。 您甚至不需要處理圖像加載。 訣竅是使用style屬性:
borderpane.setStyle("-fx-background-image: url(\"/images/system-help.png\");-fx-background-size: 500, 500;-fx-background-repeat: no-repeat;")
當然,您應該外包css文件而不使用內聯樣式。 它更容易顯示。
如果只想設置中心區域的背景,請使用以下命令:
borderpane.getCenter().setStyle("-fx-background-image: url(\"/images/system-help.png\");-fx-background-size: 500, 500;-fx-background-repeat: no-repeat;")
注意:
system-help.png
位於/images
目錄中,該目錄與應用程序的類文件位於同一級別(例如,使用eclipse,它必須存儲在bin
文件夾中,這意味着您添加了一個文件夾(例如,調用它的資源)添加到您的項目中,並將其用作source folder
(在該文件夾中,您將需要圖像文件夾。有點奇怪,但這就是fx
如何需要它)。
編輯:調整大小如下:
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.