原文的出处:
[url]http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/[/url]
我并没有全文照译.其中介绍什么是托盘的部分略去了
在译后,加了一个自己写的例子.
访问系统托盘
类java.awt.SystemTray代表了桌面的系统托盘.你可以通过静态方法SystemTray.getSystemTray()来进行访问.但是,在这之前,你的程序应该用静态方法SystemTray.isSupported()来测试一下系统是否支持托盘.如果系统托盘不存在或者系统不支持托盘,isSupported()将返回flase.在这种情况下调用getSysemTray的话,它将抛出java.lang.UnsupportedOperationException.
每个java应用程序都只有一个单一的SystemTray实例.所以,一个程序不能创建只属于它自己的SystemTray实例;而它必须用getSystemTray()方法来取得已有的那个.
SystemTray包含有一个或多个TrayIcon.通过SystemTray的方法add(java.awt.TrayIcon)方法可以添加一个TrayIcon到托盘,通过remove(java.awt.TrayIcon)将一个TrayIcon从托盘移除.注意最近添加的测试代码指明,如操作系统或者Java runtime不添加TrayIcon到托盘,add()方法可能会抛出AWTException.例如,在X-Windows中,如果托盘不存在,它将抛出AWTException
下面的代码演示了如果访问系统托盘:
final
TrayIcon trayIcon;
if
(SystemTray.isSupported()) {
SystemTray tray
=
SystemTray.getSystemTray();
Image image
=
Toolkit.getDefaultToolkit().getImage(
"
tray.gif
"
);
MouseListener mouseListener
=
new
MouseListener() {
public
void
mouseClicked(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse clicked!
"
);
}
public
void
mouseEntered(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse entered!
"
);
}
public
void
mouseExited(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse exited!
"
);
}
public
void
mousePressed(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse pressed!
"
);
}
public
void
mouseReleased(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse released!
"
);
}
};
ActionListener exitListener
=
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
System.out.println(
"
Exiting...
"
);
System.exit(
0
);
}
};
PopupMenu popup
=
new
PopupMenu();
MenuItem defaultItem
=
new
MenuItem(
"
Exit
"
);
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);
trayIcon
=
new
TrayIcon(image,
"
Tray Demo
"
, popup);
ActionListener actionListener
=
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
trayIcon.displayMessage(
"
Action Event
"
,
"
An Action Event Has Been Peformed!
"
,
TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(
true
);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
try
{
tray.add(trayIcon);
}
catch
(AWTException e) {
System.err.println(
"
TrayIcon could not be added.
"
);
}
}
else
{
//
不支持系统托盘
}
实现Tray Icon
系统托盘允许一个或多个java.awt.TrayIcon添加到它或从它移除:一个TrayIcon对象代表着一个可以添加到系统托盘的托盘图标.但是,TrayIcon的功能不止是一个显示在托盘里的图标.它还可以有工具提示(tooltip),有一个AWT的弹出菜单,还有一系列相关的侦听器
一个TrayIcon会产生各种各样的MouseEvent(pressed,released,和clicked).可以对它添加相关的侦听器去接收这些事件的通知.但是要注意,在MouseEvent从TrayIcon接收到的坐标是相对于屏幕而不是TrayIcon它自己的.TrayIcon它自己处理这些事件.例如,默认情况下,当右击TrayIcon时,和它相关的弹出菜单就会显示.当鼠标在它上面停留时,工具提示就会出现.TrayIcon也可以产生ActionEvent.在一些平台上,ActionEvent产生于用户通过鼠标或键盘选择该TrayIcon时.一个实现良好的TrayIcon应该用不同的方法去显示弹出菜单和选择托盘图标.
注意,根据最新的javadocs,当一个鼠标事件被发送到它的侦听器时,它的component属性(通过父类ComponentEvent.getComponent()获取)将会被设置为null.而source属性(通过EventObject.getSource()获取)将被设置为该TrayIcon
有用的特点
用setImage()方法可以在以后更新TrayIcon的图标:
trayIcon.setImage(updatedImage);
同样你可以更新工具提示:
trayIcon.setTooltip("I'm busy. Go away.");
TrayIcon有一个非常有用的自动调整大小的属性.这个属性决定是否自动调整图片的大小以适应托盘的大小.默认这个属性是false的.如果图片的大小不适合托盘的大小,图片仍会以它原来的大小绘制到托盘上.也就是说,如果图片比分配的托盘空间大,图片将被剪切.另一方面,如果自动调整大小被设为true,那么图片将伸缩以适应托盘图标的大小
trayIcon.setImageAutoSize(true);
最后,如果你想偶尔用工具提示通知用户程序的一些变化,你可以用displayMessage()方法.这个方法弹出一个在托盘图标上弹出一个消息.这个消息会在一段时间之后,或者用户单击它之后消失.单击它的时候,根据系统的情况,会产生ActionEvent
trayIcon.displayMessage("Finished downloading",
"Your Java application has finished downloading",
TrayIcon.MessageType.INFO);
第一个参数是标题.它通常在弹出消息里会以粗体显示.第二个参数是要显示的消息.最后,最后一个参数是TrayIcon的一个枚举类型TrayIcon.Message,有四个选择.这些选项可以用作决定在显示消息时,是否应执行某些图形操作或其他系统动作.四个选择如下:
TrayIcon.MessageType.ERROR An error message
TrayIcon.MessageType.INFO An information message
TrayIcon.MessageType.NONE A simple message
TrayIcon.MessageType.WARNING A warning message
===================================================
下面是我自己写的例子
package
blog.swing;
import
java.awt.event.ActionListener;
import
java.awt.event.ActionEvent;
import
java.awt.event.MouseListener;
import
java.awt.event.MouseEvent;
import
java.awt.PopupMenu;
import
java.awt.MenuItem;
import
java.awt.Image;
import
java.awt.AWTException;
import
java.awt.Toolkit;
import
java.awt.SystemTray;
import
java.awt.TrayIcon;
import
java.text.SimpleDateFormat;
import
java.util.Calendar;
class
SystemTrayDemo
{
SystemTray systemTray;
TrayIcon trayIcon;
Image trayImage;
String trayTip;
PopupMenu trayPopupMenu;
MenuItem trayMenuItem;
ActionListener trayActionListener;
ActionListener menuActionListener;
MouseListener trayMouseListener;
Thread displayTime;
public
SystemTrayDemo() {
if
( SystemTray.isSupported() ){
trayMouseListener
=
new
MouseListener() {
public
void
mouseClicked(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse clicked!
"
);
}
public
void
mouseEntered(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse entered!
"
);
}
public
void
mouseExited(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse exited!
"
);
}
public
void
mousePressed(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse pressed!
"
);
}
public
void
mouseReleased(MouseEvent e) {
System.out.println(
"
Tray Icon - Mouse released!
"
);
}
};
trayActionListener
=
new
ActionListener(){
public
void
actionPerformed( ActionEvent e ){
System.out.println(
"
Tray Icon - Action event happened!
"
);;
}
};
menuActionListener
=
new
ActionListener(){
public
void
actionPerformed( ActionEvent e ){
System.exit(
0
);
}
};
trayPopupMenu
=
new
PopupMenu();
trayMenuItem
=
new
MenuItem(
"
exit
"
);
trayMenuItem.addActionListener( menuActionListener );
trayPopupMenu.add( trayMenuItem );
trayTip
=
"
This is a SystemTray Demo
"
;
trayImage
=
Toolkit.getDefaultToolkit().getImage(
"
images/leaf.gif
"
);
trayIcon
=
new
TrayIcon( trayImage,trayTip,trayPopupMenu );
trayIcon.addActionListener( trayActionListener );
trayIcon.addMouseListener( trayMouseListener );
trayIcon.setImageAutoSize(
true
);
systemTray
=
SystemTray.getSystemTray();
try
{
systemTray.add( trayIcon );
}
catch
( AWTException e ){
e.printStackTrace();
}
}
else
{
System.out.println(
"
SystemTray unsupported!
"
);
}
displayTime
=
new
Thread(
new
Runnable(){
public
void
run(){
Calendar now;
SimpleDateFormat sdf
=
new
SimpleDateFormat(
"
yyyy-MM-dd hh:mm:ss
"
);
String time;
while
(
true
){
now
=
Calendar.getInstance();
time
=
sdf.format( now.getTime() );
trayIcon.displayMessage(
"
报时
"
,time,TrayIcon.MessageType.INFO);
try
{
Thread.sleep(
60000
);
}
catch
( InterruptedException e ){
e.printStackTrace();
}
}
}
});
displayTime.start();
}
public
static
void
main(String[] args)
{
new
SystemTrayDemo();
}
}