java Swing 实现将系统最小化到系统托盘
2013年09月22日
⁄ 综合
⁄ 共 6679字 ⁄ 字号
小 中 大
- 源码:
-
- package
com.topking.tray.ui;
-
- 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
java.awt.event.WindowEvent;
- import
java.awt.event.WindowListener;
-
- import
javax.swing.ImageIcon;
-
- public
class
MainFrame
extends
javax.swing.JFrame
implements
ActionListener, WindowListener{
-
-
- private
javax.swing.JLabel L_img;
- private
javax.swing.JLabel L_img2;
- private
PopupMenu pop;
- private
MenuItem open,close;
- private
TrayIcon trayicon;
-
-
-
-
- public
MainFrame() {
- this
.setTitle(
"Java实现系统托盘示例"
);
- this
.setLocation(
300
,
300
);
- initComponents();
- }
-
-
- @SuppressWarnings
(
"unchecked"
)
-
- private
void
initComponents() {
-
- 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.addActionListener(this
);
-
- close = new
MenuItem(
"关闭"
);
- 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,
"系统托盘示例(java)"
,pop);
- trayicon.addMouseListener(new
MouseListener(){
-
- public
void
mouseClicked(MouseEvent e) {
-
- if
(e.getClickCount()==
2
){
- openFrame();
- }
- }
-
- public
void
mouseEntered(MouseEvent e) {
-
-
- }
-
- public
void
mouseExited(MouseEvent e) {
-
-
- }
-
- public
void
mousePressed(MouseEvent e) {
-
-
- }
-
- public
void
mouseReleased(MouseEvent e) {
-
-
- }
-
- });
-
- try
{
- tray.add(trayicon);
- } catch
(AWTException e) {
-
- 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()
- .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
,
29
,
29
)
- .addComponent(L_img2, javax.swing.GroupLayout.PREFERRED_SIZE, 222
, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(39
, Short.MAX_VALUE))
- );
-
- pack();
- }
-
-
- public
static
void
main(String args[]) {
- java.awt.EventQueue.invokeLater(new
Runnable() {
- public
void
run() {
- new
MainFrame().setVisible(
true
);
- }
- });
- }
-
- public
void
actionPerformed(ActionEvent e) {
-
- if
(e.getSource()==open){
- openFrame();
- }
- if
(e.getSource()==close){
- System.exit(-1
);
- }
- }
-
- public
void
openFrame(){
- this
.setVisible(
true
);
- this
.setAlwaysOnTop(
true
);
- }
- public
void
windowActivated(WindowEvent arg0) {
-
-
- }
-
- public
void
windowClosed(WindowEvent arg0) {
-
- this
.setVisible(
false
);
- this
.dispose();
- }
-
- public
void
windowClosing(WindowEvent arg0) {
-
-
- }
-
- public
void
windowDeactivated(WindowEvent arg0) {
-
-
- }
-
- public
void
windowDeiconified(WindowEvent arg0) {
-
-
- }
-
-
- public
void
windowIconified(WindowEvent arg0) {
-
- this
.dispose();
- }
-
- public
void
windowOpened(WindowEvent arg0) {
-
-
- }
-
-
- }