Java使窗口最小化为图标

最小化时隐藏,在图标上点右右键选择显示窗口时,让窗口显示出来

  import Java.awt.AWTException;

  import java.awt.Image;

  import java.awt.MenuItem;

  import java.awt.SystemTray;

  import java.awt.Toolkit;

  import java.awt.TrayIcon;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import java.awt.image.BufferedImage;

  import java.io.File;

  import java.io.IOException;

  import javax.imageio.ImageIO;

  public class SystemTrayTest extends Thread {

  TrayIcon st;

  SystemTray tray =null;

  //javax.swing.Popup p;

  java.awt.PopupMenu popup;

  MenuItem mi1,mi2;

  msgbox m=null;

  public void getm(msgbox m){

  this.m=m;

  }

  public void run() {

  // TODO Auto-generated method stub

  super.run();

  if (SystemTray.isSupported()) {

  // get the SystemTray instance

  tray = SystemTray.getSystemTray();

  // load an image

  String test = System.getProperty("user.dir").replace('\\', '/');

  BufferedImage src = null;

  try {

  src = ImageIO.read(new File(test + "\\logo.jpg"));

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } // 读入源图像

  //

  Image image = Toolkit.getDefaultToolkit().getImage(test + "\\logo.jpg");

  // create a action listener to listen for default action executed on

  // the tray icon

  popup=new java.awt.PopupMenu();

  mi1=new MenuItem("显示窗口");

  mi2=new MenuItem("---");

  popup.add(mi1);

  popup.add(mi2);

  mi1.addActionListener(new ActionListener(){

  public void actionPerformed(ActionEvent e) {

  // TODO Auto-generated method stub

  m.setVisible(true);

  System.out.println("open");

  }

  });

  mi2.addActionListener(new ActionListener(){

  public void actionPerformed(ActionEvent e) {

  // TODO Auto-generated method stub

  //System.out.println("close");

  //System.exit(0);

  }

  });

  st = new TrayIcon(image, "Tray Demo", popup);

  try {

  tray.add(st);

  } catch (AWTException e) {

  e.printStackTrace();

  }

  }

  }

  public static void main(String[] args) {

  new SystemTrayTest().start();

  }

  }

  //使窗口最小化,并隐藏

  public void windowIconified(WindowEvent e) {

  System.out.print("sssssss");

  jf.setVisible(false);

  System.out.print(""+e+" "+e.getClass()+" "+e.getNewState());

  }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpjgx.html