我在将ImageIcon设置到我的JLabel时遇到问题。我正在尝试通过
设置ImageIconJLabel label = new JLabel();
label.setIcon(...);
,它会导致ImageIcon被切断。到目前为止,我已经尝试了几乎所有方法,但似乎找不到任何有效方法。
这是在具有Java 1.8的Windows 10下发生的。
我有一个返回ImageIcon的方法
public ImageIcon getIconFromFilePath(String filePath, int width, int height) throws FileNotFoundException {
Image image = ShellFolder.getShellFolder(new File(filePath)).getIcon(true);
return new ImageIcon(width == 0 || height == 0 ? image : image.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH), null);
}
作为参考,如果我不想调整ImageIcon的大小:
public ImageIcon getIconFromFilePath(String filePath) throws FileNotFoundException {
return getIconFromFilePath(filePath, 0, 0);
}
我将其设置如下:
try {
label = new JLabel(getIconFromFilePath(filepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
我还尝试通过以下方式获取图标(不是ImageIcon):
FileSystemView.getFileSystemView().getSystemIcon(new File(filepath));
哪个实习生会导致不同的方法返回类型。
我在没有切断任何东西的Macbook上尝试过。
在Windows上,Java似乎在内部将Icon放在某种容器中,然后将其切断。
解决这个问题有办法吗?非常感谢!