下面列出了怎么用java.awt.TrayIcon的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
synchronized void updateNativeImage(Image image) {
if (isDisposed())
return;
boolean autosize = ((TrayIcon)target).isImageAutoSize();
BufferedImage bufImage = new BufferedImage(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bufImage.createGraphics();
if (gr != null) {
try {
gr.setPaintMode();
gr.drawImage(image, 0, 0, (autosize ? TRAY_ICON_WIDTH : image.getWidth(observer)),
(autosize ? TRAY_ICON_HEIGHT : image.getHeight(observer)), observer);
createNativeImage(bufImage);
updateNativeIcon(!firstUpdate);
if (firstUpdate) firstUpdate = false;
} finally {
gr.dispose();
}
}
}
private void setUpTrayIcon() {
if (SystemTray.isSupported()) {
//trayIcon.setImageAutoSize(true); It renders the icon very poorly.
//So we render the icon ourselves with smooth settings.
{
Dimension d = SystemTray.getSystemTray().getTrayIconSize();
trayIcon = new TrayIcon(getIconImage().getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH));
}
//trayIcon = new TrayIcon(getIconImage());
//trayIcon.setImageAutoSize(true);
trayIcon.setToolTip(Translation.T().trayIconToolTip());
trayIcon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
NULogger.getLogger().info("System tray double clicked");
setExtendedState(JFrame.NORMAL);
setVisible(true);
repaint();
SystemTray.getSystemTray().remove(trayIcon);
}
});
}
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
synchronized void updateNativeImage(Image image) {
if (isDisposed())
return;
boolean autosize = ((TrayIcon)target).isImageAutoSize();
BufferedImage bufImage = new BufferedImage(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bufImage.createGraphics();
if (gr != null) {
try {
gr.setPaintMode();
gr.drawImage(image, 0, 0, (autosize ? TRAY_ICON_WIDTH : image.getWidth(observer)),
(autosize ? TRAY_ICON_HEIGHT : image.getHeight(observer)), observer);
createNativeImage(bufImage);
updateNativeIcon(!firstUpdate);
if (firstUpdate) firstUpdate = false;
} finally {
gr.dispose();
}
}
}
private void initTray() throws AWTException {
if (SystemTray.isSupported()) {
// 获得系统托盘对象
SystemTray systemTray = SystemTray.getSystemTray();
// 获取图片所在的URL
URL url = Thread.currentThread().getContextClassLoader().getResource(ICON_PATH);
// 为系统托盘加托盘图标
Image trayImage = Toolkit.getDefaultToolkit().getImage(url);
Dimension trayIconSize = systemTray.getTrayIconSize();
trayImage = trayImage.getScaledInstance(trayIconSize.width, trayIconSize.height, Image.SCALE_SMOOTH);
trayIcon = new TrayIcon(trayImage, "Proxyee Down");
systemTray.add(trayIcon);
loadPopupMenu();
//双击事件监听
trayIcon.addActionListener(event -> Platform.runLater(() -> loadUri(null, true)));
}
}
private void sendNotification(
final String title,
final String message,
final TrayIcon.MessageType type)
{
final String escapedTitle = SHELL_ESCAPE.escape(title);
final String escapedMessage = SHELL_ESCAPE.escape(message);
switch (OSType.getOSType())
{
case Linux:
sendLinuxNotification(escapedTitle, escapedMessage, type);
break;
case MacOS:
sendMacNotification(escapedTitle, escapedMessage);
break;
default:
sendTrayNotification(title, message, type);
}
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
synchronized void updateNativeImage(Image image) {
if (isDisposed())
return;
boolean autosize = ((TrayIcon)target).isImageAutoSize();
BufferedImage bufImage = new BufferedImage(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bufImage.createGraphics();
if (gr != null) {
try {
gr.setPaintMode();
gr.drawImage(image, 0, 0, (autosize ? TRAY_ICON_WIDTH : image.getWidth(observer)),
(autosize ? TRAY_ICON_HEIGHT : image.getHeight(observer)), observer);
createNativeImage(bufImage);
updateNativeIcon(!firstUpdate);
if (firstUpdate) firstUpdate = false;
} finally {
gr.dispose();
}
}
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, () -> {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
WPopupMenuPeer peer = AWTAccessor.getMenuComponentAccessor()
.getPeer(popup);
peer.show(popupParent, new Point(x, y));
}
});
}
synchronized void updateNativeImage(Image image) {
if (isDisposed())
return;
boolean autosize = ((TrayIcon)target).isImageAutoSize();
BufferedImage bufImage = new BufferedImage(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bufImage.createGraphics();
if (gr != null) {
try {
gr.setPaintMode();
gr.drawImage(image, 0, 0, (autosize ? TRAY_ICON_WIDTH : image.getWidth(observer)),
(autosize ? TRAY_ICON_HEIGHT : image.getHeight(observer)), observer);
createNativeImage(bufImage);
updateNativeIcon(!firstUpdate);
if (firstUpdate) firstUpdate = false;
} finally {
gr.dispose();
}
}
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
synchronized void updateNativeImage(Image image) {
if (isDisposed())
return;
boolean autosize = ((TrayIcon)target).isImageAutoSize();
BufferedImage bufImage = new BufferedImage(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bufImage.createGraphics();
if (gr != null) {
try {
gr.setPaintMode();
gr.drawImage(image, 0, 0, (autosize ? TRAY_ICON_WIDTH : image.getWidth(observer)),
(autosize ? TRAY_ICON_HEIGHT : image.getHeight(observer)), observer);
createNativeImage(bufImage);
updateNativeIcon(!firstUpdate);
if (firstUpdate) firstUpdate = false;
} finally {
gr.dispose();
}
}
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
synchronized void updateNativeImage(Image image) {
if (isDisposed())
return;
boolean autosize = ((TrayIcon)target).isImageAutoSize();
BufferedImage bufImage = new BufferedImage(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bufImage.createGraphics();
if (gr != null) {
try {
gr.setPaintMode();
gr.drawImage(image, 0, 0, (autosize ? TRAY_ICON_WIDTH : image.getWidth(observer)),
(autosize ? TRAY_ICON_HEIGHT : image.getHeight(observer)), observer);
createNativeImage(bufImage);
updateNativeIcon(!firstUpdate);
if (firstUpdate) firstUpdate = false;
} finally {
gr.dispose();
}
}
}
synchronized void updateNativeImage(Image image) {
if (isDisposed())
return;
boolean autosize = ((TrayIcon)target).isImageAutoSize();
BufferedImage bufImage = new BufferedImage(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bufImage.createGraphics();
if (gr != null) {
try {
gr.setPaintMode();
gr.drawImage(image, 0, 0, (autosize ? TRAY_ICON_WIDTH : image.getWidth(observer)),
(autosize ? TRAY_ICON_HEIGHT : image.getHeight(observer)), observer);
createNativeImage(bufImage);
updateNativeIcon(!firstUpdate);
if (firstUpdate) firstUpdate = false;
} finally {
gr.dispose();
}
}
}
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
private TrayIcon createTrayIcon() {
Image image = createTrayImage();
String tooltip = createTrayTooltip();
trayPopup = createTrayPopup();
TrayIcon icon = new TrayIcon(image, tooltip, trayPopup);
icon.setImageAutoSize(true);
icon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (trayPopup.isEnabled()) toggleWindowVisibility();
}
});
}
});
return icon;
}
@Subscribe
private void onPlayerSpawned(PlayerSpawned event)
{
final Player p = event.getPlayer();
if (config.notifyPkers() && p != null && p != client.getLocalPlayer()
&& PvPUtil.isAttackable(client, p) && !client.isFriended(p.getName(), false)
&& !friendChatManager.isMember(p.getName()))
{
String playerName = p.getName();
int combat = p.getCombatLevel();
notifier.notify("PK'er warning! A level " + combat + " player named " + playerName +
" appeared!", TrayIcon.MessageType.WARNING);
}
}
@Override
public void updateImage() {
Image image = ((TrayIcon)target).getImage();
if (image != null) {
updateNativeImage(image);
}
}
private void sendTrayNotification(
final String title,
final String message,
final TrayIcon.MessageType type)
{
if (clientUI.getTrayIcon() != null)
{
clientUI.getTrayIcon().displayMessage(title, message, type);
}
}
@Override
public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
if (image != ((TrayIcon)target).getImage() || // if the image has been changed
isDisposed())
{
return false;
}
if ((flags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS |
ImageObserver.WIDTH | ImageObserver.HEIGHT)) != 0)
{
updateNativeImage(image);
}
return (flags & ImageObserver.ALLBITS) == 0;
}
public static void sendNotification(String title, String bodyText) {
TrayIcon[] trayIcon = SystemTray.getSystemTray().getTrayIcons();
if (trayIcon.length == 1) {
trayIcon[0].displayMessage(title, bodyText, TrayIcon.MessageType.INFO);
}
}
@Override
public void updateImage() {
Image image = ((TrayIcon)target).getImage();
if (image != null) {
updateNativeImage(image);
}
}
private TrayIcon create(Image image) throws IOException {
TrayIcon trayIcon = new java.awt.TrayIcon(image);
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip(tooltip);
trayIcon.setPopupMenu(createMenu(false));
return trayIcon;
}
@Override
public void updateImage() {
Image image = ((TrayIcon)target).getImage();
if (image != null) {
updateNativeImage(image);
}
}
@Override
public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
if (image != ((TrayIcon)target).getImage() || // if the image has been changed
isDisposed())
{
return false;
}
if ((flags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS |
ImageObserver.WIDTH | ImageObserver.HEIGHT)) != 0)
{
updateNativeImage(image);
}
return (flags & ImageObserver.ALLBITS) == 0;
}
public void setIcon(boolean color) {
SystemTray tray = SystemTray.getSystemTray();
TrayIcon[] icons = tray.getTrayIcons();
if (icons.length > 0) {
String name = color ? "favicon-green.png" : "favicon-red.png";
URL url = getClass().getClassLoader().getResource(name);
Image image = Toolkit.getDefaultToolkit().getImage(url);
int trayWidth = tray.getTrayIconSize().width;
int trayHeight = tray.getTrayIconSize().height;
Image scaled = image.getScaledInstance(trayWidth, trayHeight, Image.SCALE_SMOOTH);
icons[0].setImage(scaled);
}
}
@Override
public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
if (image != ((TrayIcon)target).getImage() || // if the image has been changed
isDisposed())
{
return false;
}
if ((flags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS |
ImageObserver.WIDTH | ImageObserver.HEIGHT)) != 0)
{
updateNativeImage(image);
}
return (flags & ImageObserver.ALLBITS) == 0;
}