private MainPanel(final JFrame frame) { super(); add(check); setPreferredSize(new Dimension(320, 240)); if (!SystemTray.isSupported()) { frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); return; } frame.addWindowListener( new WindowAdapter() { @Override public void windowIconified(WindowEvent e) { if (check.isSelected()) { e.getWindow().dispose(); } } }); // or // frame.addWindowStateListener(new WindowStateListener() { // @Override public void windowStateChanged(WindowEvent e) { // if (check.isSelected() && e.getNewState() == Frame.ICONIFIED) { // e.getWindow().dispose(); // } // } // }); final SystemTray tray = SystemTray.getSystemTray(); Dimension d = tray.getTrayIconSize(); BufferedImage image = makeBufferedImage(new StarIcon(), d.width, d.height); final PopupMenu popup = new PopupMenu(); final TrayIcon icon = new TrayIcon(image, "TRAY", popup); MenuItem item1 = new MenuItem("OPEN"); item1.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setVisible(true); } }); MenuItem item2 = new MenuItem("EXIT"); item2.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tray.remove(icon); frame.dispose(); // frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); } }); popup.add(item1); popup.add(item2); try { tray.add(icon); } catch (AWTException e) { e.printStackTrace(); } }
/** * Method declaration * * @param s */ private void addToRecent(String s) { for (int i = 0; i < iMaxRecent; i++) { if (s.equals(sRecent[i])) { return; } } if (sRecent[iRecent] != null) { mRecent.remove(iRecent); } sRecent[iRecent] = s; if (s.length() > 43) { s = s.substring(0, 40) + "..."; } MenuItem item = new MenuItem(s); item.setActionCommand("#" + iRecent); item.addActionListener(this); mRecent.insert(item, iRecent); iRecent = (iRecent + 1) % iMaxRecent; }
/** * Method declaration * * @param f * @param m */ void addMenuItems(Menu f, String m[]) { for (int i = 0; i < m.length; i++) { MenuItem item = new MenuItem(m[i].substring(1)); char c = m[i].charAt(0); if (c != '-') { item.setShortcut(new MenuShortcut(c)); } item.addActionListener(this); f.add(item); } }