【问题标题】:Java mac add notifications to the dock icon (1), (2), etcJava mac添加通知到dock图标(1)、(2)等
【发布时间】:2013-12-03 09:11:21
【问题描述】:

我试图弄清楚如何在收到新消息时向我的停靠图标添加数字通知(我有一个小型聊天应用程序)

这就是我的意思:

有什么想法可以做到这一点吗?

【问题讨论】:

标签: java macos


【解决方案1】:

尝试查看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();
        }

    }

}

我应该指出,如果您在能够检查已安装类的 IDE 中运行,您应该能够获得Application 类提供的功能列表。我正在使用 NetBeans 并且能够找到所有方法的列表,问题是,有些已记录在案,有些则没有:P

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多