java - 按钮上嵌入的图像不会显示

标签 java image button javafx

我正在尝试创建一个带有嵌入图像的按钮,该图像显示在左侧,大小为 48 x 48,但显然我做错了一些事情,因为我一直得到 3 。所以我的问题是如何正确地获取我的图像展示?这是我所拥有的。

    Image buttonIcon = new Image("C:/Users/emanu_000/Documents/Excersise4/src/my-profile-icon.png");
    ImageView iconView = new ImageView(buttonIcon);

    getPicButton = new Button("Get Picture",iconView);
    getPicButton.setContentDisplay(ContentDisplay.LEFT);
    mainPane.setConstraints(getPicButton, 1, 3);
    mainPane.getChildren().add(getPicButton);

这是堆栈跟踪

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in        Application start method
at       com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$2/2093176254.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown  protocol: c
 at javafx.scene.image.Image.validateUrl(Image.java:1102)
at javafx.scene.image.Image.<init>(Image.java:608)
at com.company.Main.start(Main.java:134)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$52/135888596.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1904663592.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$50/1835699047.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/2102390814.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/216334026.run(Unknown Source)
... 1 more
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(URL.java:593)
at java.net.URL.<init>(URL.java:483)
at java.net.URL.<init>(URL.java:432)
at javafx.scene.image.Image.validateUrl(Image.java:1096)
... 16 more

最佳答案

根据Javadocs for Image ,你传递的参数是

the string representing the URL to use in fetching the pixel data

即这里需要一个 URL,而不是文件系统路径。

你可以做到

File file = new File("C:/Users/emanu_000/Documents/Excersise4/src/my-profile-icon.png");
URL url = file.toURI().toURL();
Image image = new Image(url.toExternalForm());

(我认为,更简单地说,new Image(file.toURI().toString()) 也有效)。

关于java - 按钮上嵌入的图像不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909266/

相关文章:

Java 首选项和国际化 (i18n)

javascript - 如何将图像保留在具有设定宽度和高度的 div 中,但隐藏图像的其余部分?

image - 使用 PIL (Pillow) 和 Image 但保持良好的分辨率

xcode - 如何在 IBAction 上设置或更改按钮背景颜色

java - 绕过 Grails View 解析器?

java - 使用自定义比较器排序列表不起作用

java - 无法单击 TreeView 中的元素 - Selenium webdriver java

css - JavaFX TableView 背景图片

Jquery 将相同的 id 定位为单击元素的类名?

css - 基于图像的按钮在 IE9 中模糊