java开发_系统托盘
项目结构:
运行效果:
========================================================================
下面是代码部分:
========================================================================
/tray/src/com/b510/tray托盘/DesktopCapture.java
1 package com.b510.tray托盘; 2 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.awt.event.WindowAdapter; 6 import java.awt.event.WindowEvent; 7 8 import javax.swing.JFrame; 9 import javax.swing.JOptionPane; 10 import javax.swing.UIManager; 11 12 public class DesktopCapture extends JFrame implements ActionListener { 13 14 /** 15 * 1.改变双击托盘截图为单击托盘截图 2.修复了托盘后无反应BUG 3.将放大镜提示框移至左上角和右上角 16 */ 17 private static final long serialVersionUID = 1L; 18 // JButton confirm; 19 // BufferedImage desktopImg; 20 MyTray tray; 21 boolean iconed = false; 22 23 public DesktopCapture() { 24 super("EasyCapture"); 25 init(); 26 // 当点击"-"最小化按钮时,系统会最小化到托盘 27 addWindowListener(new WindowAdapter() { 28 public void windowIconified(WindowEvent e) { 29 iconed = true; 30 setVisible(false); 31 } 32 33 // 当点击"X"关闭窗口按钮时,会询问用户是否要最小化到托盘 34 // 是,表示最小化到托盘,否,表示退出 35 public void windowClosing(WindowEvent e) { 36 int option = JOptionPane.showConfirmDialog(DesktopCapture.this, 37 "是否最小化到托盘?", "提示:", JOptionPane.YES_NO_OPTION); 38 if (option == JOptionPane.YES_OPTION) { 39 iconed = true; 40 setVisible(false); 41 } else { 42 System.exit(0); 43 } 44 } 45 }); 46 pack(); 47 setSize(350, 230); 48 setLocation(500, 300); 49 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 50 setResizable(false); 51 setVisible(true); 52 } 53 54 void init() { 55 56 tray = new MyTray(DesktopCapture.this); 57 } 58 59 // 截图 60 public void capture() { 61 62 } 63 64 public static void main(String[] args) { 65 // TODO Auto-generated method stub 66 try { 67 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 68 DesktopCapture desk = new DesktopCapture(); 69 } catch (Exception e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 } 73 74 } 75 76 @Override 77 public void actionPerformed(ActionEvent e) { 78 79 } 80 81 }
/tray/src/com/b510/tray托盘/MyTray.java
1 package com.b510.tray托盘; 2 3 import java.awt.AWTException; 4 import java.awt.Image; 5 import java.awt.MenuItem; 6 import java.awt.PopupMenu; 7 import java.awt.SystemTray; 8 import java.awt.TrayIcon; 9 import java.awt.event.ActionEvent; 10 import java.awt.event.ActionListener; 11 import java.awt.event.MouseEvent; 12 import java.awt.event.MouseListener; 13 14 import javax.swing.ImageIcon; 15 import javax.swing.JFrame; 16 17 public class MyTray implements ActionListener, MouseListener { 18 private Image icon;// 图标 19 private TrayIcon trayIcon; 20 private SystemTray systemTray;// 系统托盘 21 22 private DesktopCapture frame; // 托盘所属主窗体 23 private PopupMenu pop = new PopupMenu(); // 弹出菜单 24 private MenuItem capture = new MenuItem("capture"); 25 private MenuItem show = new MenuItem("open"); 26 private MenuItem exit = new MenuItem("exit"); 27 28 public MyTray(DesktopCapture frame) { 29 this.frame = frame; 30 // icon = Toolkit.getDefaultToolkit().getImage("./images/xiaomai.png"); 31 icon = new ImageIcon(this.getClass().getClassLoader().getResource( 32 "image/xiaomai.png")).getImage(); 33 34 if (SystemTray.isSupported()) { 35 systemTray = SystemTray.getSystemTray(); 36 trayIcon = new TrayIcon(icon, "单击直接截图-EasyCapture", pop); 37 pop.add(capture); 38 pop.add(show); 39 pop.add(exit); 40 41 try { 42 systemTray.add(trayIcon); 43 } catch (AWTException e1) { 44 e1.printStackTrace(); 45 trayIcon.addMouseListener(this); 46 } 47 } 48 trayIcon.addMouseListener(this); 49 show.addActionListener(this); 50 exit.addActionListener(this); 51 capture.addActionListener(this); 52 } 53 54 @Override 55 public void actionPerformed(ActionEvent e) { 56 if (e.getSource() == show) { 57 frame.iconed = false; 58 frame.setVisible(true); 59 frame.setExtendedState(JFrame.NORMAL); 60 } else if (e.getSource() == capture) { 61 frame.capture(); 62 } else { 63 System.exit(0); 64 } 65 66 } 67 68 // ����¼� 69 @Override 70 public void mouseClicked(MouseEvent e) { 71 if (e.getClickCount() == 1 && e.getButton() != MouseEvent.BUTTON3) { 72 frame.capture(); 73 } 74 } 75 76 @Override 77 public void mouseEntered(MouseEvent arg0) { 78 // TODO Auto-generated method stub 79 80 } 81 82 @Override 83 public void mouseExited(MouseEvent arg0) { 84 // TODO Auto-generated method stub 85 86 } 87 88 @Override 89 public void mousePressed(MouseEvent arg0) { 90 // TODO Auto-generated method stub 91 92 } 93 94 @Override 95 public void mouseReleased(MouseEvent arg0) { 96 // TODO Auto-generated method stub 97 98 } 99 }
下载源码:
https://files.cnblogs.com/hongten/tray.zip