Application _java of System tray based on Java development

Source: Internet
Author: User
Tags stub

Project structure:

Operation Effect:

========================================================================

Here is the Code section:

========================================================================

/tray/src/com/b510/tray Pallet/desktopcapture.java

Copy Code code as follows:

Package Com.b510.tray Pallet;

Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;

Import Javax.swing.JFrame;
Import Javax.swing.JOptionPane;
Import Javax.swing.UIManager;

public class Desktopcapture extends JFrame implements ActionListener {

/**
* 1. Change the double-click Tray screenshot to click on the Tray Screenshot 2. No response bug 3 after fixing the tray. Move Magnifier to Upper-left and upper-right corner
*/
Private static final long serialversionuid = 1L;
JButton confirm;
BufferedImage desktopimg;
Mytray tray;
Boolean iconed = false;

Public Desktopcapture () {
Super ("Easycapture");
Init ();
When the "-" minimize button is clicked, the system is minimized to the tray
Addwindowlistener (New Windowadapter () {
public void windowiconified (WindowEvent e) {
Iconed = true;
SetVisible (FALSE);
}

When you click on "X" to close the window button, ask the user if they want to minimize the tray
Yes, means to minimize to the pallet, no, to exit
public void windowclosing (WindowEvent e) {
int option = Joptionpane.showconfirmdialog (Desktopcapture.this,
"Is it minimized to the pallet?", "Hint:", joptionpane.yes_no_option);
if (option = = joptionpane.yes_option) {
Iconed = true;
SetVisible (FALSE);
} else {
System.exit (0);
}
}
});
Pack ();
SetSize (350, 230);
setlocation (500, 300);
Setdefaultcloseoperation (Jframe.dispose_on_close);
Setresizable (FALSE);
SetVisible (TRUE);
}

void Init () {

Tray = new Mytray (desktopcapture.this);
}

Screenshots
public void Capture () {

}

public static void Main (string[] args) {
TODO auto-generated Method Stub
try {
Uimanager.setlookandfeel (Uimanager.getsystemlookandfeelclassname ());
Desktopcapture desk = new Desktopcapture ();
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

@Override
public void actionperformed (ActionEvent e) {

}

}

/tray/src/com/b510/tray Pallet/mytray.java
Copy Code code as follows:

Package Com.b510.tray Pallet;

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 Desktopcapture frame; Pallet owning main form
Private PopupMenu pop = new PopupMenu (); pop-up menu
Private MenuItem capture = new MenuItem ("capture");
Private MenuItem show = new MenuItem ("open");
Private MenuItem exit = new MenuItem ("Exit");

Public Mytray (Desktopcapture 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 Direct screenshot-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 actionperformed (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

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.