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()

}

}

以下代码直接copy即可运行,注意修改一下托盘图标的文件路径!

/////////////////////////////////////////////////////////////////////

import java.awt.AWTException

import java.awt.Image

import java.awt.MenuItem

import java.awt.PopupMenu

import java.awt.SystemTray

import java.awt.Toolkit

import java.awt.TrayIcon

import java.awt.TrayIcon.MessageType

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.MouseAdapter

import java.awt.event.MouseEvent

import javax.swing.JFrame

public class SystemTrayTest extends JFrame {

public SystemTrayTest() throws AWTException {

this.setSize(500, 300)

this.setLocation(100, 50)

//判断当前 *** 作系统是否支持托盘

if (SystemTray.isSupported()) {

//获取系统托盘

SystemTray tray = SystemTray.getSystemTray()

//托盘的右键菜单

PopupMenu menu = new PopupMenu()

//退出菜单

MenuItem exitItem = new MenuItem("Exit")

exitItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.exit(0)

}

})

//显示主窗口菜单

MenuItem showItem = new MenuItem("Show the main frame")

showItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

SystemTrayTest.this.setVisible(true)

}

})

//关于菜单(未注册事件)

MenuItem aboutItem = new MenuItem("about...")

menu.add(exitItem)

menu.add(showItem)

menu.add(aboutItem)

//托盘的标题

String title = "Hey,Java!"

//托盘图标

Image img = Toolkit.getDefaultToolkit().getImage("icon.jpg")

//实例化TrayIcon对象

TrayIcon icon = new TrayIcon(img, title, menu)

//托盘添加鼠标事件

icon.addMouseListener(new MouseAdapter() {

public void mouseReleased(MouseEvent e) {

SystemTrayTest.this.setVisible(true)

}

})

tray.add(icon)

icon.displayMessage("Hello!", "this is my DIY SystemTray!",

MessageType.INFO)// 运行程序的时候右下角会提示信息

}

}

public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

try {

new SystemTrayTest().setVisible(true)

} catch (AWTException e) {

e.printStackTrace()

}

}

})

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存