Java如何使窗口最小化为图标
2个回答
展开全部
最小化时隐藏,在图标上点右右键选择显示窗口时,让窗口显示出来 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()); }责任编辑:小草
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询