系统托盘图标图片问题

所用到的主要java类:

SystemTray   TrayIcon  ImageIO

源代码部分:

package com.hxtch;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyServer {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
     ServerFrame sFrame=new ServerFrame();
     sFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     sFrame.setVisible(true);
     System.out.println("UserDir :"+System.getProperty("user.dir"));
     System.out.println("User "+ new java.sql.Date(System.currentTimeMillis()));
  
 }

}
/**
 *
 *服务器端窗体
 */
 class ServerFrame extends JFrame{
 
 private static final   int c=0;
 private JButton btnStart;
 private JButton btnStop;
 private Thread trd;
 private SystemTray st;
 private TrayIcon icon;
 private PopupMenu pmenu;
 private boolean isStart=false;
 public ServerFrame(){
  btnStart=new JButton("启动服务器");
  btnStop=new JButton("停止服务器");
  trd=new Thread(new SendThread());
  
  JPanel p=new JPanel();
  
  btnStart.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    startServer();
   }
   
  });
  btnStop.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    stopServer();
   }
   
  });
  p.add(btnStart);
  p.add(btnStop);
  this.getContentPane().add(p, BorderLayout.CENTER);
  
  pack();
  setTray();
//  this.dispose();
 }
 public void startServer(){
  System.out.println("服务器启动.......");
  if(!isStart){
   trd.start();
   }else {
    if(!trd.isAlive()){
     trd.resume();
     System.out.println("Thread "+trd.getName()+" is alive!");
    }
   }
 
 }
 public void stopServer(){
  System.out.println("服务器停止.......");
  trd.stop();
  isStart=true;
 }
 
 public void setTray(){
 if(SystemTray.isSupported()){
  st=SystemTray.getSystemTray();
  this.pmenu=new PopupMenu();
  MenuItem exitM=new MenuItem("退出");
  MenuItem startM=new MenuItem("启动");
  pmenu.add(exitM);
  exitM.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    System.out.println("System exit ...");
    System.exit(0);
    
   }
  });
  startM.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    System.out.println("System start ...");
    
    
   }
  });
  pmenu.add(startM);
  
  try{
  icon=new TrayIcon(Toolkit.getDefaultToolkit().createImage("tomcat.gif"));
  
  icon.setPopupMenu(pmenu);
  icon.setToolTip("This is my first Icon.");
  
  st.add(icon);
  
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 }
}
 
 class SendThread implements Runnable{

 private ServerSocket server;
 private Socket socket;
 public SendThread(){
  try{
  server=new ServerSocket(5000);
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 public void run() {
  // TODO Auto-generated method stub
  try{
  socket=server.accept();
  OutputStream ops=socket.getOutputStream();
  System.out.println("Send data is :"+getData());
  PrintWriter pw=new PrintWriter(ops);
     while(true)
     {
   pw.println(getData());
   
   pw.flush();
   Thread.currentThread().sleep(1000);
     }
  
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public String getData(){
  StringBuffer sb=new StringBuffer();
  Random rm=new Random();
  for(int i=1;i<=7;i++){
   sb.append((int)(rm.nextDouble()*100));
   sb.append(",");
  }
  sb.deleteCharAt(sb.length()-1);
  sb.append(";");
  return sb.toString();
 }

 }
 
 

错误现象:
桌面右下角有图标,但不显示图片或显示白色。

这个程序写调试了一天的时间还是没有成功,第二天终于找到了出错的原因,原来我用ImageIO读取的图片像素太大,用ACD处理之后,可爱的Tomcat.gif 图片终于显示了。

你可能感兴趣的:(系统托盘图标图片问题)