就不解释了,直接看源代码啊......(*^__^*)
JAR包在附件中……
/*
* 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包在附件中……