Java系统托盘

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;

public class TrayTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String path = "icons/out1.jpg";
		System.out.println(path);
		Image image = Toolkit.getDefaultToolkit().getImage(
				path);
		PopupMenu popupTi = new PopupMenu();// 弹出菜单
		MenuItem showItem = new MenuItem("Exit");// 菜单项
		popupTi.add(showItem);
		showItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
			
		});
		TrayIcon ti = new TrayIcon(image, "Java系统托盘", popupTi);// 图标,标题,右键弹出菜单
		SystemTray tray = SystemTray.getSystemTray();
		try {
			tray.add(ti);// 在系统托盘区中增加图标
		} catch (AWTException e) {
			System.err.println(e);
		}
	}

}

 

你可能感兴趣的:(java)