java - 将默认的 Java 咖啡码头图标更改为其他图标
问题描述
我阅读了很多答案来更改图像中显示的默认 java 咖啡图标(操作系统:MacOS)(例如使用 JFrame.setIconImage()),但无法做到。
如果有人成功地改变它。您能否就您如何做到这一点提供逐步的解决方案,并可能提供解释?
解决方案
So, I did a really quick test using...
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import com.apple.eawt.Application;
public class Test {
public static void main(String[] args) {
try {
Application application = Application.getApplication();
application.setDockIconImage(ImageIO.read(Test.class.getResource("/javaapplication163/bunny.jpg")));
} catch (IOException ex) {
ex.printStackTrace();
}
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
public TestPane() {
add(new JLabel("Hello"));
}
}
}
And it showed...
Neat ... however, this will only compile and run on a Mac.
A "typical" solution to this would be to use reflection to try and load the com.apple.eawt.Application
at run time, something like...
try {
Class appClass = Class.forName("com.apple.eawt.Application");
Class params[] = new Class[]{Image.class};
Method getApplication = appClass.getMethod("getApplication");
Object application = getApplication.invoke(appClass);
Method setDockIconImage = appClass.getMethod("setDockIconImage", params);
setDockIconImage.invoke(application, ImageIO.read(Test.class.getResource("/javaapplication163/bunny.jpg")));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException | IOException ex) {
ex.printStackTrace();
}
But this is some what error prone and messy, not to mention a complete pain in the ... code to maintain.
So, then I thought I might try using the command line parameters, as demonstrated at The dock icon...
-Xdock:name="Hello bunny" -Xdock:icon=/path/to/bunny.jpg
which also worked.
While I kind of prefer this solution, it does mean that the icon needs to be externalised from Jar, which is kind of a pain, and you'll probably need to supply a full working path.
If I was to generate a App bundle for my project, I'd probably follow this, but if I was just deploying a Jar, I might be tempted to use one of the previous methods
推荐阅读
- swift - 取消初始化呈现的视图控制器
- ember.js - 强制 ember 存储跳过缓存并从 API 调用中获取数据
- java - 运行 jar 文件时在 docker 容器中加载 .so 库时出错
- kubernetes - 如何将 Kubernetes 服务类型“LoadBalancer”与特定云负载均衡器集成
- python - 扩展 code.InteractiveInterpreter 与历史,选项卡完成,
- python - Pyro4:在远程子对象上调用方法而不尝试返回子对象
- javascript - 如果我使用两个“if”语句而不仅仅是一个“if/else”语句,为什么二进制搜索算法不起作用?
- textures - 如何正确可视化 A-Frame 中的 AO 地图贡献
- python - 在 C++ 中实现 python np.fromstring()
- google-sheets - 创建 ImportXML 以从任何内容创建者的视频页面获取最后 30 个视频的单个 YouTube 视频视图。可能的?