public class SystemTray extends Object
SystemTray类代表桌面的系统托盘。
在Microsoft Windows上,它被称为“任务栏状态区”,在Gnome上将其称为“通知区域”,在KDE上称为“系统托盘”。
系统托盘由桌面上运行的所有应用程序共享。
在某些平台上,系统托盘可能不存在或可能不受支持,在这种情况下, getSystemTray()抛出UnsupportedOperationException 。 要检测系统托盘是否受支持,请使用isSupported() 。
SystemTray可能包含一个或多个TrayIcons ,它们使用add(java.awt.TrayIcon)方法添加到托盘中,并使用remove(java.awt.TrayIcon)删除不再需要的托盘 。 TrayIcon由图像,弹出菜单和一组关联的侦听器组成。 详情请参阅TrayIcon课程。
每个Java应用程序都有一个单独的SystemTray实例,允许应用程序在应用程序运行时与桌面的系统托盘接口。 SystemTray实例可以从getSystemTray()方法获得。 应用程序可能无法创建自己的SystemTray实例。
以下代码片段演示了如何访问和自定义系统托盘:
TrayIcon trayIcon = null; if (SystemTray.isSupported()) { // get the SystemTray instance SystemTray tray = SystemTray.getSystemTray(); // load an image Image image = Toolkit.getDefaultToolkit().getImage(...); // create a action listener to listen for default action executed on the tray icon ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { // execute default action of the application // ... } }; // create a popup menu PopupMenu popup = new PopupMenu(); // create menu item for the default action MenuItem defaultItem = new MenuItem(...); defaultItem.addActionListener(listener); popup.add(defaultItem); /// ... add other items // construct a TrayIcon trayIcon = new TrayIcon(image, "Tray Demo", popup); // set the TrayIcon properties trayIcon.addActionListener(listener); // ... // add the tray image try { tray.add(trayIcon); } catch (AWTException e) { System.err.println(e); } // ... } else { // disable tray option in your application or // perform other actions ... } // ... // some time later // the application state has changed - update the image if (trayIcon != null) { trayIcon.setImage(updatedImage); } // ...
TrayIcon
| Modifier and Type | Method and Description |
|---|---|
void |
add(TrayIcon trayIcon)
添加
TrayIcon到
SystemTray 。
|
void |
addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
将
PropertyChangeListener添加到特定属性的侦听器列表中。
|
PropertyChangeListener[] |
getPropertyChangeListeners(String propertyName)
返回与named属性相关联的所有侦听器的数组。
|
static SystemTray |
getSystemTray()
获取表示桌面托盘区域的
SystemTray实例。
|
TrayIcon[] |
getTrayIcons()
返回此应用程序添加到托盘中的所有图标的数组。
|
Dimension |
getTrayIconSize()
返回托盘图标在系统托盘中占用的空间大小(以像素为单位)。
|
static boolean |
isSupported()
返回当前平台是否支持系统托盘。
|
void |
remove(TrayIcon trayIcon)
删除指定的
TrayIcon从
SystemTray 。
|
void |
removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
从侦听器列表中删除特定属性的
PropertyChangeListener 。
|
public static SystemTray getSystemTray()
SystemTray实例。
这总是返回每个应用程序相同的实例。
在某些平台上可能不支持系统托盘。
您可以使用isSupported()方法来检查系统托盘是否受支持。
如果安装了SecurityManager,则必须授予AWTPermission accessSystemTray才能获取SystemTray实例。 否则这个方法会抛出一个SecurityException。
SystemTray实例
UnsupportedOperationException - 如果当前平台不支持系统托盘
HeadlessException - 如果
GraphicsEnvironment.isHeadless()返回
true
SecurityException -如果
accessSystemTray不被许可
add(TrayIcon) , TrayIcon , isSupported() , SecurityManager.checkPermission(java.security.Permission) , AWTPermission
public static boolean isSupported()
TrayIcon.setPopupMenu(PopupMenu) )或操作事件(请参阅TrayIcon.addActionListener(ActionListener) )。
开发人员不应该假定所有的系统托盘功能都被支持。 要保证托盘图标的默认操作始终可以访问,请将默认操作添加到动作侦听器和弹出菜单。 有关如何执行此操作的示例,请参阅example 。
注意 :实现SystemTray和TrayIcon时, 强烈建议您将不同的手势分配给弹出菜单和动作事件。 为两个目的重载手势是混乱的,可能会阻止用户访问一个或另一个。
false如果不支持系统托盘访问;
如果支持最小系统托盘访问,则此方法返回true ,但不保证当前平台支持所有系统托盘功能
getSystemTray()
public void add(TrayIcon trayIcon) throws AWTException
TrayIcon到SystemTray 。
添加后,系统托盘中的托盘图标就会显示。
图标在托盘中显示的顺序未指定 - 它与平台和实现相关。
由应用程序添加的所有图标都将自动从删除SystemTray应用程序退出时,也当桌面系统托盘不可用。
trayIcon - 要添加的
TrayIcon
NullPointerException - 如果
trayIcon是
null
IllegalArgumentException - 如果一个
TrayIcon的同一个实例被
TrayIcon添加
AWTException - 如果桌面系统托盘丢失
remove(TrayIcon) , getSystemTray() , TrayIcon , Image
public void remove(TrayIcon trayIcon)
TrayIcon从SystemTray 。
由应用程序添加的所有图标都将自动从删除SystemTray应用程序退出时,也当桌面系统托盘不可用。
如果trayIcon为null或未添加到系统托盘,则不会抛出异常并且不执行任何操作。
trayIcon - 要删除的
TrayIcon
add(TrayIcon) , TrayIcon
public TrayIcon[] getTrayIcons()
返回的数组是实际数组的副本,可以以任何方式修改而不影响系统托盘。 要从TrayIcon中删除SystemTray ,请使用remove(TrayIcon)方法。
add(TrayIcon) , TrayIcon
public Dimension getTrayIconSize()
TrayIcon类中有一个类似的方法TrayIcon.getSize() 。
TrayIcon.setImageAutoSize(boolean) , Image , TrayIcon.getSize()
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
PropertyChangeListener 。
目前支持以下属性:
Property Description trayIcons The SystemTray's array of TrayIcon objects. The array is accessed via the getTrayIcons() method.systemTray This property contains SystemTray instance when the system tray is available or null otherwise.getSystemTray() method.
listener仅在此上下文中监听属性更改。
如果listener为null ,则不会抛出异常并且不执行任何操作。
propertyName - 指定的属性
listener - 要添加的属性更改侦听器
removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener) ,
getPropertyChangeListeners(java.lang.String)
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
PropertyChangeListener 。
PropertyChangeListener必须来自这个上下文。
如果propertyName或listener为null或无效,则不会抛出异常并且不采取任何操作。
propertyName - 指定的属性
listener - 要删除的PropertyChangeListener
addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener) ,
getPropertyChangeListeners(java.lang.String)
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
只有在这个上下文中的监听器被返回。
propertyName - 指定的属性
PropertyChangeListener s与命名属性相关联;
如果没有添加此类侦听器,或者如果propertyName为null或无效,则返回一个空数组
addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener) ,
removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.