当前位置: 代码迷 >> J2SE >> 求java简单的托盘程序解决办法
  详细解决方案

求java简单的托盘程序解决办法

热度:34   发布时间:2016-04-24 14:02:15.0
求java简单的托盘程序
谁有java托盘程序,没有有什么要求的,就是能简单的实现最小化早右下脚,实现数据库的查询就可以了,如果没有实现数据库的查询,那就先求一个最简单的托盘程序!

------解决方案--------------------

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class TrayIcon {

private PopupMenu popup;
private TrayIcon trayIcon;
private SystemTray tray;

public static void main(String[] args) {
TrayIcon trayIconDemo = new TrayIcon();
}

public TrayIcon() {
if (!SystemTray.isSupported()) {
System.out.println( "SystemTray is not supported ");
return;
}
popup = new PopupMenu();
trayIcon = new TrayIcon(createImage( "1.gif ", "tray icon "));
tray = SystemTray.getSystemTray();

CheckboxMenuItem cb1 = new CheckboxMenuItem( "Set auto size ");
MenuItem cb2 = new MenuItem( "change Icon ");
MenuItem exitItem = new MenuItem( "Exit ");

popup.add(cb1);
popup.add(cb2);
popup.addSeparator();
popup.add(exitItem);

trayIcon.setPopupMenu(popup);

try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println( "TrayIcon could not be added. ");
return;
}

trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This dialog box is run from System Tray ");
}
});

cb1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int cb1Id = e.getStateChange();
if (cb1Id == ItemEvent.SELECTED){
trayIcon.setImageAutoSize(true);
} else {
trayIcon.setImageAutoSize(false);
}
}
});

cb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.setImage(createImage( "2.gif ", "tray icon "));
}
});

exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon);
System.exit(0);
}
});
}

protected Image createImage(String path, String description) {
URL imageURL = TrayIconDemo.class.getResource(path);

if (imageURL == null) {
System.err.println( "Resource not found: " + path);
return null;
} else {
return (new ImageIcon(imageURL, description)).getImage();
}
}
}

  相关解决方案