java语言 托盘控制 任务栏

 
  1. import java.util.*;
  2. import java.io.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. public class Test extends JFrame {
  7.     public Test() {
  8.         final TrayIcon trayIcon;
  9.         if (SystemTray.isSupported()) {
  10.             SystemTray tray = SystemTray.getSystemTray();
  11.             Image image = Toolkit.getDefaultToolkit().getImage("src/pic/head.gif");
  12.             MouseListener mouseListener = new MouseListener() {
  13.                 public void mouseClicked(MouseEvent e) {
  14.                     System.out.println("Tray Icon - Mouse clicked!");
  15.                 }
  16.                 public void mouseEntered(MouseEvent e) {
  17.                     System.out.println("Tray Icon - Mouse entered!");
  18.                 }
  19.                 public void mouseExited(MouseEvent e) {
  20.                     System.out.println("Tray Icon - Mouse exited!");
  21.                 }
  22.                 public void mousePressed(MouseEvent e) {
  23.                     System.out.println("Tray Icon - Mouse pressed!");
  24.                 }
  25.                 public void mouseReleased(MouseEvent e) {
  26.                     System.out.println("Tray Icon - Mouse released!");
  27.                 }
  28.             };
  29.             ActionListener exitListener = new ActionListener() {
  30.                 public void actionPerformed(ActionEvent e) {
  31.                     System.out.println("Exiting...");
  32.                     System.exit(0);
  33.                 }
  34.             };
  35.             PopupMenu popup = new PopupMenu();
  36.             MenuItem defaultItem = new MenuItem("Exit");
  37.             defaultItem.addActionListener(exitListener);
  38.             popup.add(defaultItem);
  39.             trayIcon = new TrayIcon(image, "Tray Demo", popup);
  40.             ActionListener actionListener = new ActionListener() {
  41.                 public void actionPerformed(ActionEvent e) {
  42.                     trayIcon.displayMessage("Action Event",
  43.                             "An Action Event Has Been Performed!",
  44.                             TrayIcon.MessageType.INFO);
  45.                 }
  46.             };
  47.             trayIcon.setImageAutoSize(true);
  48.             trayIcon.addActionListener(actionListener);
  49.             trayIcon.addMouseListener(mouseListener);
  50.             try {
  51.                 tray.add(trayIcon);
  52.             } catch (AWTException e) {
  53.                 System.err.println("TrayIcon could not be added.");
  54.             }
  55.         } else {
  56.             //  System Tray is not supported
  57.         }
  58.     }
  59.     public static void main(String[] args) {
  60.         Test t = new Test();
  61.     }
  62. }

阅读文献:http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/

你可能感兴趣的:(java,image,String,语言,任务)