创建系统托盘程序

    技术2022-05-20  41

    package mySystemTray;

    import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;

    import javax.swing.*;

    public class SystemTrayFrame extends JFrame {

     /**author  hjx  * 2011-02-26  * 实现一个系统托盘程序,并添加弹出菜单,鼠标双击功能。  * @param args  */

     private JButton button1 = null; //最小化到托盘 private JButton button2 = null; //退出系统按钮

     private ImageIcon imageicon = null; //图标对象 private SystemTray trays = null; //系统托盘区对象 private TrayIcon trayicon = null; //托盘对象

     private static final long serialVersionUID = 1L;

     public static void main(String[] args) {  // TODO Auto-generated method stub  new SystemTrayFrame(); //主方法,新建对象; }

     //构造方法 public SystemTrayFrame() {  super();  this.setTitle("使用系统托盘");  this.setBounds(100, 100, 400, 180);    this.setResizable(false); //设置窗体不可最大化  this.setLayout(null); //采用空边界布局  //要实现点击窗体右上角的X按钮不进入真正关闭窗体事件,这里就不能设置窗体关闭事件。  //this.setDefaultCloseOperation(JFrame);

      button1 = new JButton("缩小到托盘"); //创建按钮组件  button1.setBounds(20, 20, 120, 30); //定位组件及大小  button2 = new JButton("程序退出"); //创建按钮组件  button2.setBounds(180, 20, 120, 30); //定位组件及大小

      this.add(button1); //在窗体上添加各组件  this.add(button2);

      //判断本操作系统是否支持托盘程序  if (SystemTray.isSupported()) {

       tray();

      }

      this.addWindowListener(new WindowListener() {

       public void windowActivated(WindowEvent e) {    // TODO Auto-generated method stub

       }

       public void windowClosed(WindowEvent e) {    // TODO Auto-generated method stub

       }

       public void windowClosing(WindowEvent e) {    // TODO Auto-generated method stub

        //在点击窗体的右上角的X关闭按钮时,并不是真正关闭窗体。而是最小化到托盘区,这是很多软件都在使用的功能。    //在此添加了一个提示框,只做演示用。    JOptionPane.showMessageDialog(new JLabel(), "程序最小化到托盘区",      "信息提示", 2);

        try {     trays.add(trayicon); //添加托盘图标到系统托盘区    } catch (AWTException e1) {     // TODO Auto-generated catch block     e1.printStackTrace();    }    setVisible(false); // 使窗口不可视

       }

       public void windowDeactivated(WindowEvent e) {    // TODO Auto-generated method stub

       }

       public void windowDeiconified(WindowEvent e) {    // TODO Auto-generated method stub

       }

       public void windowIconified(WindowEvent e) {    // TODO Auto-generated method stub

        try {     trays.add(trayicon); //添加托盘图标到系统托盘区     setVisible(false); // 使窗口不可视

        } catch (AWTException e1) {     // TODO Auto-generated catch block     e1.printStackTrace();    }

       }

       public void windowOpened(WindowEvent e) {    // TODO Auto-generated method stub

       }

      });

      this.setVisible(true); //开始显示窗体;

     }

     public void tray() {

      //定义托盘图标对象  imageicon = new ImageIcon("D:/Program Files/Java/404.png");

      //右键弹出的菜单  PopupMenu pupo = new PopupMenu();

      MenuItem open = new MenuItem("打开");  MenuItem exit = new MenuItem("退出");  MenuItem author = new MenuItem("作者");  pupo.add(open);  pupo.add(exit);  pupo.add(author);

      //获得托盘区域对象  trays = SystemTray.getSystemTray();

      //创建托盘对象  trayicon = new TrayIcon(imageicon.getImage(), "托盘程序", pupo);

      //为各个组件添加监听事件  button1.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {    // TODO Auto-generated method stub

        try {     trays.add(trayicon); //向托盘区域添加托盘对象     setVisible(false); // 使窗口不可视    } catch (AWTException e1) {     // TODO Auto-generated catch block     e1.printStackTrace();    }

       }

      });

      button2.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {    // TODO Auto-generated method stub    System.exit(0);   }

      });

      trayicon.addMouseListener(new MouseAdapter() {

       public void mouseClicked(MouseEvent e) {

        if (e.getClickCount() == 2) { //如果双击托盘图标     trays.remove(trayicon); //从托盘区域移去托盘对          setExtendedState(JFrame.NORMAL); //使窗体恢复到原来的形状;     setVisible(true); //设置窗体可显示;

        }

       };

      });

      open.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {    // TODO Auto-generated method stub

        trays.remove(trayicon); //从托盘区域移去托盘对

        setVisible(true); //设置窗体可显示;    setAlwaysOnTop(true); //使窗体置顶

        setExtendedState(JFrame.NORMAL); //使窗体恢复到原来的形状;

        //repaint();   }

      });

      exit.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {    // TODO Auto-generated method stub    System.exit(0);   }

      });

      author.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {    // TODO Auto-generated method stub

        JOptionPane.showMessageDialog(new JLabel(), "作者:865086322",      "信息提示", 1);

       }

      });

     }

    }


    最新回复(0)