栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在任务栏的系统托盘中隐藏JFrame

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在任务栏的系统托盘中隐藏JFrame

import java.awt.*;import java.awt.event.*;import javax.swing.Jframe;import javax.swing.UIManager;public class HideToSystemTray extends Jframe{    TrayIcon trayIcon;    SystemTray tray;    HideToSystemTray(){        super("SystemTray test");        System.out.println("creating instance");        try{ System.out.println("setting look and feel"); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        }catch(Exception e){ System.out.println("Unable to set LookAndFeel");        }        if(SystemTray.isSupported()){ System.out.println("system tray supported"); tray=SystemTray.getSystemTray(); Image image=Toolkit.getDefaultToolkit().getImage("/media/faisal/DukeImg/Duke256.png"); ActionListener exitListener=new ActionListener() {     public void actionPerformed(ActionEvent e) {         System.out.println("Exiting....");         System.exit(0);     } }; PopupMenu popup=new PopupMenu(); MenuItem defaultItem=new MenuItem("Exit"); defaultItem.addActionListener(exitListener); popup.add(defaultItem); defaultItem=new MenuItem("Open"); defaultItem.addActionListener(new ActionListener() {     public void actionPerformed(ActionEvent e) {         setVisible(true);         setExtendedState(Jframe.NORMAL);     } }); popup.add(defaultItem); trayIcon=new TrayIcon(image, "SystemTray Demo", popup); trayIcon.setImageAutoSize(true);        }else{ System.out.println("system tray not supported");        }        addWindowStateListener(new WindowStateListener() { public void windowStateChanged(WindowEvent e) {     if(e.getNewState()==ICONIFIED){         try {  tray.add(trayIcon);  setVisible(false);  System.out.println("added to SystemTray");         } catch (AWTException ex) {  System.out.println("unable to add to tray");         }     }        if(e.getNewState()==7){         try{ tray.add(trayIcon); setVisible(false); System.out.println("added to SystemTray"); }catch(AWTException ex){ System.out.println("unable to add to system tray");        } }        if(e.getNewState()==MAXIMIZED_BOTH){         tray.remove(trayIcon);         setVisible(true);         System.out.println("Tray icon removed");     }     if(e.getNewState()==NORMAL){         tray.remove(trayIcon);         setVisible(true);         System.out.println("Tray icon removed");     } }        });        setIconImage(Toolkit.getDefaultToolkit().getImage("Duke256.png"));        setVisible(true);        setSize(300, 200);        setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);    }    public static void main(String[] args){        new HideToSystemTray();    }}

这是工作程序!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485907.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号