bar*_*892 4 java icons label javafx
我正在尝试从文件(.ico/.exe)中读取缩略图(图标; 32x32px)并将其设置为JavaFX标签.
我的第一次尝试:
public Icon getLargeIcon(String exeFile) {
if (exeFile != null) {
File file = new File(exeFile);
try {
ShellFolder sf = ShellFolder.getShellFolder(file);
return new ImageIcon(sf.getIcon(true), sf.getFolderType());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
之后,我这样做:
Icon largeIcon = getLargeIcon(file.getAbsolutePath());
ImageIcon swingImageIcon = (ImageIcon) largeIcon;
java.awt.Image awtImage = swingImageIcon.getImage();
Image fxImage = javafx.scene.image.Image.impl_fromPlatformImage(awtImage);
lblAppIconValue.setGraphic(new ImageView(fxImage));
Run Code Online (Sandbox Code Playgroud)
我通过几个网站搜索并找到了这个,但它给了我一个例外:
java.lang.UnsupportedOperationException: unsupported class for loadPlatformImage
我的第二次尝试:
URL url = file.toURI().toURL();
Image image = new Image(url.toString());
lblAppIconValue.setGraphic(new ImageView(image));
Run Code Online (Sandbox Code Playgroud)
也没工作......
我的问题:如何将javax.swing.Icon设置为JavaFX标签?可能吗?如果不可能,我如何从文件中读取缩略图并将其设置为JavaFX标签的图标/图形?
谢谢!
永远不要使用impl_方法:这些不是公共API的一部分.
要将awt Image转换为FX Image,SwingFXUtils类in in javafx.embed.swing有一个toFXImage(...)将a转换BufferedImage为JavaFX的方法Image.目前尚不清楚图标中的图像是否为a BufferedImage,因此您需要执行以下几个步骤:
BufferedImage bImg ;
if (awtImage instanceof BufferedImage) {
bImg = (BufferedImage) awtImage ;
} else {
bImg = new BufferedImage(awtImage.getWidth(null), awtImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bImg.createGraphics();
graphics.drawImage(awtImage, 0, 0, null);
graphics.dispose();
}
Image fxImage = SwingFXUtils.toFXImage(bImg, null);
Run Code Online (Sandbox Code Playgroud)
这是一种相当低效的方法,因为您首先从文件创建awt图像,然后将其转换为FX图像,可能通过中间缓冲图像.如果您可以访问ShellFolder该类的源代码,您可能会看到它如何实现该getIcon()方法并遵循相同的过程.在某些时候,它必须获得InputStream图像数据; 一旦你有了,你可以将它传递给javafx.scene.image.Image构造函数.
| 归档时间: |
|
| 查看次数: |
3953 次 |
| 最近记录: |