gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:34:42 37 4
gpt4 key购买 nike

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

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

最佳答案

在 MacOS 下,尝试使用类似 Application#requestUserAttention(boolean) 的内容

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

nb-抱歉,我自己没有尝试过。

已更新示例

来自JavaDocs

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.

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

在 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);
}

}

}

请确保您确实关注应用程序;)

关于java - 如何让我的应用程序图标在 Mac Dock 中弹起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496763/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com