首页
论坛
图书
开发资料
在线文档
网址
下载
联系我们
 新闻│Java│JavaScript│Eclipse│Eclipse 英文│J2EE│J2ME│J2SE│JSP│Netbeans│Hibernate│JBuilder│Spring│Struts
站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → Windows系统托盘图标实践(AWT)

Windows系统托盘图标实践(AWT)

摘要: Windows系统托盘图标实践(AWT)

</td> </tr> <tr> <td width="524" height="35" valign="top" class="ArticleTeitle"> 下载了trayicon-1.7.9b,大略看了它的例子(源码没有研究),以下程序仅供参考。
让我们来给下面简单的程序"Hello World!"加上Windows系统的托盘图标吧!

import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class TrayIconTest extends Frame{

Label lab=new Label("Hello World!!!");

public TrayIconTest(){
add(lab);
setSize(400,300);
addWindowListener(new MywindowListener());

}

class MywindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}

public static void main(String[] args){
TrayIconTest test=new TrayIconTest();
test.show();
}
}

以下主要添加了托盘图标及弹出菜单:(运行这个程序时需要图像文件,TrayIcon12.dll,包

com.jeans.trayicon,还要设置好classpath,请下载我的文件夹)。


import java.awt.*;
import java.awt.event.*;
import java.io.*;

import com.jeans.trayicon.*;

public class TrayIconTest extends Frame{

protected WindowsTrayIcon icon;//托盘图标
Label lab=new Label("Hello World!!!");

public TrayIconTest() throws TrayIconException, InterruptedException{
add(lab);
Image france = loadImage("Duke16.gif");
icon = new WindowsTrayIcon(france, 16, 16);
icon.setToolTipText("Hello");//设置托盘图标的提示信息
icon.setPopup(makePopup());//给托盘图标加右键菜单
icon.addActionListener(new TrayIconListener());//单击托盘图标时的动作
icon.setVisible(true);//托盘图标设置为显示状态
WindowsTrayIcon.keepAlive();
setSize(400,300);
addWindowListener(new MywindowListener());

}



public static void main(String[] args){
try {
String appName = "TestTray";
//通过托盘图标库,设置发送信息的回调方法。
long result = WindowsTrayIcon.sendWindowsMessage(appName, 1234);
if (result != -1) {
System.out.println("Already running other instance of "+appName+
"(returns: "+result+")");
return;
}
WindowsTrayIcon.initTrayIcon(appName);

TrayIconTest test = new TrayIconTest();
test.show();
} catch (TrayIconException e) {
System.out.println("Error: "+e.getMessage());
} catch (InterruptedException e) {
}
}


// 装载gif图像
public static Image loadImage(String fileName) {
return Toolkit.getDefaultToolkit().getImage("demo"+File.separator+"images"+
File.separator+fileName);
}

//单击托盘图标时显示应用程序的窗口
private class TrayIconListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
setVisible(true);
// Request input focus
requestFocus();
}

}
//关闭程序窗口的动作设置为隐藏窗口
private class MywindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
setVisible(false);
// System.exit(0);
}
}

public TrayIconPopup makePopup() {//构造弹出菜单
TrayIconPopup popup = new TrayIconPopup();
// 增加三个菜单项
TrayIconPopupSimpleItem item = new TrayIconPopupSimpleItem("&About");
item.addActionListener(new AboutListener());
popup.addMenuItem(item);
item = new TrayIconPopupSimpleItem("&Configure");
item.addActionListener(new ConfigureListener());
popup.addMenuItem(item);
// 增加分隔线
TrayIconPopupSeparator sep = new TrayIconPopupSeparator();
popup.addMenuItem(sep);
item = new TrayIconPopupSimpleItem("E&xit");
item.addActionListener(new ExitListener());
popup.addMenuItem(item);
return popup;
}

private class ExitListener implements ActionListener {//弹出菜单中的Exit操作

public void actionPerformed(ActionEvent evt) {
WindowsTrayIcon.cleanUp();
System.exit(0);
}

}


private class AboutListener implements ActionListener {//弹出菜单中的About操作

public void actionPerformed(ActionEvent evt) {
System.out.println("About selected.");
DemoAboutBox box = new DemoAboutBox();
//centerDialog(box);
box.show();
}

}


private class ConfigureListener implements ActionListener {//弹出菜单中的Configure操作

public void actionPerformed(ActionEvent evt) {
ConfigureBox box = new ConfigureBox();
box.show();
}

}

}

class ConfigureBox extends Frame {//弹出的对话框1

public ConfigureBox() {
super("Configure TrayIcon");
setBackground(SystemColor.control);
setLayout(new GridLayout(0,1));
add(new Label("Hello"));
addWindowListener(new CloseWindowListener());
pack();
setSize(300,200);

}

private class CloseWindowListener extends WindowAdapter {

public void windowClosing(WindowEvent evt) {
dispose();
}

}

}


class DemoAboutBox extends Frame {//弹出的对话框2

// Create new about box given parent frame
public DemoAboutBox() {
super("About TrayIcon");
setBackground(SystemColor.control);
setLayout(new GridLayout(0,1,3,3));
add(new Label("TrayIcon version "+WindowsTrayIcon.TRAY_VERSION));
add(new Label("Written by Jan Struyf "));
Panel buttons = new Panel();
buttons.setLayout(new FlowLayout());
Button button = new Button("OK");
button.addActionListener(new CloseListener());
buttons.add(button);
add(buttons, BorderLayout.SOUTH);
addWindowListener(new CloseWindowListener());
pack();
}
private class CloseListener implements ActionListener {

public void actionPerformed(ActionEvent evt) {
dispose();
}

}


private class CloseWindowListener extends WindowAdapter {

public void windowClosing(WindowEvent evt) {
dispose();
}

}
}


function TempSave(ElementID) { CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value); CommentsPersistDiv.save("CommentXMLStore"); } function Restore(ElementID) { CommentsPersistDiv.load("CommentXMLStore"); document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent"); } </td> <td width="160" valign="top" class="ArticleTeitle">
</td> </tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">


↑返回目录
前一篇: Windows系统托盘图标实践(Swing)
后一篇: 在Java中运用Hashtable

首页 | 全站 Sitemap | 联系我们 | 设为首页 | 收藏本站
版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved