📄 mytray.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package cn.haha.main;import java.awt.AWTException;import java.awt.Image;import java.awt.MenuItem;import java.awt.PopupMenu;import java.awt.SystemTray;import java.awt.TrayIcon;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import javax.swing.ImageIcon;import org.apache.log4j.Logger;/** * * @author tzy */public class MyTray { private TrayIcon trayIcon ; private SystemTray tray ; private MainFrame mainframe ; static Logger log = Logger.getLogger(MyTray.class.getName ());//日志输出对象 public MyTray(MainFrame mainframe){ this.mainframe = mainframe; initTray(); } private void initTray(){ if(SystemTray.isSupported()){ //检查当前系统是否支持系统托盘 tray = SystemTray.getSystemTray();//获取表示桌面托盘区的 SystemTray 实例。// Image image = Toolkit.getDefaultToolkit().getImage("D:\\koubei.png");// ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("qq3.png")); ImageIcon imageIcon = new ImageIcon("image/tray.png"); Image image = imageIcon.getImage(); PopupMenu popupMenu = new PopupMenu(); MenuItem exitItem = new MenuItem("关闭"); exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { try{ System.exit(0); }catch(Exception ex) { ex.printStackTrace(); } } }); popupMenu.add(exitItem); trayIcon = new TrayIcon(image, "自动回帖机", popupMenu); trayIcon.addMouseListener(new java.awt.event.MouseAdapter(){ public void mouseClicked(MouseEvent e) { if(e.getClickCount()==2){ showIT(true); } } }); try{ tray.add(trayIcon); // 将 TrayIcon 添加到 SystemTray。 } catch (AWTException e) { System.err.println(e); } }else{ System.out.println("你的系统不支持系统托盘"); } } public void tray(){ try { tray.remove(trayIcon); tray.add(trayIcon); // 将托盘图标添加到系统的托盘实例中 mainframe.setVisible(false); // 使窗口不可视 } catch (AWTException ex) { ex.printStackTrace(); } } /** * 浏览器从托盘返回桌面 * */ public void showIT(boolean visable) { if (mainframe.isVisible() != visable) mainframe.setVisible(visable); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -