Project Structure:
Running effect:
========================================================== ======================================
The following is the code:
========================================================== ======================================
/Tray/src/com/b510/tray/export topcapture. javaCopy codeThe Code is as follows: package com. b510.tray tray;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. WindowAdapter;
Import java. awt. event. javaswevent;
Import javax. swing. JFrame;
Import javax. swing. JOptionPane;
Import javax. swing. UIManager;
Public class implements topcapture extends JFrame implements ActionListener {
/**
* 1. Change double-click tray to click tray 2. Fixed no response BUG after tray 3. Move the magnifier prompt box to the upper left corner and upper right corner
*/
Private static final long serialVersionUID = 1L;
// JButton confirm;
// BufferedImage implements topimg;
MyTray tray;
Boolean iconed = false;
Public writable topcapture (){
Super ("EasyCapture ");
Init ();
// When you click the "-" Minimize button, the system minimizes the number to the tray.
AddWindowListener (new WindowAdapter (){
Public void incluwiconified (incluwevent e ){
Iconed = true;
SetVisible (false );
}
// When you click "X" to close the window, you will be asked if you want to minimize the number of pallets.
// Yes, indicating to minimize to the tray; no, indicating to exit
Public void windowClosing (WindowEvent e ){
Int option = JOptionPane. showConfirmDialog (export topcapture. this,
"Minimize to pallets? "," Prompt: ", JOptionPane. YES_NO_OPTION );
If (option = JOptionPane. YES_OPTION ){
Iconed = true;
SetVisible (false );
} Else {
System. exit (0 );
}
}
});
Pack ();
SetSize (350,230 );
SetLocation (500,300 );
Setdefaclocloseoperation (JFrame. DISPOSE_ON_CLOSE );
SetResizable (false );
SetVisible (true );
}
Void init (){
Tray = new MyTray (fig. this );
}
//
Public void capture (){
}
Public static void main (String [] args ){
// TODO Auto-generated method stub
Try {
UIManager. setLookAndFeel (UIManager. getSystemLookAndFeelClassName ());
Using topcapture desk = new using topcapture ();
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
@ Override
Public void actionreceivmed (ActionEvent e ){
}
}
/Tray/src/com/b510/tray/MyTray. java
Copy codeThe Code is as follows: package com. b510.tray tray;
Import 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 javax. swing. ImageIcon;
Import javax. swing. JFrame;
Public class MyTray implements ActionListener, MouseListener {
Private Image icon; // icon
Private TrayIcon trayIcon;
Private SystemTray systemTray; // System Tray
Private sector topcapture frame; // The main form to which the tray belongs
Private PopupMenu pop = new PopupMenu (); // the pop-up menu
Private MenuItem capture = new MenuItem ("capture ");
Private MenuItem show = new MenuItem ("open ");
Private MenuItem exit = new MenuItem ("exit ");
Public MyTray (writable topcapture frame ){
This. frame = frame;
// Icon = Toolkit. getDefaultToolkit (). getImage ("./images/xiaomai.png ");
Icon = new ImageIcon (this. getClass (). getClassLoader (). getResource (
"Image/xiaomai.png"). getImage ();
If (SystemTray. isSupported ()){
SystemTray = SystemTray. getSystemTray ();
TrayIcon = new TrayIcon (icon, "click-EasyCapture", pop );
Pop. add (capture );
Pop. add (show );
Pop. add (exit );
Try {
SystemTray. add (trayIcon );
} Catch (AWTException e1 ){
E1.printStackTrace ();
TrayIcon. addMouseListener (this );
}
}
TrayIcon. addMouseListener (this );
Show. addActionListener (this );
Exit. addActionListener (this );
Capture. addActionListener (this );
}
@ Override
Public void actionreceivmed (ActionEvent e ){
If (e. getSource () = show ){
Frame. iconed = false;
Frame. setVisible (true );
Frame. setExtendedState (JFrame. NORMAL );
} Else if (e. getSource () = capture ){
Frame. capture ();
} Else {
System. exit (0 );
}
}
// ����
@ Override
Public void mouseClicked (MouseEvent e ){
If (e. getClickCount () = 1 & e. getButton ()! = MouseEvent. BUTTON3 ){
Frame. capture ();
}
}
@ Override
Public void mouseEntered (MouseEvent arg0 ){
// TODO Auto-generated method stub
}
@ Override
Public void mouseExited (MouseEvent arg0 ){
// TODO Auto-generated method stub
}
@ Override
Public void mousePressed (MouseEvent arg0 ){
// TODO Auto-generated method stub
}
@ Override
Public void mouseReleased (MouseEvent arg0 ){
// TODO Auto-generated method stub
}
}