基於java開發之系統托盤的應用

來源:互聯網
上載者:User

項目結構:

運行效果:

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

下面是代碼部分:

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

/tray/src/com/b510/tray托盤/DesktopCapture.java複製代碼 代碼如下:package com.b510.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 {

/**
* 1.改變雙擊托盤為單擊托盤 2.修複了托盤後無反應BUG 3.將放大鏡提示框移至左上方和右上方
*/
private static final long serialVersionUID = 1L;
// JButton confirm;
// BufferedImage desktopImg;
MyTray tray;
boolean iconed = false;

public DesktopCapture() {
super("EasyCapture");
init();
// 當點擊"-"最小化按鈕時,系統會最小化到托盤
addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
iconed = true;
setVisible(false);
}

// 當點擊"X"關閉視窗按鈕時,會詢問使用者是否要最小化到托盤
// 是,表示最小化到托盤,否,表示退出
public void windowClosing(WindowEvent e) {
int option = JOptionPane.showConfirmDialog(DesktopCapture.this,
"是否最小化到托盤?", "提示:", 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);
}

//
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托盤/MyTray.java
複製代碼 代碼如下:package com.b510.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;// 表徵圖
private TrayIcon trayIcon;
private SystemTray systemTray;// 系統托盤

private DesktopCapture frame; // 托盤所屬主表單
private PopupMenu pop = new PopupMenu(); // 快顯功能表
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, "單擊直接-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

}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.