好吧,我正在用 Java 编写一个 IRC 客户端,我想知道是否有办法让我的应用程序的图标在触发 nickalert(或任何其他相关通知)时在 Dock 中反弹。
如果 Windows 也有某种通知系统,我也想知道它。
在 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 确保您专注于应用程序;)