为什么Java AWT FileDialog setIconImage方法无法设置图标?
我正在尝试使用 Java AWT FileDialog,但我想用其他东西替换默认的 Java 对话框图标。简而言之,代码看起来像这样:
Frame frame = new Frame();
Image image = ImageIO.read(new URL("file:/path/to/myfile.jpg"));
FileDialog fileDialog = new FileDialog(frame, "Save As", FileDialog.SAVE);
fileDialog.setIconImage(image);
fileDialog.setDirectory("/path/to/directory");
fileDialog.setFile("filename.txt");
fileDialog.setVisible(true);
我尝试了几种变体,包括读取图像的不同方法、打包 FileDialog、打包 Frame、设置 Frame 的图标等。但是,无论我尝试什么,FileDialog 图标永远不会改变。当我设置框架的图标并将框架设置为可见时,框架显示了正确的图标,但对于 FileDialog 来说仍然不行。
有什么想法吗?
I am attempting to use a Java AWT FileDialog, but I want to replace the default Java Dialog icon with something else. In short, the code looks something like this:
Frame frame = new Frame();
Image image = ImageIO.read(new URL("file:/path/to/myfile.jpg"));
FileDialog fileDialog = new FileDialog(frame, "Save As", FileDialog.SAVE);
fileDialog.setIconImage(image);
fileDialog.setDirectory("/path/to/directory");
fileDialog.setFile("filename.txt");
fileDialog.setVisible(true);
I've tried several variation, including a different method of reading the image, packing the FileDialog, packing the Frame, setting the icon of the Frame, etc. However, regardless of what I try, the FileDialog icon never changes. When I set the icon of the Frame and set the Frame to visible, the frame displayed the correct icon, but it was still a no go for the FileDialog.
Any Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FileDialog 作为一个 AWT 组件,将与本地平台更加紧密地联系在一起。在 Solaris 情况下,它可能会从启动应用程序本身的 java 可执行文件中获取图标。
有什么原因不能使用 JFileChooser 吗?
FileDialog, being an AWT component, is going to be more tied to the local platform. In the Solaris case, it's possible it's picking up the icon from the java executeable that launched your app itself.
Is there a reason you can't use JFileChooser?
这可能无法在所有平台上实现。我猜
FileDialog
以某种方式使用操作系统提供的 filedialog,如果它不支持图标,那么你就不走运了。That might not be possible on all platforms. I guess the
FileDialog
somehow uses the filedialog provided by the OS and if that doesn't support icons you are out of luck.