Java by API/java.awt/TrayIcon

Материал из Java эксперт
Перейти к: навигация, поиск

new TrayIcon(Image image)

  

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class Main {
  public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
      return;
    }
    SystemTray tray = SystemTray.getSystemTray();
    PropertyChangeListener pcl;
    pcl = new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent pce) {
        System.out.println("Property changed = " + pce.getPropertyName());
        TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
        if (tia != null) {
          for (int i = 0; i < tia.length; i++)
            System.out.println(tia[i]);
        }
        tia = (TrayIcon[]) pce.getNewValue();
        if (tia != null) {
          for (int i = 0; i < tia.length; i++)
            System.out.println(tia[i]);
        }
      }
    };
    tray.addPropertyChangeListener("trayIcons", pcl);
    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    TrayIcon icon = null;
    tray.add(icon = new TrayIcon(bi));
    Thread.sleep(3000);
    tray.remove(icon);
    Thread.sleep(3000);
    System.exit(0);
  }
}





new TrayIcon(Image image, String tooltip)

  

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
  static Image image = Toolkit.getDefaultToolkit().getImage("images/tray.gif");
  static TrayIcon trayIcon = new TrayIcon(image, "Tester2");
  public static void main(String[] a) throws Exception {
    if (SystemTray.isSupported()) {
      SystemTray tray = SystemTray.getSystemTray();
      trayIcon.setImageAutoSize(true);
      trayIcon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println("In here");
          trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO);
        }
      });
      try {
        tray.add(trayIcon);
      } catch (AWTException e) {
        System.err.println("TrayIcon could not be added.");
      }
    }
  }
}





new TrayIcon(Image image, String tooltip, PopupMenu popup)

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
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.MouseMotionListener;
import java.awt.image.BufferedImage;
public class Main {
  public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
      return;
    }
    SystemTray tray = SystemTray.getSystemTray();
    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    };
    miExit.addActionListener(al);
    popup.add(miExit);
    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);
    al = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
      }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);
    MouseListener ml = new MouseListener() {
      public void mouseClicked(MouseEvent e) {
        System.out.println("Tray icon: Mouse clicked");
      }
      public void mouseEntered(MouseEvent e) {
        System.out.println("Tray icon: Mouse entered");
      }
      public void mouseExited(MouseEvent e) {
        System.out.println("Tray icon: Mouse exited");
      }
      public void mousePressed(MouseEvent e) {
        System.out.println("Tray icon: Mouse pressed");
      }
      public void mouseReleased(MouseEvent e) {
        System.out.println("Tray icon: Mouse released");
      }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
      public void mouseDragged(MouseEvent e) {
        System.out.println("Tray icon: Mouse dragged");
      }
      public void mouseMoved(MouseEvent e) {
        System.out.println("Tray icon: Mouse moved");
      }
    };
    ti.addMouseMotionListener(mml);
    tray.add(ti);
  }
}





TrayIcon: addActionListener(ActionListener listener)

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
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.MouseMotionListener;
import java.awt.image.BufferedImage;
public class Main {
  public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
      return;
    }
    SystemTray tray = SystemTray.getSystemTray();
    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    };
    miExit.addActionListener(al);
    popup.add(miExit);
    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);
    al = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
      }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);
    MouseListener ml = new MouseListener() {
      public void mouseClicked(MouseEvent e) {
        System.out.println("Tray icon: Mouse clicked");
      }
      public void mouseEntered(MouseEvent e) {
        System.out.println("Tray icon: Mouse entered");
      }
      public void mouseExited(MouseEvent e) {
        System.out.println("Tray icon: Mouse exited");
      }
      public void mousePressed(MouseEvent e) {
        System.out.println("Tray icon: Mouse pressed");
      }
      public void mouseReleased(MouseEvent e) {
        System.out.println("Tray icon: Mouse released");
      }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
      public void mouseDragged(MouseEvent e) {
        System.out.println("Tray icon: Mouse dragged");
      }
      public void mouseMoved(MouseEvent e) {
        System.out.println("Tray icon: Mouse moved");
      }
    };
    ti.addMouseMotionListener(mml);
    tray.add(ti);
  }
}





