Let's start with a classic Hello World example:
1 Packagecom.xxx.yyy.zzz;2 3 ImportJavax.swing.JFrame;4 5 Public classMainFrameextendsJFrame {6 7 Private Static Final LongSerialversionuid = -3059928131346032935l;8 PublicMainFrame () {9 Super();Ten This. setSize (300, 200); One This. Getcontentpane (). setlayout (NULL); A This. Settitle ("Hello world!"); - ///Set the behavior when clicking "Close" in the upper right corner of the window, stop the main loop when you click "Close", exit the program - This. Setdefaultcloseoperation (jframe.exit_on_close); the } - } - - classMain { + Public Static voidMain (string[] args) { -MainFrame frm =NewMainFrame (); +Frm.setvisible (true); A } at}
Compile and run the above code under Eclipse and display the following small window:
Many applications will reduce the window to a small area (tray) near the digital clock of the taskbar when clicking on the "Minimize" button. Since Java version 1.6, two new classes have been added to AWT: Systemtray,trayicon. With these two classes, Java can also use the tray icon.
With TrayIcon, you need to prepare a small icon. JPG, PNG and other general image format can be used, does not support the m$ ico. Do not know the size of the picture there is no requirement, I prepared is a 32*32 png, gestures, the size of the tray icon seems to be 16*16, it is possible that Java will be in the internal conversion it.
Here is the code after adding TrayIcon, which hides the window when minimized, displays the tray icon, and when the icon is clicked, the icon disappears and the window is restored. But unfortunately the icon in the tray only a blank, even if replaced by 16*16 picture is also the case.
1 ///Implement Interface WindowListener2 Public classMainFrameextendsJFrameImplementswindowlistener{3 4 Private Static Final LongSerialversionuid = -3059928131346032935l;5 ///32*32 PNG image6 Private StaticImage image = Toolkit.getdefaulttoolkit (). GetImage ("/c_cyan.png");7 ///TrayIcon Object8 Private StaticTrayIcon TrayIcon =NewTrayIcon (Image, "XX assistant");9 Ten PublicMainFrame () { One Super(); A This. setSize (300, 200); - This. Getcontentpane (). setlayout (NULL); - This. Settitle ("Hello world!"); the This. Setdefaultcloseoperation (jframe.exit_on_close); - ///Register window related event listener - This. Addwindowlistener ( This); - } + - Public voidwindowactivated (WindowEvent e) {} + A Public voidwindowclosed (WindowEvent e) {} at - Public voidwindowclosing (WindowEvent e) {} - - Public voidwindowdeactivated (WindowEvent e) {} - - Public voidwindowdeiconified (windowevent e) { in } - to Public voidwindowiconified (windowevent e) { + ///detects whether the operating system supports the system tray - if(!systemtray.issupported ()) { the return; * } $ Panax Notoginseng FinalMainFrame parent = This; - FinalSystemtray Systemtray =Systemtray.getsystemtray (); the + ///Set TrayIcon automatically adjust the image size, it seems that after this setting no longer focus on the image size ATrayicon.setimageautosize (true); the ///TrayIcon Add a listener to the event, click to restore the main window, delete the TrayIcon +Trayicon.addactionlistener (NewActionListener () { - Public voidactionperformed (ActionEvent event) { $ ///Show main window $Parent.setvisible (true); - ///Remove TrayIcon from the system tray because the same trayicon cannot be added two times - Systemtray.remove (TrayIcon); the } - });Wuyi the Try { - ///Add TrayIcon to the system tray Wu Systemtray.add (TrayIcon); -}Catch(Awtexception exception) { About exception.printstacktrace (); $ } - - ///Hide This window -Parent.setvisible (false); A } + the Public voidwindowopened (WindowEvent e) {} -}
The Hello world example of swing with trayicon (small tray icon)