gpt4 book ai didi

java - 将图像插入边框作为背景

转载 作者:行者123 更新时间:2023-11-30 07:14:59 25 4
gpt4 key购买 nike

我想将图像加载到 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

附言2我设法用这段代码加载图像:

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

borderpane.setCenter(iv);

最佳答案

我让它工作了。您甚至不需要处理图像加载。诀窍是使用样式属性:

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 文件夹,这意味着您向项目添加一个文件夹(例如将其称为 resources)并将其用作 source 文件夹,在此文件夹中您将需要图片文件夹。有点奇怪,但这正是 fx 需要它的方式)。

编辑:调整大小如下:

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

关于java - 将图像插入边框作为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18164695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com