zoukankan      html  css  js  c++  java
  • Swing带TrayIcon(托盘小图标)的Hello world示例

            首先上一个经典的Hello world示例:

     1 package com.xxx.yyy.zzz;
     2 
     3 import javax.swing.JFrame;
     4 
     5 public class MainFrame extends JFrame {
     6 
     7     private static final long serialVersionUID = -3059928131346032935L;
     8     public MainFrame() {
     9         super();
    10         this.setSize(300, 200);
    11         this.getContentPane().setLayout(null);
    12         this.setTitle("Hello world!");
    13         /// 设置点击窗口右上角“关闭”时的行为,点击“关闭”时停止主循环,退出程序
    14         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    15     }
    16 }
    17 
    18 class Main {
    19     public static void main(String[] args) {
    20         MainFrame frm = new MainFrame();
    21         frm.setVisible(true);
    22     }
    23 }

            在eclipse下编译运行上述代码,显示如下的小窗口:

            很多应用软件都会在点击“最小化”这个按钮时,将窗口缩小到任务栏的数字时钟附近的一个小区域(托盘)。java自1.6版以后,在AWT里新增了两个类:SystemTray,TrayIcon。通过这两个类,java也能使用托盘图标了。

            使用TrayIcon,需要准备一个小图标。jpg、png等通用的图片格式貌似都可以用,不支持M$的ico。不知道对图片的尺寸有没有要求,本人准备的是一个16*16的png,比划了一下,托盘图标的大小大概是16*16。Java的TrayIcon内部做了图像转换机制,只要设置好就行了。

            以下是添加了TrayIcon之后的代码,最小化时隐藏窗口,显示托盘图标;点击图标时,图标消失,窗口出现。

     1 /// 实现接口WindowListener
     2 public class MainFrame extends JFrame implements WindowListener{
     3 
     4     private static final long serialVersionUID = -3059928131346032935L;
     5     /// 32*32的png图像
     6     private static Image image = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("c_cyan.png"));
     7     /// TrayIcon对象
     8     private static TrayIcon trayIcon = new TrayIcon(image,"xx助手");
     9 
    10     public MainFrame() {
    11         super();
    12         this.setSize(300, 200);
    13         this.getContentPane().setLayout(null);
    14         this.setTitle("Hello world!");
    15         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    16         /// 注册本窗口Window相关事件监听器
    17         this.addWindowListener(this);
    18     }
    19 
    20     public void windowActivated(WindowEvent e) {}
    21 
    22     public void windowClosed(WindowEvent e) {}
    23 
    24     public void windowClosing(WindowEvent e) {}
    25 
    26     public void windowDeactivated(WindowEvent e) {}
    27 
    28     public void windowDeiconified(WindowEvent e) {
    29     }
    30 
    31     public void windowIconified(WindowEvent e) {
    32         /// 检测操作系统是否支持系统托盘
    33         if (!SystemTray.isSupported()) {
    34             return;
    35         }
    36 
    37         final MainFrame parent = this;
    38         final SystemTray systemTray = SystemTray.getSystemTray();
    39 
    40         /// 设置TrayIcon自动调整图像大小,看来这么设置之后不用再关注图片尺寸了
    41         trayIcon.setImageAutoSize(true);
    42         /// TrayIcon添加击事件的监听器,点击后恢复主窗口,删除TrayIcon
    43         trayIcon.addActionListener(new ActionListener() {
    44             public void actionPerformed(ActionEvent event) {
    45                 /// 显示主窗口,设置窗口为正常状态
    46                 parent.setVisible(true);
    47 parent.setExtendedState(JFrame.NORMAL);
    48 /// 从系统托盘之中移除TrayIcon,因为同一个TrayIcon不能添加两次 49 systemTray.remove(trayIcon); 50 } 51 }); 52 53 try { 54 /// 往系统托盘之中添加TrayIcon 55 systemTray.add(trayIcon); 56 } catch (AWTException exception) { 57 exception.printStackTrace(); 58 } 59 60 /// 隐藏本窗口 61 parent.setVisible(false); 62 } 63 64 public void windowOpened(WindowEvent e) {} 65 }

            换个32*32大小的图标,再编译运行,同样也能达到效果,只是显示出来的小图标没有16*16的清晰。

  • 相关阅读:
    Ethical Hacking
    Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    食物链 POJ
    Wireless Network POJ
    Candies POJ
    畅通工程再续 HDU
    Jungle Roads HDU
  • 原文地址:https://www.cnblogs.com/dsdk2008/p/4745898.html
Copyright © 2011-2022 走看看