TrayIcon: addMouseMotionListener(MouseMotionListener listener)

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
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.MouseMotionListener;
import java.awt.image.BufferedImage;
public class Main {
  public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
      return;
    }
    SystemTray tray = SystemTray.getSystemTray();
    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    };
    miExit.addActionListener(al);
    popup.add(miExit);
    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);
    al = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
      }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);
    MouseListener ml = new MouseListener() {
      public void mouseClicked(MouseEvent e) {
        System.out.println("Tray icon: Mouse clicked");
      }
      public void mouseEntered(MouseEvent e) {
        System.out.println("Tray icon: Mouse entered");
      }
      public void mouseExited(MouseEvent e) {
        System.out.println("Tray icon: Mouse exited");
      }
      public void mousePressed(MouseEvent e) {
        System.out.println("Tray icon: Mouse pressed");
      }
      public void mouseReleased(MouseEvent e) {
        System.out.println("Tray icon: Mouse released");
      }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
      public void mouseDragged(MouseEvent e) {
        System.out.println("Tray icon: Mouse dragged");
      }
      public void mouseMoved(MouseEvent e) {
        System.out.println("Tray icon: Mouse moved");
      }
    };
    ti.addMouseMotionListener(mml);
    tray.add(ti);
  }
}





TrayIcon: displayMessage(String caption, String text, MessageType messageType)

  

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
  static Image image = Toolkit.getDefaultToolkit().getImage("images/tray.gif");
  static TrayIcon trayIcon = new TrayIcon(image, "Tester2");
  public static void main(String[] a) throws Exception {
    if (SystemTray.isSupported()) {
      SystemTray tray = SystemTray.getSystemTray();
      trayIcon.setImageAutoSize(true);
      trayIcon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println("In here");
          trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO);
        }
      });
      try {
        tray.add(trayIcon);
      } catch (AWTException e) {
        System.err.println("TrayIcon could not be added.");
      }
    }
  }
}





TrayIcon: setActionCommand(String command)

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
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.MouseMotionListener;
import java.awt.image.BufferedImage;
public class Main {
  public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
      return;
    }
    SystemTray tray = SystemTray.getSystemTray();
    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    };
    miExit.addActionListener(al);
    popup.add(miExit);
    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);
    al = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
      }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);
    MouseListener ml = new MouseListener() {
      public void mouseClicked(MouseEvent e) {
        System.out.println("Tray icon: Mouse clicked");
      }
      public void mouseEntered(MouseEvent e) {
        System.out.println("Tray icon: Mouse entered");
      }
      public void mouseExited(MouseEvent e) {
        System.out.println("Tray icon: Mouse exited");
      }
      public void mousePressed(MouseEvent e) {
        System.out.println("Tray icon: Mouse pressed");
      }
      public void mouseReleased(MouseEvent e) {
        System.out.println("Tray icon: Mouse released");
      }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
      public void mouseDragged(MouseEvent e) {
        System.out.println("Tray icon: Mouse dragged");
      }
      public void mouseMoved(MouseEvent e) {
        System.out.println("Tray icon: Mouse moved");
      }
    };
    ti.addMouseMotionListener(mml);
    tray.add(ti);
  }
}





TrayIcon: setImageAutoSize(boolean autosize)

  
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;
public class Main {
  public static void main(String[] args)throws Exception {
    if (!SystemTray.isSupported()) {
      System.out.println("SystemTray is not supported");
      return;
    }
    SystemTray tray = SystemTray.getSystemTray();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("trayIcon.jpg");
    PopupMenu menu = new PopupMenu();
    MenuItem messageItem = new MenuItem("Show Message");
    messageItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("asdf");
      }
    });
    menu.add(messageItem);
    MenuItem closeItem = new MenuItem("Close");
    closeItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    menu.add(closeItem);
    TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
    icon.setImageAutoSize(true);
    tray.add(icon);
  }
}