Windows系统托盘图标实践(AWT)

  import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class TrayIconTest extends Frame{
      
         Label lab=new Label("Hello World!!!");
    
         public TrayIconTest(){
            add(lab);
            setSize(400,300);
            addWindowListener(new MywindowListener());
            
         }
         
         class MywindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
 }

         public static void main(String[] args){
           TrayIconTest test=new TrayIconTest();
           test.show();
         }
}

   以下主要添加了托盘图标及弹出菜单:(运行这个程序时需要图像文件,TrayIcon12.dll,包

com.jeans.trayicon,还要设置好classpath,请下载我的文件夹)。


import java.awt.*;
import java.awt.event.*;
import java.io.*;

import com.jeans.trayicon.*;

public class TrayIconTest extends Frame{

         protected WindowsTrayIcon icon;//托盘图标
         Label lab=new Label("Hello World!!!");
    
         public TrayIconTest() throws TrayIconException, InterruptedException{
            add(lab);
            Image france = loadImage("Duke16.gif");
                  icon = new WindowsTrayIcon(france, 16, 16);
                  icon.setToolTipText("Hello");//设置托盘图标的提示信息
                  icon.setPopup(makePopup());//给托盘图标加右键菜单
                  icon.addActionListener(new TrayIconListener());//单击托盘图标时的动作
                  icon.setVisible(true);//托盘图标设置为显示状态
                  WindowsTrayIcon.keepAlive();
                  setSize(400,300);
                  addWindowListener(new MywindowListener());
           
        }
         
        

         public static void main(String[] args){
                try {
                        String appName = "TestTray";
                        //通过托盘图标库,设置发送信息的回调方法。
                        long result = WindowsTrayIcon.sendWindowsMessage(appName, 1234);
                        if (result != -1) {
                           System.out.println("Already running other instance of "+appName+
                                            "(returns: "+result+")");
                           return;
                        }
                        WindowsTrayIcon.initTrayIcon(appName);

                        TrayIconTest test = new TrayIconTest();
                        test.show();
                } catch (TrayIconException e) {
                        System.out.println("Error: "+e.getMessage());
                } catch (InterruptedException e) {
                }
       }

        
      // 装载gif图像
  public static Image loadImage(String fileName) {
  return Toolkit.getDefaultToolkit().getImage("demo"+File.separator+"images"+File.separator+fileName);
}  

       //单击托盘图标时显示应用程序的窗口
      private class TrayIconListener implements ActionListener {
                    public void actionPerformed(ActionEvent evt) {
                        setVisible(true);
                        // Request input focus
                        requestFocus();
                   }

      }
     //关闭程序窗口的动作设置为隐藏窗口
     private class MywindowListener extends WindowAdapter{
                 public void windowClosing(WindowEvent e){
                         setVisible(false);
                         //  System.exit(0);
                 }
     }

     public TrayIconPopup makePopup() {//构造弹出菜单
               TrayIconPopup popup = new TrayIconPopup();
               // 增加三个菜单项
               TrayIconPopupSimpleItem item = new TrayIconPopupSimpleItem("&About");
               item.addActionListener(new AboutListener());
               popup.addMenuItem(item);
               item = new TrayIconPopupSimpleItem("&Configure");
               item.addActionListener(new ConfigureListener());
               popup.addMenuItem(item);
               // 增加分隔线
               TrayIconPopupSeparator sep = new TrayIconPopupSeparator();
               popup.addMenuItem(sep);
               item = new TrayIconPopupSimpleItem("E&xit");
               item.addActionListener(new ExitListener());
               popup.addMenuItem(item);
                   return popup;
      }

    private class ExitListener implements ActionListener {//弹出菜单中的Exit操作

            public void actionPerformed(ActionEvent evt) {
                  WindowsTrayIcon.cleanUp();
                  System.exit(0);
            }

    }


      private class AboutListener implements ActionListener {//弹出菜单中的About操作

                public void actionPerformed(ActionEvent evt) {
                     System.out.println("About selected.");
                     DemoAboutBox box = new DemoAboutBox();
                      //centerDialog(box);
                     box.show();
                }

       }


       private class ConfigureListener implements ActionListener {//弹出菜单中的Configure操作

           public void actionPerformed(ActionEvent evt) {
                        ConfigureBox box = new ConfigureBox();
                        box.show();
           }

     }     

}

class ConfigureBox extends Frame {//弹出的对话框1

           public ConfigureBox() {
                super("Configure TrayIcon");
                setBackground(SystemColor.control);
                setLayout(new GridLayout(0,1));
                add(new Label("Hello"));
                addWindowListener(new CloseWindowListener());
                pack();
                setSize(300,200);

           }

     private class CloseWindowListener extends WindowAdapter {

          public void windowClosing(WindowEvent evt) {
                  dispose();
         }

     }

}


class DemoAboutBox extends Frame {//弹出的对话框2

       // Create new about box given parent frame
       public DemoAboutBox()  {
               super("About TrayIcon");
               setBackground(SystemColor.control);
               setLayout(new GridLayout(0,1,3,3));
               add(new Label("TrayIcon version "+WindowsTrayIcon.TRAY_VERSION));
               add(new Label("Written by Jan Struyf "));
               Panel buttons = new Panel();
               buttons.setLayout(new FlowLayout());
               Button button = new Button("OK");
               button.addActionListener(new CloseListener());
               buttons.add(button);
               add(buttons, BorderLayout.SOUTH);
               addWindowListener(new CloseWindowListener());
                pack();
        }
       private class CloseListener implements ActionListener {

           public void actionPerformed(ActionEvent evt) {
                  dispose();
           }

      }


      private class CloseWindowListener extends WindowAdapter {

           public void windowClosing(WindowEvent evt) {
                dispose();
           }

     }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值