實現(xiàn)系統(tǒng)托盤圖標,借用dll動態(tài)鏈接庫,用JAVA JNI實現(xiàn)。
JDK6.0的系統(tǒng)托盤的雛形是個開源的JDIC,實現(xiàn)原理就是不同的系統(tǒng)調(diào)用不同的JNI不是像LZ那樣想的,可以在任何平臺上跑,所以也是分支持不支持的,LZ可以用JDK1.6或JDI。
view plaincopy to clipboardprint?import java.awt.AWTException;
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.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
/**
* Java1.6.0實現(xiàn)系統(tǒng)托盤技術(shù)演示
*/
public class TrayDemo extends JFrame {
private JPanel pane = null;
private JButton button = null; // 啟動托盤圖標的按鈕
private JLabel label = null; // 用來顯示系統(tǒng)是否支持托盤的信息
private TrayIcon trayIcon = null; // 托盤圖標
private SystemTray tray = null; // 本操作系統(tǒng)托盤的實例
public TrayDemo() {
super("Java1.6.0托盤技術(shù)演示");
try {
// 將LookAndFeel設(shè)置成Windows樣式
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
pane = new JPanel();
button = new JButton("縮小到托盤");
button.setEnabled(false);
label = new JLabel("本操作系統(tǒng)不支持托盤");
pane.add(label);
pane.add(button);
if(SystemTray.isSupported()){ // 如果操作系統(tǒng)支持托盤
this.tray();
}
this.getContentPane().add(pane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
}
/**
* 托盤相關(guān)代碼
*/
private void tray(){
label.setText("本操作系統(tǒng)支持托盤");
button.setEnabled(true);
tray = SystemTray.getSystemTray(); // 獲得本操作系統(tǒng)托盤的實例
ImageIcon icon = new ImageIcon("F:/NetBeansProjects/Desktop/src/38.gif"); // 將要顯示到托盤中的圖標
PopupMenu pop = new PopupMenu(); // 構(gòu)造一個右鍵彈出式菜單
MenuItem show = new MenuItem("顯示窗口");
MenuItem exit = new MenuItem("退出演示");
MenuItem author = new MenuItem("Author");
/**
* TrayIcon有三個構(gòu)造
* TrayIcon(Image image) 用“圖標”來構(gòu)造
* TrayIcon(Image image, String tooltip) 用“圖標”和“ToolTip”構(gòu)造
* TrayIcon(Image image, String tooltip, PopupMenu popup) 用“圖標”,“ToolTip”,“彈出菜單”來構(gòu)造一個托盤圖標
*/
trayIcon = new TrayIcon(icon.getImage(), "Java1.6.0托盤技術(shù)演示", pop);
// 點擊本按鈕后窗口被關(guān)閉,托盤圖標被添加到系統(tǒng)的托盤中
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
tray.add(trayIcon); // 將托盤圖標添加到系統(tǒng)的托盤實例中
setVisible(false); // 使窗口不可視
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
/**
* 添加鼠標監(jiān)聽器,當鼠標在托盤圖標上雙擊時,默認顯示窗口
*/
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){ // 鼠標雙擊
tray.remove(trayIcon); // 從系統(tǒng)的托盤實例中移除托盤圖標
setVisible(true); // 顯示窗口
}
}
});
show.addActionListener(new ActionListener() { // 點擊“顯示窗口”菜單后將窗口顯示出來
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon); // 從系統(tǒng)的托盤實例中移除托盤圖標
setVisible(true); // 顯示窗口
}
});
exit.addActionListener(new ActionListener() { // 點擊“退出演示”菜單后退出程序
public void actionPerformed(ActionEvent e) {
System.exit(0); // 退出程序
}
});
author.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showMessage();
}
});
pop.add(show);
pop.add(exit);
pop.add(author);
}
/**
* 顯示信息
*/
private void showMessage(){
JOptionPane.showMessageDialog(this, new JLabel("<html>作者:voole" +
"<br>Blog:http://voole.javaeye.com</html>"), "voole", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
new TrayDemo();
}
}