有这么个问题:
Windows(2000,XP)上跑的Client,要求运行后能够缩小到系统托盘上成个小图标呆着,程序状态改变(自动的)后,能改变图标的颜色,比如蓝色图标变成灰色图标。这个用java怎么实现?
给个思路啊~~~~~~
Windows(2000,XP)上跑的Client,要求运行后能够缩小到系统托盘上成个小图标呆着,程序状态改变(自动的)后,能改变图标的颜色,比如蓝色图标变成灰色图标。这个用java怎么实现?
给个思路啊~~~~~~
import java.awt.*;
import javax.swing.*;public class trayDemo extends JFrame
{
private TrayIcon icon = null;
private Image image;
public trayDemo()
{
if(SystemTray.isSupported())
{
super("TrayIcon Demo");
SystemTray tray = SystemTray.getSystemTray();
image = Toolkit.getDefaultToolkit().getImage("image/icon.jpg");
icon = new TrayIcon(image,"测试成功");
tray.add(icon);
this.setSize(300,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else
{
System.out.println("No Supported SystemTray");
}
}
public static void main(String args[])
{
new trayDemo().setVisible(true);
}
}可以试试,但是要在jdk1.6上运行才行。
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;public class TrayIconDemo {
private PopupMenu popup;
private TrayIcon trayIcon;
private SystemTray tray;
public static void main(String[] args) {
TrayIconDemo trayIconDemo = new TrayIconDemo();
}
public TrayIconDemo() {
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();
}
}
}
yuyang2007()的代码是能将应用缩小到任务栏上,但不是系统托盘。
boby(......)的代码达到了目的,能缩小到系统托盘上了,不过图片没显示出来,我弄了个18×18的gif,不过不管用,算是白璧微瑕,我有点儿怀疑是是图片的尺寸不对,boby(......)指点下。
过两天再结贴,看还有没有新主意。
出处在这里:http://www.1cn.biz/index.php?q=node/377在JDK6中 ,AWT新增加了两个类:Desktop和SystemTray,前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定的邮箱发邮件,用默认应用程序打开或编辑文件(比如,用记事本打开以txt为后缀名的文件),用系统默认的打印机打印文档;后者可以用来在系统托盘区创建一个托盘程序.下面代码演示了Desktop和SystemTray的用法./**
*
* @author chinajash
*/
import java.awt.Desktop;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.PopupMenu;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.AWTException;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class DesktopTray {
private static Desktop desktop;
private static SystemTray st;
private static PopupMenu pm;
public static void main(String[] args) {
if(Desktop.isDesktopSupported()){//判断当前平台是否支持Desktop类
desktop = Desktop.getDesktop();
}
if(SystemTray.isSupported()){//判断当前平台是否支持系统托盘
st = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("netbeans.png");//定义托盘图标的图片
createPopupMenu();
TrayIcon ti = new TrayIcon(image, "Desktop Demo Tray", pm);
try {
st.add(ti);
} catch (AWTException ex) {
ex.printStackTrace();
}
}
}
public static void sendMail(String mail){
if(desktop!=null && desktop.isSupported(Desktop.Action.MAIL)){
try {
desktop.mail(new URI(mail));
} catch (IOException ex) {
ex.printStackTrace();
} catch (URISyntaxException ex) {
ex.printStackTrace();
}
}
}
public static void openBrowser(String url){
if(desktop!=null && desktop.isSupported(Desktop.Action.BROWSE)){
try {
desktop.browse(new URI(url));
} catch (IOException ex) {
ex.printStackTrace();
} catch (URISyntaxException ex) {
ex.printStackTrace();
}
}
}
public static void edit(){
if(desktop!=null && desktop.isSupported(Desktop.Action.EDIT)){
try {
File txtFile = new File("test.txt");
if(!txtFile.exists()){
txtFile.createNewFile();
}
desktop.edit(txtFile);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void createPopupMenu(){
pm = new PopupMenu();
MenuItem openBrowser = new MenuItem("Open My Blog");
openBrowser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openBrowser("http://blog.csdn.net/chinajash");
}
});
MenuItem sendMail = new MenuItem("Send Mail to me");
sendMail.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMail("mailto:[email protected]");
}
});
MenuItem edit = new MenuItem("Edit Text File");
sendMail.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
edit();
}
});
MenuItem exitMenu = new MenuItem("&Exit");
exitMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
pm.add(openBrowser);
pm.add(sendMail);
pm.add(edit);
pm.addSeparator();
pm.add(exitMenu);
}
} 如果在Windows中运行该程序,可以看到在系统托盘区有一个图标,右击该图标会弹出一个菜单,点击Open My Blog会打开IE,并浏览"http://blog.csdn.net/chinajash";点击Send Mail to me会打开Outlook Express给我发邮件;点击Edit Text File会打开记事本编辑在程序中创建的文件test.txt
首先谢谢大家。代码我都试验了,汇报下:
yuyang2007()的代码是能将应用缩小到任务栏上,但不是系统托盘。
boby(......)的代码达到了目的,能缩小到系统托盘上了,不过图片没显示出来,我弄了个18×18的gif,不过不管用,算是白璧微瑕,我有点儿怀疑是是图片的尺寸不对,boby(......)指点下。
过两天再结贴,看还有没有新主意。
=============================图片没读到吧
?
anod() ( ) 信誉:93 2007-08-09 09:45:14 得分: 0
首先谢谢大家。代码我都试验了,汇报下:
yuyang2007()的代码是能将应用缩小到任务栏上,但不是系统托盘。
boby(......)的代码达到了目的,能缩小到系统托盘上了,不过图片没显示出来,我弄了个18×18的gif,不过不管用,算是白璧微瑕,我有点儿怀疑是是图片的尺寸不对,boby(......)指点下。
过两天再结贴,看还有没有新主意。=============================图片没读到吧=============================没发现图片找到的报错啊
不知道是不是操作系统的问题,我用2000
http://download.csdn.net/user/swvincent