systemTray例子

阅读更多
package black.dragon.starttool;

import java.awt.AWTException;
import java.awt.Desktop;
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.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author whuang
 * 
 * @version 1.0, 2008-5-20 create
 */
public class DesktopTrayRun {
	private static Desktop desktop;
	private static SystemTray st;
	private static PopupMenu pm;

	public static void main(String[] args) {
		if (Desktop.isDesktopSupported()) {// 判断当前平台是否支持Desktop类
			desktop = Desktop.getDesktop();
		}
		if (SystemTray.isSupported()) {// 判断当前平台是否支持系统托盘
			st = SystemTray.getSystemTray();
			Image image = Toolkit.getDefaultToolkit().getImage("testdata/black_dragon1.png");// 定义托盘图标的图片
			createPopupMenu();
			TrayIcon ti = new TrayIcon(image, "Desktop Demo Tray", pm);
			try {
				st.add(ti);
			} catch (AWTException ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void sendMail(String mail) {
		if (desktop != null && desktop.isSupported(Desktop.Action.MAIL)) {
			try {
				desktop.mail(new URI(mail));
			} catch (IOException ex) {
				ex.printStackTrace();
			} catch (URISyntaxException ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void openBrowser(String url) {
		if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
			try {
				desktop.browse(new URI(url));
			} catch (IOException ex) {
				ex.printStackTrace();
			} catch (URISyntaxException ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void edit() {
		if (desktop != null && desktop.isSupported(Desktop.Action.EDIT)) {
			try {
				File txtFile = new File("test.txt");
				if (!txtFile.exists()) {
					txtFile.createNewFile();
				}
				desktop.edit(txtFile);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void createPopupMenu() {
		pm = new PopupMenu();
		MenuItem openBrowser = new MenuItem("Open My Blog");
		openBrowser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openBrowser("http://blog.csdn.net/chinajash");
			}
		});

		MenuItem sendMail = new MenuItem("Send Mail to me");
		sendMail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendMail("mailto:[email protected]");
			}
		});

		MenuItem edit = new MenuItem("Edit Text File");
		sendMail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				edit();
			}
		});

		MenuItem exitMenu = new MenuItem("&Exit");
		exitMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		pm.add(openBrowser);
		pm.add(sendMail);
		pm.add(edit);
		pm.addSeparator();
		pm.add(exitMenu);
	}

}

你可能感兴趣的:(Java,.net,JDK,Yahoo,Blog)