我想让我的 java 程序在 Windows 的任务栏和 Mac 的 Dock 中有一个图标

I want to make my java program have an icon in the Taskbar for Windows and the Dock for Mac

我有一个为我的西班牙语老师制作的程序,我想让 dock/taskbar 有一个图标。我这里试了一下,还是不行。

package com.jaketherey;

import java.awt.Color;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Inglés_Switcher {

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {

        JFrame frame = new switcherContent();

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(950, 850);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setTitle("Inglés Switcher by Jacob Batista");
        frame.getContentPane().setBackground(Color.GREEN);

        //Attempt to Add Icon, Remember I want it for both Mac and Windows

        ImageIcon icon = new ImageIcon("/com/jaketherey/ñ.png");
        frame.setIconImage(icon.getImage());

        }

    });

}

}

如果您需要它用于内容的 JFrame,那么我会把它给您,但我认为它对问题并不重要...

帮忙?谢谢

对于Windows,先看看Window#setIconImages, demonstrated here

对于MacOS,就变得有点难了。您需要使用自定义库(仅在 Mac 上可用)

Apple eAWT 提供 Application class 允许更改应用程序的停靠栏图标。

import com.apple.eawt.Application;
...
Application application = Application.getApplication();
Image image = ...
application.setDockIconImage(image);

也演示了here

如上面链接的示例所述,在 MacOS 上部署的首选方法是使用 Mac OS application bundler