java 托盘

java 托盘
package  test;

import  java.awt.Frame;
import  java.awt.Image;
import  java.awt.SystemTray;
import  java.awt.Toolkit;
import  java.awt.TrayIcon;
import  java.awt.event.MouseAdapter;
import  java.awt.event.MouseEvent;
import  java.awt.event.WindowAdapter;
import  java.awt.event.WindowEvent;

import  javax.swing.JFrame;

@SuppressWarnings(
" serial " )
public   class  MyFrame  extends  JFrame
{
    
public MyFrame()
    
{
        
super("MY FRAME");
        
this.init();
    }

    
    
private void init()
    
{
        
this.setSize(400300);
        Image icons 
= getToolkit().getImage(this.getClass().getResource("/icons/cc.gif"));
        
this.setIconImage(icons);
        
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        
this.setLocation((int) (width - this.getWidth()) / 2, (int) (height - this.getHeight()) / 2);
        
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
// 托盘
        TrayIcon trayIcon = new TrayIcon(icons, "MY FRAME");
        
try
        
{
            SystemTray.getSystemTray().add(trayIcon);
        }

        
catch (Exception ex)
        
{}
        
        
// 托盘事件-最小化
        this.addWindowListener(new WindowAdapter()
            
{
                
public void windowIconified(WindowEvent e)
                
{
                    
// 窗口最小化时dispose该窗口
                    dispose();
                }

            }
);
        
        
//托盘事件-双击托盘最大化
        trayIcon.addMouseListener(new MouseAdapter()
            
{
                
public void mouseClicked(MouseEvent e)
                
{
                    
// 双击托盘窗口再现
                    if (e.getClickCount() == 2)
                    
{
                        setExtendedState(Frame.NORMAL);
                    }

                    setVisible(
true);
                }

            }
);
        
this.setResizable(false);
        
this.setVisible(true);
    }

    
    
public static void main(String[] args)
    
{
        
new MyFrame();
    }

}

你可能感兴趣的:(java 托盘)