Application of system tray based on Java development
- 2020-04-01 01:49:08
- OfStack
Project structure:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013050216451235.png "width = 304 height = 138 >
Operation effect:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013050216451236.png "width = 452 height = 264 > < img Alt =" "border = 0 SRC = "/ / files.jb51.net/file_images/article/201305/2013050216451237.png" width = 355 height = 231 >
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Here is the code section:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
/ tray/SRC/com/b510 / tray tray/DesktopCapture. Java
package com.b510.tray The tray ;
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 {
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 minimizes to the tray
addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
iconed = true;
setVisible(false);
}
//When the "X" button is clicked to close the window, the user is asked if they want to minimize to the tray
//Yes, means minimize to pallet, no, means exit
public void windowClosing(WindowEvent e) {
int option = JOptionPane.showConfirmDialog(DesktopCapture.this,
" Whether to minimize to pallet ?", " 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);
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 tray/MyTray. Java
package com.b510.tray The 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;//The system tray
private DesktopCapture frame; //The tray belongs to the main form
private PopupMenu pop = new PopupMenu(); //The popup 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
}
}