Java中将应用程序最小化到托盘

本文提供了一个使用Java实现系统托盘程序的示例代码,包括如何创建托盘图标、设置弹出菜单以及处理鼠标点击事件等关键步骤。

测试代码包括2个文件: TaryFrame.java   和 Tary.java.

 

///////////////////////////////////////////////    1. TaryFrame.java   //////////////////////////////////////////////////////


import javax.swing.JFrame;

import javax.swing.JTextArea;
import javax.swing.JTextField;

 

public class TaryFrame extends JFrame{
 
 public  static JTextArea jta_info = new JTextArea(10,30);
 
 public  JTextField jtf_port;
 
 public TaryFrame(){
  this.setTitle("tray test");
  this.setSize(410,380);
  
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);

  //窗体大小固定
     setResizable(false);
     new Tary(this);
  this.setVisible(true);
 }
 
 public static void main(String[] args) {
  new TaryFrame();
 }

}

 

///////////////////////////////////////////////    2. Tary.java   //////////////////////////////////////////////////////


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.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.JFrame;

public class Tary implements ActionListener,MouseListener,WindowListener{
 
  TrayIcon trayIcon=null;
  Image iconImage= Toolkit.getDefaultToolkit().getImage("img/trayicon.png");
  JFrame frame;
  public Tary(JFrame f){
   frame=f;
    if (SystemTray.isSupported()) {
     SystemTray tray = SystemTray.getSystemTray();
     PopupMenu popup = new PopupMenu();
     //popup.addActionListener(this);
     MenuItem defaultItem = new MenuItem("打开");
     defaultItem.addActionListener(this);
     popup.add(defaultItem);
     defaultItem = new MenuItem("退出");
     defaultItem.addActionListener(this);
     popup.add(defaultItem);
     trayIcon = new TrayIcon(iconImage, "Tray Test", popup);
     trayIcon.addMouseListener(this);
       try {
      tray.add(trayIcon);
      } catch (AWTException e) {
      System.err.println(e);
               }
    }
    frame.setIconImage(iconImage);
    frame.addWindowListener(this);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
   }
  public void windowActivated(WindowEvent arg0) {
   //this.setAlwaysOnTop(true);
  }
 
  public void windowClosed(WindowEvent arg0) {
  
  }
 
  public void windowClosing(WindowEvent arg0) {
  
  }
 
  public void windowDeactivated(WindowEvent arg0) {
  
  }
 
  public void windowDeiconified(WindowEvent arg0) {
  
  }
 
  public void windowIconified(WindowEvent arg0) {
   frame.setVisible(false);
  }
 
  public void windowOpened(WindowEvent arg0) {
  
  }
 
  public void actionPerformed(ActionEvent e) {
   if(e.getActionCommand().equals("退出"))
    System.exit(0);
   else if(e.getActionCommand().equals("打开"))
    frame.setVisible(true);
   
  }
 
  public void mouseClicked(MouseEvent e) {
   if(e.getClickCount()==2)
   {
    frame.setVisible(true);
    frame.setExtendedState(JFrame.NORMAL);
    frame.repaint();
   }
  }
 
  public void mouseEntered(MouseEvent arg0) {
   System.out.println("mouseEntered ");
  
  }
 
  public void mouseExited(MouseEvent arg0) {
   System.out.println("mouseExited ");
  }
 
  public void mousePressed(MouseEvent arg0) {
   System.out.println("mousePressed ");
  }
 
  public void mouseReleased(MouseEvent arg0) {
   System.out.println("mouseReleased ");
  }
}

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值