- java.lang.Object
-
- java.awt.SystemTray
-
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实例,允许应用程序在应用程序运行时与桌面的系统托盘进行连接。 可以从getSystemTray()方法获得SystemTray实例。 应用程序可能无法创建自己的SystemTray实例。以下代码片段演示了如何访问和自定义系统托盘:
TrayIcontrayIcon = null; if (SystemTray.isSupported()) { // get the SystemTray instance SystemTray tray = SystemTray.getSystemTray(); // load an imageImageimage =Toolkit.getDefaultToolkit().getImage(...); // create a action listener to listen for default action executed on the tray iconActionListenerlistener = newActionListener() { public voidactionPerformed(ActionEvente) { // execute default action of the application // ... } }; // create a popup menuPopupMenupopup = newPopupMenu(); // create menu item for the default action MenuItem defaultItem = new MenuItem(...); defaultItem.addActionListener(listener); popup.add(defaultItem); /// ... add other items // construct a TrayIcon trayIcon = newTrayIcon(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); } // ...- 从以下版本开始:
- 1.6
- 另请参见:
-
TrayIcon
-
-
方法摘要
所有方法 静态方法 接口方法 具体的方法 Modifier and Type 方法 描述 voidadd(TrayIcon trayIcon)添加TrayIcon到SystemTray。voidaddPropertyChangeListener(String propertyName, PropertyChangeListener listener)在特定属性的列表中添加一个PropertyChangeListener。PropertyChangeListener[]getPropertyChangeListeners(String propertyName)返回与named属性相关联的所有侦听器的数组。static SystemTraygetSystemTray()获取表示桌面托盘区域的SystemTray实例。TrayIcon[]getTrayIcons()返回此应用程序添加到托盘中的所有图标的数组。DimensiongetTrayIconSize()返回托盘图标在系统托盘中占用的空间大小(以像素为单位)。static booleanisSupported()返回当前平台是否支持系统托盘。voidremove(TrayIcon trayIcon)删除指定TrayIcon从SystemTray。voidremovePropertyChangeListener(String propertyName, PropertyChangeListener listener)从侦听器列表中删除特定属性的PropertyChangeListener。
-
-
-
方法详细信息
-
getSystemTray
public static SystemTray getSystemTray()
获取表示桌面托盘区域的SystemTray实例。 这总是返回每个应用程序相同的实例。 在某些平台上可能不支持系统托盘。 您可以使用isSupported()方法来检查系统托盘是否受支持。如果安装了SecurityManager,则必须授予
accessSystemTray才能获取SystemTray实例。 否则这个方法会抛出一个SecurityException。- 结果
-
表示桌面托盘区域的
SystemTray实例 - 异常
-
UnsupportedOperationException- 如果当前平台不支持系统托盘 -
HeadlessException- 如果GraphicsEnvironment.isHeadless()返回true -
SecurityException- 如果不授予accessSystemTray权限 - 另请参见:
-
add(TrayIcon),TrayIcon,isSupported(),SecurityManager.checkPermission(java.security.Permission),AWTPermission
-
isSupported
public static boolean isSupported()
返回当前平台是否支持系统托盘。 除了显示托盘图标,最小的系统托盘支持还包括弹出式菜单(参见TrayIcon.setPopupMenu(PopupMenu))或操作事件(请参阅TrayIcon.addActionListener(ActionListener))。开发人员不应该假定所有的系统托盘功能都被支持。 要确保托盘图标的默认操作始终可以访问,请将默认操作添加到动作侦听器和弹出菜单。 有关如何执行此操作的示例,请参阅
example。注意 :实现
SystemTray和TrayIcon, 强烈建议您将不同的手势分配给弹出菜单和动作事件。 为两个目的重载手势是混乱的,可能会阻止用户访问一个或另一个。- 结果
-
false如果不支持系统托盘访问; 如果支持最小系统托盘访问,则此方法返回true,但不保证当前平台支持所有系统托盘功能 - 另请参见:
-
getSystemTray()
-
add
public void add(TrayIcon trayIcon) throws AWTException
在TrayIcon添加一个SystemTray。 添加后,系统托盘中的托盘图标就会显示。 图标在托盘中显示的顺序未指定 - 它与平台和实现相关。在应用程序退出时,当桌面系统托盘不可用时,应用程序添加的所有图标都将自动从
SystemTray删除。- 参数
-
trayIcon- 要添加的TrayIcon - 异常
-
NullPointerException- 如果trayIcon是null -
IllegalArgumentException- 如果一个TrayIcon的同一个实例被TrayIcon添加 -
AWTException- 如果桌面系统托盘丢失 - 另请参见:
-
remove(TrayIcon),getSystemTray(),TrayIcon,Image
-
remove
public void remove(TrayIcon trayIcon)
删除指定TrayIcon从SystemTray。应用程序退出时,应用程序添加的所有图标将自动从
SystemTray删除,并且桌面系统托盘不可用时也会自动删除。如果
trayIcon为null或未添加到系统托盘,则不会抛出异常,也不会执行任何操作。- 参数
-
trayIcon- 要删除的TrayIcon - 另请参见:
-
add(TrayIcon),TrayIcon
-
getTrayIcons
public TrayIcon[] getTrayIcons()
返回此应用程序添加到托盘中的所有图标的数组。 您无法访问另一个应用程序添加的图标。 一些浏览器将不同代码库中的小程序分割成单独的上下文,并在这些上下文之间建立墙壁。 在这种情况下,只会返回从上下文添加的托盘图标。返回的数组是实际数组的副本,可以以任何方式修改而不影响系统托盘。 要从
TrayIcon删除SystemTray,请使用remove(TrayIcon)方法。- 结果
- 添加到此托盘的所有托盘图标的数组,如果没有添加,则为空数组
- 另请参见:
-
add(TrayIcon),TrayIcon
-
getTrayIconSize
public Dimension getTrayIconSize()
返回托盘图标在系统托盘中占用的空间大小(以像素为单位)。 在创建托盘图标之前,开发人员可以使用此方法获取托盘图标的图像属性的首选大小。 为方便起见,在TrayIcon类中有一个类似的方法TrayIcon.getSize()。- 结果
- 托盘图标的默认大小(以像素为单位)
- 另请参见:
-
TrayIcon.setImageAutoSize(boolean),Image,TrayIcon.getSize()
-
addPropertyChangeListener
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
在特定属性的侦听器列表中添加一个PropertyChangeListener。 目前支持以下属性: SystemTray properties Property DescriptiontrayIconsTheSystemTray's array ofTrayIconobjects. The array is accessed via thegetTrayIcons()method.
This property is changed when a tray icon is added to (or removed from) the system tray.
For example, this property is changed when the system tray becomes unavailable on the desktop
and the tray icons are automatically removed.systemTrayThis property containsSystemTrayinstance when the system tray is available ornullotherwise.
This property is changed when the system tray becomes available or unavailable on the desktop.
The property is accessed by thegetSystemTray()method.listener仅在此上下文中监听属性更改。如果
listener为null,则不会抛出异常,也不会执行任何操作。- 参数
-
propertyName- 指定的属性 -
listener- 要添加的属性更改侦听器 - 另请参见:
-
removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener),getPropertyChangeListeners(java.lang.String)
-
removePropertyChangeListener
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)
-
getPropertyChangeListeners
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
返回与named属性相关联的所有侦听器的数组。只有在这个上下文中的监听器被返回。
- 参数
-
propertyName- 指定的属性 - 结果
-
所有的
PropertyChangeListener与命名的属性相关联; 如果没有添加此类侦听器,或者如果propertyName为null或无效,则返回一个空数组 - 另请参见:
-
addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener),removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
-
-