JAVA实现系统托盘

就不解释了,直接看源代码啊......(*^__^*)


/*
* MainFrame.java
*
* Created on 2008年9月20日, 上午11:44
*/

package com.topking.tray.ui;

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 java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;

/**
*
* @author lzkj
*/
public class MainFrame extends javax.swing.JFrame implements ActionListener, WindowListener{

// Variables declaration - do not modify
private javax.swing.JLabel L_img;
private javax.swing.JLabel L_img2;
private PopupMenu pop;
private MenuItem open,close;
private TrayIcon trayicon;
// End of variables declaration


/** Creates new form MainFrame */
public MainFrame() {
this.setTitle("Java实现系统托盘示例");
this.setLocation(300,300);
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

// L_img = new javax.swing.JLabel(new ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean1.png")));
// L_img2 = new javax.swing.JLabel(new ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean2.png")));
L_img = new javax.swing.JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/tray/images/netbean1.png")));
L_img2 = new javax.swing.JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/tray/images/netbean2.png")));


pop = new PopupMenu();
open = new MenuItem("打开");
open.addActionListener(this);

close = new MenuItem("关闭");
close.addActionListener(this);

pop.add(open);
pop.add(close);

if(SystemTray.isSupported()){
SystemTray tray = SystemTray.getSystemTray();
Image icon = this.getToolkit().getImage(this.getClass().getClassLoader().getResource("com/topking/tray/images/user_edit.png"));
trayicon = new TrayIcon(icon,"系统托盘示例(java)",pop);
trayicon.addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getClickCount()==2){
openFrame();
}
}

public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

});

try {
tray.add(trayicon);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(L_img2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addComponent(L_img, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(L_img)
.addGap(29, 29, 29)
.addComponent(L_img2, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(39, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==open){
openFrame();
}
if(e.getSource()==close){
System.exit(-1);
}
}

public void openFrame(){
this.setVisible(true);
this.setAlwaysOnTop(true);
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
this.setVisible(false);
this.dispose();
}

public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

//窗口最小化
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
this.dispose();
}

public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub

}


}

JAR包在附件中……
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值