Java程序托盘

Java程序托盘,第1张

import java.awt.Color

import java.awt.Graphics2D

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 java.awt.image.BufferedImage

import javax.swing.JOptionPane

public class TrayIconTest {

public static void main(String[] args) throws Exception{

BufferedImage img=new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB_PRE)

Graphics2D g=img.createGraphics()

g.setColor(Color.yellow)

g.setClip(new java.awt.geom.Ellipse2D.Double(1,1,30,30))

g.fillRect(0, 0, 32,32)

g.dispose()

//悉槐 BufferedImage img=javax.imageio.ImageIO.read(new java.net.URL("file:/睁庆友/差搜/d:/logo.gif"))

TrayIcon ti=new TrayIcon(img)

ti.setToolTip("TrayIcon test")

ti.setImageAutoSize(true)

SystemTray.getSystemTray().add(ti)

ActionListener al=new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.out.println(e.getActionCommand())

if("exit".equals(e.getActionCommand()))

System.exit(0)

}

}

PopupMenu pm=new PopupMenu()

MenuItem mi=new MenuItem("a")

mi.addActionListener(al)

pm.add(mi)

mi=new MenuItem("exit")

mi.addActionListener(al)

pm.add(mi)

ti.setPopupMenu(pm)

ti.addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {

if(e.getClickCount()==2){

System.out.println("doubleClicked.")

JOptionPane.showMessageDialog(null,"Hi!")

}

}

public void mousePressed(MouseEvent e) {

System.out.println("mousePressed")

}

public void mouseReleased(MouseEvent e) {

System.out.println("mouseReleased")

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

})

}

}

http://hi.baidu.com/hardneedl/blog/item/23f15c3eebdbe73671cf6ca0.html

其中

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {exitSystem()}

public void windowIconified(WindowEvent e) {

setVisible(false)

if(SystemTray.isSupported()){

try {

SystemTray.getSystemTray().add(trayIcon)

} catch(AWTException e1) {

e1.printStackTrace()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12379352.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存