当前位置: 代码迷 >> J2SE >> 兑现系统托盘图标(不依赖于平台)
  详细解决方案

兑现系统托盘图标(不依赖于平台)

热度:433   发布时间:2016-04-24 16:46:54.0
实现系统托盘图标(不依赖于平台)
在网上看了很多实现系统托盘图标的帖子,但都大同小异:借用dll动态链接库,用JAVA JNI实现。
我想的是有没有用纯JAVA实现的系统图标?它应该不依赖于OS的,能在Windows下实现,在Linux下也可以运行实现的?!

对不起,只有这么点分了!有了可以再加。

------解决方案--------------------
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:chinajash@yahoo.com.cn ");
  }
 });

 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);
}
}
------解决方案--------------------
JDK6.0的系统托盘的雏形其实是个开源的JDIC,其实现原理就是不同的系统调用不同的JNI
不是象LZ那样想的,可以在任何平台上跑,所以也是分支持不支持的,LZ可以用JDK1.6或JDIC
package csdn;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
  相关解决方案