如何让我的应用程序图标在Mac Dock中弹跳

时间:2013-02-26 01:31:20

标签: java macos

我正在用Java编写一个IRC客户端,我想知道是否有办法让我的应用程序的图标在触发一个nickalert(或任何其他相关通知)时在Dock中反弹。

如果Windows也有某种通知系统,我也想了解它。

1 个答案:

答案 0 :(得分:3)

在MacOS下,尝试使用Application#requestUserAttention(boolean)

之类的内容
import com.apple.eawt.Application;
...
Application application = Application.getApplication();
application.requestUserAttention(false);

nb-我没有试过这个我自己的抱歉。

更新了示例

来自JavaDocs

  

请求用户注意此应用程序(通常通过弹跳   Dock图标)。关键请求将继续反弹Dock   图标,直到应用程序被激活。已经有效的应用程序   要求注意什么都不做。

这意味着,如果应用程序具有焦点,那么该方法将不执行任何操作。

在Mac OSX 10.7.5,Java 1.7.0_07上进行测试

import com.apple.eawt.Application;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestMacIcon {

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

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

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

        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            final Application application = Application.getApplication();
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    try {
                        System.out.println("clicked");
                        application.requestUserAttention(true);
                        application.setDockIconImage(ImageIO.read(getClass().getResource("/Java.png")));
                        application.setDockIconBadge("Blah");
                        application.requestUserAttention(true);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            Timer time = new Timer(2000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!SwingUtilities.getWindowAncestor(TestPane.this).hasFocus()) {
                        ((Timer)e.getSource()).stop();
                        System.out.println("Pay attention!!");
                        application.requestUserAttention(true);
                    }
                }
            });
            time.setRepeats(true);
            time.setCoalesce(true);
            time.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

Ps确保你做对焦应用;)