Java Swing implementation minimizes system to system tray
Last Update:2018-07-25
Source: Internet
Author: User
Source: * * mainframe.java * * created on 2008 year September 20, Morning 11:44 */ package com.topking.tray.ui; Import & nbsp;java.awt.awtexception; Import java.awt.Image; Import java.awt.MenuItem; Import java.awt.PopupMenu; Import java.awt.SystemTray; Import java.awt.TrayIcon; Import java.awt.event.ActionEvent; Import java.awt.event.actionlistener; Import java.awt.event.MouseEvent; Import java.awt.event.mouselistener; Import java.awt.event.WindowEvent; Import java.awt.event.windowlistener; Import javax.swing.ImageIcon; /** * * @author lzkj */ public CLass MainFrame extends javax.swing.JFrame implements ActionListener, windowlistener{ // Variables declaration - Do not modify private javax.swing.JLabel L_img; private javax.swing.JLabel L_img2; Private PopupMenu pop; private MenuItem open,close; private TrayIcon trayicon; // end of variables declaration /** creates new form mainframe */ public mainframe () { this. SettitlE ("Java Implementation system Tray example"); this setlocation (+); initcomponents (); } /** This method is called from Within the constructor to * initialize the Form. * warning: do not modify this code. the content of this method is * always Regenerated by the form editor. */ @SuppressWarnings ("unchecked") // <editor-fold defaultstate= "collapsed" desc= "Generated Code "> Private void initcomponents () { // L_img = new javax.swing.jlabel (New imageicon (Mainframe.class). GetResource ("com/topking/ Tray/images/netbean1.png ")); // l_img2 = new javax.swing.jlabel (New ImageIcon ( Mainframe.class). GetResource ("Com/topking/tray/images/netbean2.png")); l_img = new javax.swing.jlabel (new imageicon (this. getclass (). getClassLoader (). GetResource ("Com/topking/tray/images/netbean1.png")); l_img2 = new javax.swing.jlabel (New imaGeicon (this. getclass (). getClassLoader (). GetResource ("Com/topking/tray/images/netbean2.png")); pop = new PopupMenu (); open = new MenuItem ("Open "); open.addactionlistener (this); close = new menuitem ("Off"); Close.addactionlistener (This); pop.add (Open); pop.add (Close);   if (systemtray.issupported ()) { SystemTray tray = Systemtray.getsystemtray (); image icon = this. Gettoolkit (). GetImage (this. getclass (). getClassLoader (). getresource ("com /topking/tray/images/user_edit.png "); trayicon = new trayicon (icon, "System Tray sample (Java)", pop); trayicon.addmouselistener (New MouseListener () { public void mouseclicked (mouseevent e) { // todo auto-generated Method stub if (E.getclickcount () = 2) { openframe (); } } public void mouseentered (MouseEvent e) { &nbSp; // todo auto-generated method stub } public void mouseexited (mouseevent e) { // TODO Auto-generated method stub } &nbsP; public void mousepressed (mouseevent e) { // todo auto-generated method stub } public void mousereleased (mouseevent e) { // TODO Auto-generated method stub } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP}); try { Tray.add (TrayIcon); } Catch (awtexception e) { // todo auto-generated catch block e.printstacktrace (); } } Javax.swing.grouplayout layout = new javax.swing.grouplayout (GetContentPane ()); getcontentpane (). setlayout (layout); layout.sethorizontalgroup ( layout.createparallelgroup (Javax.swing.GroupLayout.Alignment.LEADING ) .addgroup ( Javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup () &nBsp; .addcontainergap () .addgroup (Layout.createparallelgroup ( Javax.swing.GroupLayout.Alignment.TRAILING) .addcomponent (L_img2, javax.swing.grouplayout.alignment.leading, javax.swing.grouplayout.default_size, 380, Short.MAX _value) .addcomponent (l_img, javax.swing.grouplayout.alignment.leading, javax.swing.grouplayout.default_size, 380, short.max_value) .addcontainergap ()) ); layout.setverticalgroup ( layout.createparallelgroup ( Javax.swing.GroupLayout.Alignment.LEADING) .addgroup (Layout.createsequentialgroup () .addcontainergap () .addcomponent (L_img) .addgap (29, , ) .addcomponent (l_img2, javax.swing.grouplayout.preferred_size, 222, Javax.swing.GroupLayout.PREFERRED_SIZE) .addcontainergap (39 , short.max_value)) ); pack (); }// </ editor-fold> /** * @param args the command line arguments */ public static void main (string args[]) { java.awt.eventqueue.invokelater (New runnable () { public void run () { New &NBsp MainFrame () setvisible (true); }  }); } public void actionperformed (Actionevent e) { // TODO Auto-generated Method stub if (E.getsource () ==open) { openframe (); } if ( E.getsource () ==close) { System.exit ( -1); } } &nbsP public void openframe () { this. setvisible (True); this . Setalwaysontop (True); } public void windowactivated (windowevent arg0) { // todo auto-generated method stub } public void windowclosed (windowevent arg0) { // todo auto-generated method stub this. setvisible (False); this.Dispose (); } public void windowclosing (windowevent arg0) { // todo auto-generated method stub } public void windowdeactivated (windowevent arg0) { // todo auto-generated method stub } public void windowdeiconified (windowevent arg0) { // todo auto-generated method stub } //window minimized public void windowiconified (WINDOWEVENT&NBSP;ARG0) { // todo auto-generated method stub this. Dispose () ; } public void WindowOpened (windowevent arg0) { // Todo auto-generated method stub } }