14. 13. 6. 系统托盘使用 Using system tray

 

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 javax.swing.JOptionPane;

public class SystemTrayIcon {

	public static void main(String[] args) throws Exception{
		if(!SystemTray.isSupported()){
			System.out.println("系统不支持!");
			return;
		}
		
		SystemTray tray = SystemTray.getSystemTray();
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("E:\\java\\Test\\icon\\44.png");
		
		PopupMenu menu = new PopupMenu();//右键菜单
		MenuItem messageItem = new MenuItem("显示消息");
		messageItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "http://blog.csdn.com/youyigong");
			}
		});
		
		MenuItem closeMenu = new MenuItem("关闭退出");
		closeMenu.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		menu.add(messageItem);
		menu.add(closeMenu);
		TrayIcon icon = new TrayIcon(image,"系统图标演示",menu);
		icon.setImageAutoSize(true);
		
		tray.add(icon);
	}
}


 

你可能感兴趣的:(exception,String,image,System,import,menu)