java程序 实现右下角系统托盘图标显示 后台运行

java | 2019-09-20 12:53:55

qq,微信都能关闭窗口,然后在右下角托盘区显示小图标,我刚好想做一个截图工具,也想实现java托盘区显示小图标的方法。

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;
 
import javax.swing.JFrame;
 
 
public class MySystemTray extends JFrame{
	
	public MySystemTray() {
		init();
	}
	
	public void init() {
		
		this.setSize(300, 200);
		this.setLocationRelativeTo(null);
		this.setTray();
		this.setVisible(true);
	}
	
	//添加托盘显示:1.先判断当前平台是否支持托盘显示
	public void setTray() {
		
		if(SystemTray.isSupported()){//判断当前平台是否支持托盘功能
			//创建托盘实例
			SystemTray tray = SystemTray.getSystemTray();
			//创建托盘图标:1.显示图标Image 2.停留提示text 3.弹出菜单popupMenu 4.创建托盘图标实例
			//1.创建Image图像
			Image image = Toolkit.getDefaultToolkit().getImage("trayIconImage/clientIcon.jpg");
			//2.停留提示text
			String text = "MySystemTray";
			//3.弹出菜单popupMenu
			PopupMenu popMenu = new PopupMenu();
			MenuItem itmOpen = new MenuItem("打开");
			itmOpen.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					Show();
				}				
			});
			MenuItem itmHide = new MenuItem("隐藏");
			itmHide.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					UnVisible();
				}
			});
			MenuItem itmExit = new MenuItem("退出");
			itmExit.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					Exit();
				}
			});
			popMenu.add(itmOpen);
			popMenu.add(itmHide);
			popMenu.add(itmExit);
			
			//创建托盘图标
			TrayIcon trayIcon = new TrayIcon(image,text,popMenu);
			//将托盘图标加到托盘上
			try {
				tray.add(trayIcon);
			} catch (AWTException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	//内部类中不方便直接调用外部类的实例(this不能指向)
	public void UnVisible() {
		this.setVisible(false);
	}
	public void Show() {
		this.setVisible(true);
	}
	public void Exit() {
		System.exit(0);
	}
	
	
	public static void main(String[] args) {
		new MySystemTray();
	}
}

 

登录后即可回复 登录 | 注册
    
关注编程学问公众号