简体   繁体   English

Java Mac将通知添加到停靠图标(1),(2)等

[英]Java mac add notifications to the dock icon (1), (2), etc

Im trying to figure out how i can add number notifications to my dock icon when a new message is received (I've got a small chat app) 我正在尝试弄清楚如何在收到新消息时向我的停靠图标添加数字通知(我有一个小型聊天应用程序)

This is what i mean: 这就是我的意思:

在此处输入图片说明

Any ideas how to accomplish this? 任何想法如何做到这一点?

Try taking a look at com.apple.eawt.Application (I'm having a hard time finding JavaDocs) 尝试看看com.apple.eawt.Application (我很难找到JavaDocs)

简单柜台

import com.apple.eawt.Application;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestBadge {

    public static void main(String[] args) {
        new TestBadge();
    }

    public TestBadge() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private int count;

        public TestPane() {
            setLayout(new GridBagLayout());
            add((label = new JLabel("0")));
            Timer timer = new Timer(250, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    count++;
                    label.setText(Integer.toString(count));
                    Application.getApplication().setDockIconBadge(Integer.toString(count));
                }
            });
            timer.start();
        }

    }

}

I should point out, that if you're running in an IDE capable of inspecting the installed classes, you should be able to get a list of the functionality provided by the Application class. 我应该指出,如果您正在能够检查已安装类的IDE中运行,则应该能够获得Application类提供的功能列表。 I was using NetBeans and was able to find a listing of all the methods, problem is, some were documented and some weren't :P 我使用的是NetBeans,能够找到所有方法的清单,问题是,有的已记录在案,有的不是:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM