基于java开发之系统托盘的应用
项目结构:
运行效果:
========================================================================
下面是代码部分:
========================================================================
/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
}
}