随笔 - 20  文章 - 12  trackbacks - 0
<2006年3月>
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(1)

随笔档案(20)

java论坛

我的朋友

最新随笔

搜索

  •  

积分与排名

  • 积分 - 14863
  • 排名 - 1997

最新评论

阅读排行榜

评论排行榜

网上主要流传有两种方式实现系统托盘:
1.Windows Tray Icon (http://jeans.studentenweb.org/java/trayicon/trayicon.html)
shot-balloon.gif

2.SysTray for Java (http://systray.sourceforge.net/)
shot.png

这两个都是开源的...可以根据上面的下载.

相对来说,我更喜欢SysTray for Java,原因很简单,SysTray for Java实现了我所要的功能而且相对来说比Windows Tray Icon 要简单.

使用SysTray是很简单的.下载下来的文件有个例子Example.java,照着这个实现你所需要的功能应该不算困难.

主要是菜单和按钮的操作,和操作一般的JFrame一样.

下面是一个例子程序:

package qiya.systray;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import snoozesoft.systray4j.SysTrayMenu;
import snoozesoft.systray4j.SysTrayMenuEvent;
import snoozesoft.systray4j.SysTrayMenuIcon;
import snoozesoft.systray4j.SysTrayMenuItem;
import snoozesoft.systray4j.SysTrayMenuListener;

public class MainFrame extends JFrame implements ActionListener,
  SysTrayMenuListener {

 static final int INIT_WIDTH = 400;// 默认窗口宽度

 static final int INIT_HEIGHT = 244;// 默认窗口高度

 private static final String toolTip = "宽带计费接口";// 提示文字

 static final SysTrayMenuIcon icon = new SysTrayMenuIcon("rocket.gif");// 图片信息

 SysTrayMenu menu;// 菜单

 private JButton launchButton = new JButton();// 启动按钮

 private JButton exitButton = new JButton();// 退出按钮

 private JLabel statusLabel = new JLabel();// 运行状态

 public static void main(String[] args) {
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (InstantiationException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (UnsupportedLookAndFeelException e) {
   e.printStackTrace();
  }

  new MainFrame();
 }

 public MainFrame() {
  super("宽带计费接口");// 标题
  setIconImage(new ImageIcon(getClass().getResource("rocket.gif"))
    .getImage());// 图标
  this.setLayout(null);
  this.setSize(new Dimension(INIT_WIDTH, INIT_HEIGHT));
  Dimension dimScreen = Toolkit.getDefaultToolkit().getScreenSize();
  int xPos = (dimScreen.width - INIT_WIDTH) / 2;
  int yPos = (dimScreen.height - INIT_HEIGHT) / 2;

  statusLabel.setText("系统状态监视");
  statusLabel.setBounds(new Rectangle(45, 35, 280, 40));
  statusLabel.setToolTipText("当前系统的运行状态");
  statusLabel.setFont(new Font("宋体", 0, 14));

  launchButton.setText("启动");
  launchButton.setBounds(new Rectangle(80, 180, 80, 23));

  exitButton.setText("退出");
  exitButton.setBounds(new Rectangle(230, 180, 80, 23));

  this.getContentPane().add(statusLabel, null);
  this.getContentPane().add(launchButton, null);
  this.getContentPane().add(exitButton, null);

  launchButton.addActionListener(this);
  exitButton.addActionListener(this);

  this.setBounds(xPos, yPos, INIT_WIDTH, INIT_HEIGHT);

  icon.addSysTrayMenuListener(this);
  this.createMenu();
  this.setVisible(true);
 }

 /**
  * 创建菜单
  *
  */
 private void createMenu() {
  SysTrayMenuItem subItem1 = new SysTrayMenuItem("退出", "退出");
  subItem1.addSysTrayMenuListener(this);

  SysTrayMenuItem subItem2 = new SysTrayMenuItem("关于", "关于");
  subItem2.addSysTrayMenuListener(this);

  SysTrayMenuItem subItem3 = new SysTrayMenuItem("帮助", "帮助");
  subItem3.addSysTrayMenuListener(this);

  menu = new SysTrayMenu(icon, toolTip);// 生成菜单
  menu.addItem(subItem1);
  menu.addSeparator();
  menu.addItem(subItem2);
  menu.addItem(subItem3);
 }

 /**
  * 点击按钮事件
  */
 public void actionPerformed(ActionEvent e) {
  if (e.getActionCommand().equalsIgnoreCase("退出")) {
   System.exit(0);
  } else if (e.getActionCommand().equalsIgnoreCase("启动")) {
   // 启动计费程序

  }
 }

 /**
  * 菜单选择事件
  */
 public void menuItemSelected(SysTrayMenuEvent e) {
  if (e.getActionCommand().equalsIgnoreCase("退出")) {
   System.exit(0);
  } else if (e.getActionCommand().equalsIgnoreCase("关于")) {
   JOptionPane.showMessageDialog(this, "宽带计费接口" + "完成于2005-3-27");
  } else if (e.getActionCommand().equalsIgnoreCase("帮助")) {
   JOptionPane.showMessageDialog(this, "宽带计费接口" + "帮助文件待写...");
  }
 }

 /**
  * 左键单击事件
  */
 public void iconLeftClicked(SysTrayMenuEvent e) {
  if (this.isVisible()) {// 如果可见,最小化
   this.setVisible(false);
  } else {// 如果不可见显示出来
   this.setVisible(true);
  }
 }

 /**
  * 左键双击事件
  */
 public void iconLeftDoubleClicked(SysTrayMenuEvent e) {
  if (this.isVisible()) {// 如果可见,最小化
   this.setVisible(false);
  } else {// 如果不可见显示出来
   this.setVisible(true);
  }
 }
}


tray.gif
posted on 2006-03-28 12:39 阅读(229) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: