下面列出了java.awt.SystemTray#getSystemTray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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)));
}
}
void setTray() {
if (!Config.getInstance().getBoolean(Config.MAIN_TRAY)) {
this.removeTray();
return;
}
if (!SystemTray.isSupported()) {
LOGGER.info("tray icon not supported");
return;
}
if (mTrayIcon == null)
mTrayIcon = this.createTrayIcon();
SystemTray tray = SystemTray.getSystemTray();
if (tray.getTrayIcons().length > 0)
return;
try {
tray.add(mTrayIcon);
} catch (AWTException ex) {
LOGGER.log(Level.WARNING, "can't add tray icon", ex);
}
}
private void enableTray(final Stage stage) {
try {
ContextMenu menu = new ContextMenu();
MenuItem updateMenuItem = new MenuItem();
MenuItem showMenuItem = new MenuItem();
showMenuItem.setText("查看群信息");
updateMenuItem.setText("修改群信息");
menu.getItems().add(showMenuItem);
menu.getItems().add(updateMenuItem);
menu.getItems().add(new MenuItem("好好的事实的话"));
menu.getItems().add(new MenuItem("好好的事实的话"));
menu.getItems().add(new MenuItem("好好的事实的话"));
menu.getItems().add(new MenuItem("好好的事实的话"));
menu.getItems().add(new MenuItem("好好的事实的话"));
SystemTray tray = SystemTray.getSystemTray();
BufferedImage image = ImageIO.read(OnlyTrayIconDemo.class.getResourceAsStream("tray.png"));
// Image image = new
// ImageIcon("Resources/Images/Logo/logo_16.png").getImage();
trayIcon = new OnlyTrayIcon(image, "自动备份工具");
trayIcon.setToolTip("自动备份工具");
trayIcon.setContextMenu(menu);
tray.add(trayIcon);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initTray() {
try {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
tray.add(trayIcon);
}
} catch (AWTException ex) {
ex.printStackTrace();
}
}
private void start(String[] args) {
SystemTray tray = SystemTray.getSystemTray();
TrayIcon icon = new TrayIcon(Toolkit.getDefaultToolkit().createImage(
getClass().getResource(
"/com/ramussoft/gui/server/application.png")),
getString("Server") + " " + Metadata.getApplicationName(),
createPopupMenu());
icon.setImageAutoSize(true);
try {
tray.add(icon);
} catch (AWTException e) {
e.printStackTrace();
}
}
public static void setupTray() throws AWTException {
final SystemTray tray = SystemTray.getSystemTray();
final PopupMenu popup = new PopupMenu();
final MenuItem info = new MenuItem();
final MenuItem exit = new MenuItem();
final TrayIcon trayIcon = new TrayIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), "MLGA", popup);
try {
InputStream is = FileUtil.localResource("icon.png");
trayIcon.setImage(ImageIO.read(is));
is.close();
} catch (IOException e1) {
e1.printStackTrace();
}
info.addActionListener(e -> {
String message = "Double-Click to lock/unlock the overlay for dragging";
JOptionPane.showMessageDialog(null, message, "Information", JOptionPane.INFORMATION_MESSAGE);
});
exit.addActionListener(e -> {
running = false;
tray.remove(trayIcon);
ui.close();
System.out.println("Terminated UI...");
System.out.println("Cleaning up system resources. Could take a while...");
handle.close();
System.out.println("Killed handle.");
System.exit(0);
});
info.setLabel("Help");
exit.setLabel("Exit");
popup.add(info);
popup.add(exit);
tray.add(trayIcon);
}
private void hideTrayIcon() {
SystemTray tray = SystemTray.getSystemTray();
if (tray != null) {
try {
tray.remove(trayIcon);
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
}
trayIcon = null;
}
/**
* Create tray icon.
*
* @param icon the icon
* @param title the title
* @param frame the frame
* @return the tray icon
*/
@Nullable
public static TrayIcon createTrayIcon(@Nonnull final Image icon, @Nonnull final String title, @Nonnull final Frame frame)
{
if (!SystemTray.isSupported())
{
return null;
}
final SystemTray systemTray = SystemTray.getSystemTray();
final TrayIcon trayIcon = new TrayIcon(icon, title);
trayIcon.setImageAutoSize(true);
try
{
systemTray.add(trayIcon);
}
catch (AWTException ex)
{
log.debug("Unable to add system tray icon", ex);
return trayIcon;
}
// Bring to front when tray icon is clicked
trayIcon.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
frame.setVisible(true);
frame.setState(Frame.NORMAL); // Restore
}
});
return trayIcon;
}
public static void showTrayMessage(String title, String message) {
try {
SystemTray tray = SystemTray.getSystemTray();
BufferedImage image = ImageIO.read(AlertMaker.class.getResource(LibraryAssistantUtil.ICON_IMAGE_LOC));
TrayIcon trayIcon = new TrayIcon(image, "Library Assistant");
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("Library Assistant");
tray.add(trayIcon);
trayIcon.displayMessage(title, message, MessageType.INFO);
tray.remove(trayIcon);
} catch (Exception exp) {
exp.printStackTrace();
}
}
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);
}
}
public static void removeTrayIcon() {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
if (trayIcon != null) {
tray.remove(trayIcon);
trayIcon = null;
}
}
}
@Override
public void shutdown() {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
tray.remove(trayIcon);
}
ChatManager.getInstance().removeChatMessageHandler(chatMessageHandler);
}
/**
* Tray icon handler.
*/
private void trayIconHandler(InputStream iconInputStream) throws IOException, AWTException {
// set up the system tray
var tray = SystemTray.getSystemTray();
var image = ImageIO.read(iconInputStream);
// use the loaded icon as tray icon
var trayIcon = new TrayIcon(image);
// show the primary stage if the icon is right clicked
trayIcon.addActionListener(event -> Platform.runLater(this::showStage));
// add app name as a menu item
var openItem = new MenuItem(APPLICATION_TITLE);
// show the primary stage if the app name item is clicked
openItem.addActionListener(event -> Platform.runLater(this::showStage));
// add Preferences menu item
var settingItem = new MenuItem("Preferences");
settingItem.addActionListener(event -> Platform.runLater(() -> UIUtils.showPreferencesDialog(0)));
// add check for updates menu item
var updateCheckItem = new MenuItem("Check for Updates");
// add current version info menu item
var versionItem = new MenuItem("Version: " + PROPERTIES.getProperty("version"));
// add click action listener
updateCheckItem.addActionListener(event -> {
try {
Desktop.getDesktop().browse(new URI(IOUtils.GITHUB_RELEASES_URL));
} catch (IOException | URISyntaxException ignored) {
}
});
// add quit option as the app cannot be closed by clicking the window close button
var exitItem = new MenuItem("Quit");
// add action listener for cleanup
exitItem.addActionListener(event -> {
// remove the icon
tray.remove(trayIcon);
Platform.exit();
System.exit(0);
});
// set up the popup menu
final var popup = new PopupMenu();
popup.add(openItem);
popup.addSeparator();
popup.add(settingItem);
popup.addSeparator();
popup.add(versionItem);
popup.add(updateCheckItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
// add icon to the system
tray.add(trayIcon);
}
void removeTray() {
if (mTrayIcon != null) {
SystemTray tray = SystemTray.getSystemTray();
tray.remove(mTrayIcon);
}
}