import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class TrayTest
{
private TrayIcon trayIcon;
public TrayTest()
{
if (SystemTray.isSupported()) {
Image image = Toolkit.getDefaultToolkit().getImage("c:/temp/0.jpg");
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "notepad", popup);
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("notepad.exe");
} catch (IOException e1) {
trayIcon.displayMessage("Action Event",
"notepad is not found !",
TrayIcon.MessageType.INFO);
}
}
});
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e1) {
e1.printStackTrace();
}
} else {
System.err.println("System tray is currently not supported.");
}
}
public static void main(String[] args) throws Exception
{
new TrayTest();
}
}
댓글 없음:
댓글 쓰기