java Swing最小化到工作列圖示,單擊顯示
阿新 • • 發佈:2019-01-09
private static final long serialVersionUID = 1L; private static TrayIcon trayIcon = null; static JFrame mf = new JFrame(); static SystemTray tray = SystemTray.getSystemTray(); public static void myFrame() { // 窗體 mf.setLocation(300, 100); mf.setSize(500, 300); mf.setTitle("hello world"); mf.setVisible(true);// 使視窗可見 mf.addWindowListener(new WindowAdapter() { // 視窗關閉事件 public void windowClosing(WindowEvent e) { System.exit(0); }; public void windowIconified(WindowEvent e) { // 視窗最小化事件 mf.setVisible(false); MServer.miniTray(); } }); } private static void miniTray() { // 視窗最小化到工作列托盤 ImageIcon trayImg = new ImageIcon("");// 托盤圖示 trayIcon = new TrayIcon(trayImg.getImage(), "test", new PopupMenu()); trayIcon.setImageAutoSize(true); trayIcon.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 1) {// 單擊 1 雙擊 2 tray.remove(trayIcon); mf.setVisible(true); mf.setExtendedState(JFrame.NORMAL); mf.toFront(); } } }); try { tray.add(trayIcon); } catch (AWTException e1) { e1.printStackTrace(); } }