TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
下载了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 <Jan.Struyf@cs.kuleuven.ac.be>"));
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();
}
}
}
源码下载:http://file.javaxxz.com/2014/10/29/235558343.zip |
|