如何让我的应用程序图标在Mac Dock中弹跳 [英] How to make my app icon bounce in the Mac dock

查看:180
本文介绍了如何让我的应用程序图标在Mac Dock中弹跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Well I'm writing an IRC client in Java and I was wondering if there was a way to make my app's icon bounce in the dock when a nickalert is triggered (or any other relevant notification).

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

If Windows also has some sort of notification system I'd like to know about it as well.

推荐答案

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

Under the MacOS, try using something like Application#requestUserAttention(boolean)

import com.apple.eawt.Application;
...
Application application = Application.getApplication();
application.requestUserAttention(false);

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

nb- I've not tried this my self - sorry.

更新了示例

来自 JavaDocs


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

Requests user attention to this application (usually through bouncing the Dock icon). Critical requests will continue to bounce the Dock icon until the app is activated. An already active application requesting attention does nothing.

这意味着,如果应用程序具有焦点,那么方法将什么都不做。

That means, that if the application has focus, then the method will do nothing.

在Mac OSX 10.7.5上测试,Java 1.7.0_07

Test on 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确保你做对焦应用;)

Ps make sure that you do-focus application ;)

这篇关于如何让我的应用程序图标在Mac Dock中弹跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