public class SystemTray extends Object
SystemTray
类别代表一个桌面系统托盘。在微软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)
返回已与已命名的属性关联的所有侦听器的数组。
|
static SystemTray |
getSystemTray()
获取表示桌面托盘区的
SystemTray 实例。
|
TrayIcon[] |
getTrayIcons()
通过此应用程序返回添加到托盘中的所有图标的数组。
|
Dimension |
getTrayIconSize()
返回一个托盘图标在系统托盘中占据的空间的大小,以像素为单位。
|
static boolean |
isSupported()
返回当前平台上是否支持系统托盘的是否支持。
|
void |
remove(TrayIcon trayIcon)
从
SystemTray 移除指定的
TrayIcon 。
|
void |
removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
删除一个
PropertyChangeListener 从监听器列表的特定属性。
|
public static SystemTray getSystemTray()
SystemTray
实例。这总是返回每个应用程序的同一个实例。在某些平台上,系统托盘可能不被支持。你可以使用
isSupported()
方法检查系统托盘支持。
如果要安装,必须授予的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
加以补充
null
trayIcon
NullPointerException
IllegalArgumentException
-如果一个
TrayIcon
相同的实例加入不止一次
AWTException
如果桌面系统盘丢失
remove(TrayIcon)
,
getSystemTray()
,
TrayIcon
,
Image
public void remove(TrayIcon trayIcon)
SystemTray
移除指定的
TrayIcon
。
所有的图标添加到应用程序会自动从SystemTray
删除应用程序退出时,也当桌面系统托盘不可用。
如果trayIcon
是null
或不添加到系统托盘,不引发异常,则不执行行动。
trayIcon
-
TrayIcon
被删除
add(TrayIcon)
,
TrayIcon
public TrayIcon[] getTrayIcons()
返回的数组是一个实际数组的副本,并可以以任何方式修改,而不影响系统托盘。从SystemTray
删除TrayIcon
,使用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 | 描述 |
---|---|
trayIcons |
The SystemTray 's array of TrayIcon objects. The array is accessed via the getTrayIcons() 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. |
systemTray |
This property contains SystemTray instance when the system tray is available or null otherwise.This property is changed when the system tray becomes available or unavailable on the desktop. The property is accessed by the 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.