JAVA 编写托盘例子

效果如上图所示:

本例子采用了JDIC(JDesktop Integration Components)组件,实现了JAVA托盘和消息提醒的功能。

 

 例子源代码

 以下是托盘类Tray.java


import org.jdesktop.jdic.tray.*;

import com.nci.ir.common.db.QuerierImpl;
import com.nci.midwinter.security.crypto.DefaultCryptoService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Map;

public class Tray implements ActionListener, ItemListener {

 SystemTray tray = SystemTray.getDefaultSystemTray();

 TrayIcon ti;

 JFrame frame;

 public Tray() {
  loadLoginFrame();

 }

 // Returns just the class name -- no package info.
 protected String getClassName(Object o) {
  String classString = o.getClass().getName();
  int dotIndex = classString.lastIndexOf(".");

  return classString.substring(dotIndex + 1);
 }

 public void actionPerformed(ActionEvent e) {
  JMenuItem source = (JMenuItem) (e.getSource());
  String s = source.getText();
  if (s.equalsIgnoreCase("Quit")) {
   System.out.println("Quit menu item selected!");
   System.exit(0);
  } else {
   s = "Action event detected." + "/n" + "    Event source: " + source
     + " (an instance of " + getClassName(source) + ")";

   System.out.println(s);
  }
 }

 public void itemStateChanged(ItemEvent e) {
  JMenuItem source = (JMenuItem) (e.getSource());
  String s = "Item event detected."
    + "/n"
    + "    Event source: "
    + source.getText()
    + " (an instance of "
    + getClassName(source)
    + ")"
    + "/n"
    + "    New state: "
    + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected"
      : "unselected");

  System.out.println(s);
 }

 public static void main(String[] args) {
  new Tray();
 }

 public void loadLoginFrame() {
  // Construct the GUI for balloon message.
  frame = new JFrame("消息提醒用户登录");
  frame.getContentPane().setLayout(new BorderLayout());
  frame.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

  JPanel topPanel = new JPanel();
  topPanel.setBorder(BorderFactory.createEtchedBorder());
  topPanel.setLayout(new BorderLayout());
  topPanel.add(new JLabel("登录名: "), BorderLayout.WEST);
  final JTextField captionField = new JTextField("");

  topPanel.add(captionField, BorderLayout.CENTER);
  // JPanel typePanel = new JPanel();
  // final JComboBox typeBox = new JComboBox(new String[]{"INFO", "ERROR",
  // "WARNING", "NONE" });
  // typePanel.add(new JLabel(" Type:"), BorderLayout.WEST);
  // typePanel.add(typeBox, BorderLayout.EAST);
  // topPanel.add(typePanel, BorderLayout.EAST);
  frame.getContentPane().add(topPanel, BorderLayout.NORTH);

  JPanel messagePanel = new JPanel();
  messagePanel.setLayout(new BorderLayout());
  messagePanel.add(new JLabel("密码:"), BorderLayout.WEST);
  final JPasswordField password = new JPasswordField();
  // final JTextArea messageArea = new JTextArea(5, 20);
  // messageArea.setText("登录名必须输入");

  password.setBorder(BorderFactory.createEtchedBorder());
  messagePanel.add(password);
  frame.getContentPane().add(messagePanel, BorderLayout.CENTER);

  JPanel buttonPanel = new JPanel();
  final JButton okButton = new JButton("登录");
  final JButton cancelButton = new JButton("关闭");
  ActionListener al = new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    if (e.getSource() == cancelButton)
     // System.exit(0);
     frame.setVisible(false);
    else if (e.getSource() == okButton) {
     //MyTimer.userId = captionField.getText();
     
     if(!login(captionField.getText(),password.getText())){
     showTray();
     }else{
       JOptionPane.showMessageDialog(null,"登录失败,请重新登录!","警告",JOptionPane.WARNING_MESSAGE);   


     }
     // ti.displayMessage(captionField.getText(),
     // messageArea.getText(), typeBox.getSelectedIndex());
    }
   }
  };
  okButton.addActionListener(al);
  cancelButton.addActionListener(al);

  buttonPanel.add(okButton);
  buttonPanel.add(cancelButton);
  frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
 }

 public void showTray() {
  JPopupMenu menu;
  JMenu submenu;
  JMenuItem menuItem;
  JRadioButtonMenuItem rbMenuItem;
  JCheckBoxMenuItem cbMenuItem;

  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e) {
   e.printStackTrace();
  }
  if (Integer
    .parseInt(System.getProperty("java.version").substring(2, 3)) >= 5)
   System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
  menu = new JPopupMenu("A Menu");

  // "Quit" menu item
  menu.addSeparator();
  menuItem = new JMenuItem("Quit");
  menuItem.addActionListener(this);
  menu.add(menuItem);

  // ImageIcon i = new ImageIcon("duke.gif");
  ImageIcon i = new ImageIcon(Tray.class.getResource("images/duke.gif"));

  ti = new TrayIcon(i, "任务消息提醒器", menu);

  ti.setIconAutoSize(true);
  ti.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    frame.setVisible(!frame.isVisible());
   }
  });
  ti.addBalloonActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(null,
      "Balloon Message been clicked - TrayIcon", "Message",
      JOptionPane.INFORMATION_MESSAGE);
   }
  });

  tray.addTrayIcon(ti);

  new MyTimer(ti).run();
 }

 /**
  *
  * @return
  */
 public boolean login(String loginId, String pwd) {

  QuerierImpl query = new QuerierImpl();
  String crtptPwd;
  try {
   crtptPwd = new DefaultCryptoService().encrypt(pwd);

   String sql = "select * from ACL_EXTENDED_USER where LOGIN_NAME='"
     + loginId + " and PASSWORD_VALUE='" + crtptPwd + "'";
   java.util.List list = query.selectQuery(sql);
   if (list != null && list.size() == 1) {
    Map map=(Map)list.get(0);
    String userId=(String) map.get("USER_ID");
    MyTimer.userId=userId;
    return true;
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  return false;

 }

}

 以下是定时器类MyTimer.java

/**
 * 定时器
 */

import java.util.*;

import org.jdesktop.jdic.tray.TrayIcon;

import com.nci.ir.common.db.QuerierImpl;

 

/**
 * Created by IntelliJ IDEA. User: zhubj Date: 2003-11-12 Time: 10:20:57 To
 * change this template use Options | File Templates.
 */
public class MyTimer extends TimerTask {
 private  TrayIcon ti;
 
 public static String userId;
 
 public MyTimer(TrayIcon ti){
  this.ti=ti;
 }
 public void run() {

 

   //int numberOfMillisecondsInTheFuture = 10000; // 10 sec
   Date timeToRun = new Date(System.currentTimeMillis());
     //+ numberOfMillisecondsInTheFuture);

   Timer timer = new Timer();
   
   timer.schedule(new TimerTask() {
    public void run() {
     remindMessage();
    }
   }, timeToRun, 30 * 1000);
   //为了测试定时30秒,正式运行请修改实际参数。
   
  }
 

 /**
  * 消息提醒
  *
  */
 public void remindMessage() {
  QuerierImpl query=new QuerierImpl();
  String sql="select * from workflow_workitem ";//where TRANSACTOR !=null and  PARTICIPANTID=登录用户,自己修改
  try {
   long taskSize=query.getRsSize(sql);
   if(taskSize>0){
    ti.displayMessage("任务消息提醒:", "您好,您有"+taskSize+"个待办任务未处理,请及时处理!", 3);
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

 

}

 如果要运行以上程序,请去jdic网站下载组件,jdic.dll,jdic.jar,以及去掉一些相关代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值