基于java开发之系统托盘的应用

一个人y幸福

一个人y幸福

2016-02-19 10:03

岁数大了,QQ也不闪了,微信也不响了,电话也不来了,但是图老师依旧坚持为大家推荐最精彩的内容,下面为大家精心准备的基于java开发之系统托盘的应用,希望大家看完后能赶快学习起来。

项目结构:

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

运行效果:

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

========================================================================

下面是代码部分:

========================================================================

/tray/src/com/b510/tray托盘/DesktopCapture.java
代码如下:

package com.b510.tray托盘;

 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;

 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 import javax.swing.UIManager;

 public class DesktopCapture extends JFrame implements ActionListener {

     /**
      * 1.改变双击托盘截图为单击托盘截图 2.修复了托盘后无反应BUG 3.将放大镜提示框移至左上角和右上角
      */
     private static final long serialVersionUID = 1L;
     // JButton confirm;
     // BufferedImage desktopImg;
     MyTray tray;
     boolean iconed = false;

     public DesktopCapture() {
         super("EasyCapture");
         init();
         // 当点击"-"最小化按钮时,系统会最小化到托盘
         addWindowListener(new WindowAdapter() {
             public void windowIconified(WindowEvent e) {
                 iconed = true;
                 setVisible(false);
             }

             // 当点击"X"关闭窗口按钮时,会询问用户是否要最小化到托盘
             // 是,表示最小化到托盘,否,表示退出
             public void windowClosing(WindowEvent e) {
                 int option = JOptionPane.showConfirmDialog(DesktopCapture.this,
                         "是否最小化到托盘?", "提示:", JOptionPane.YES_NO_OPTION);
                 if (option == JOptionPane.YES_OPTION) {
                     iconed = true;
                     setVisible(false);
                 } else {
                     System.exit(0);
                 }
             }
         });
         pack();
         setSize(350, 230);
         setLocation(500, 300);
         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         setResizable(false);
         setVisible(true);
     }

     void init() {

         tray = new MyTray(DesktopCapture.this);
     }

     // 截图
     public void capture() {

     }

     public static void main(String[] args) {
         // TODO Auto-generated method stub
         try {
             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
             DesktopCapture desk = new DesktopCapture();
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }

     }

     @Override
     public void actionPerformed(ActionEvent e) {

     }

 }

/tray/src/com/b510/tray托盘/MyTray.java
代码如下:

package com.b510.tray托盘;

 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 java.awt.event.MouseListener;

 import javax.swing.ImageIcon;
 import javax.swing.JFrame;

 public class MyTray implements ActionListener, MouseListener {
     private Image icon;// 图标
     private TrayIcon trayIcon;
     private SystemTray systemTray;// 系统托盘

     private DesktopCapture frame; // 托盘所属主窗体
     private PopupMenu pop = new PopupMenu(); // 弹出菜单
     private MenuItem capture = new MenuItem("capture");
     private MenuItem show = new MenuItem("open");
     private MenuItem exit = new MenuItem("exit");

     public MyTray(DesktopCapture frame) {
         this.frame = frame;
         // icon = Toolkit.getDefaultToolkit().getImage("./images/xiaomai.png");
         icon = new ImageIcon(this.getClass().getClassLoader().getResource(
                 "image/xiaomai.png")).getImage();

         if (SystemTray.isSupported()) {
             systemTray = SystemTray.getSystemTray();
             trayIcon = new TrayIcon(icon, "单击直接截图-EasyCapture", pop);
             pop.add(capture);
             pop.add(show);
             pop.add(exit);

             try {
                 systemTray.add(trayIcon);
             } catch (AWTException e1) {
                 e1.printStackTrace();
                 trayIcon.addMouseListener(this);
             }
         }
         trayIcon.addMouseListener(this);
         show.addActionListener(this);
         exit.addActionListener(this);
         capture.addActionListener(this);
     }

     @Override
     public void actionPerformed(ActionEvent e) {
         if (e.getSource() == show) {
             frame.iconed = false;
             frame.setVisible(true);
             frame.setExtendedState(JFrame.NORMAL);
         } else if (e.getSource() == capture) {
             frame.capture();
         } else {
             System.exit(0);
         }

     }

     // ����¼�
     @Override
     public void mouseClicked(MouseEvent e) {
         if (e.getClickCount() == 1 && e.getButton() != MouseEvent.BUTTON3) {
             frame.capture();
         }
     }

     @Override
     public void mouseEntered(MouseEvent arg0) {
         // TODO Auto-generated method stub

     }

     @Override
     public void mouseExited(MouseEvent arg0) {
         // TODO Auto-generated method stub

     }

     @Override
     public void mousePressed(MouseEvent arg0) {
         // TODO Auto-generated method stub

     }

     @Override
     public void mouseReleased(MouseEvent arg0) {
         // TODO Auto-generated method stub

     }
 }

展开更多 50%)
分享

猜你喜欢

基于java开发之系统托盘的应用

编程语言 网络编程
基于java开发之系统托盘的应用

使用Java实现系统托盘功能的介绍(附源码以及截图)

编程语言 网络编程
使用Java实现系统托盘功能的介绍(附源码以及截图)

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

如何自动移去系统托盘失效的图标

编程语言 网络编程
如何自动移去系统托盘失效的图标

JAVA应用系统工具快捷托盘实例代码

编程语言 网络编程
JAVA应用系统工具快捷托盘实例代码

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

实用技巧:清除系统托盘图标

windows 操作系统
实用技巧:清除系统托盘图标

Windows7的系统托盘区域有什么改进

电脑入门
Windows7的系统托盘区域有什么改进

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

基于Android设计模式之--SDK源码之策略模式的详解

基于Android设计模式之--SDK源码之策略模式的详解

Some tips of wmi scripting in jscript (1)

Some tips of wmi scripting in jscript (1)
下拉加载更多内容 ↓