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

}

}

java

最小化到系统托盘

2010-09-06 15:32

package test

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.MouseAdapter

import java.awt.event.MouseEvent

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

import javax.swing.ImageIcon

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.SwingUtilities

public class MyTray {

TrayIcon trayIcon//

托盘图标,但不是

Image

类型的银槐厅

SystemTray Tray//

系统托盘

Image

img=(new

ImageIcon("images/锋隐icon.png")).getImage()//

托盘图标,建

议使用较小的图片

public MyTray(){

final JFrame frame=new JFrame()

JPanel jp=new JPanel()

frame.add(jp)

frame.setSize(320,240)

frame.setLocationRelativeTo(null)

frame.setResizable(false)

frame.setIconImage(img)//

设置窗口左上角的图标

frame.setVisible(true)

Tray=SystemTray.getSystemTray()//

获得系统托盘实例

//

创建系统托盘的右键d出菜单

PopupMenu pm=new PopupMenu()

MenuItem mi0=new MenuItem("Open")

MenuItem mi1=new MenuItem("Close")

pm.add(mi0)

pm.add(mi1)

trayIcon=new TrayIcon(img,"",pm)//

创建托盘图标实例

trayIcon.setImageAutoSize(true)/明消/

图标自动适应托盘,也就是说它自动

改变大小

trayIcon.setToolTip(" ")//

设置提示语

//

添加系统托盘图标

frame.addWindowListener(new

WindowAdapter()

{//

当“关闭”窗口时,最

小化到系统托盘

public void windowClosing(WindowEvent e) {

try {

Tray.add(trayIcon)

} catch (AWTException exc) {

exc.printStackTrace()

}

}})

mi0.addActionListener(new ActionListener(){ //

右键d出菜单的事件

监听

public void actionPerformed(ActionEvent e) {

frame.setExtendedState(JFrame.NORMAL)

frame.setVisible(true)

Tray.remove(trayIcon)

}

})

mi1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.exit(0)

Tray.remove(trayIcon)//

退出程序,移出系统托盘处的图标


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存