java實現(xiàn)系統(tǒng)托盤示例
桌面的系統(tǒng)托盤即當(dāng)程序最小化或者關(guān)閉按鈕程序并沒有退出,而是最小化在任務(wù)狀態(tài)區(qū)域(Windows系統(tǒng)),當(dāng)鼠標(biāo)點(diǎn)擊那個區(qū)域所在的圖標(biāo)有提示以及其他的操作。在 Microsoft Windows 上,它被稱為“任務(wù)欄狀態(tài)區(qū)域 (Taskbar Status Area)”,在 Gnome 上,它被稱為“通知區(qū)域 (Notification Area)”,在 KDE 上,它被成為“系統(tǒng)托盤 (System Tray)”。系統(tǒng)托盤由運(yùn)行在桌面上的所有應(yīng)用程序共享。
jdk1.6 中新增了兩個類來實現(xiàn):
SystemTray和 TrayIcon,以下為詳細(xì)介紹:
SystemTray
類介紹:
在某些平臺上,可能不存在或不支持系統(tǒng)托盤,所以要首先使用SystemTray.isSupported()來檢查當(dāng)前的系統(tǒng)是否支持系統(tǒng)托盤
SystemTray可以包含一個或多個 TrayIcon,可以使用 add(java.awt.TrayIcon)方法將它們添加到托盤,當(dāng)不再需要托盤時,使用 remove(java.awt.TrayIcon)
移除它。
TrayIcon由圖像、彈出菜單和一組相關(guān)偵聽器組成。每個 Java 應(yīng)用程序都有一個 SystemTray實例,在應(yīng)用程序運(yùn)行時,它允許應(yīng)用程序與桌面系統(tǒng)托盤建立連接。
SystemTray實例可以通過getSystemTray ()方法獲得。應(yīng)用程序不能創(chuàng)建自己的 SystemTray
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 java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
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.Timer;
import javax.swing.UIManager;
/**
*
* @author Mr.LiuTao
*/
public class TrayByJdk extends JFrame implements ActionListener{
private JPanel pane = null;
private JButton button = null; // 啟動托盤圖標(biāo)的按鈕
private JLabel label = null; // 用來顯示系統(tǒng)是否支持托盤的信息
private TrayIcon trayIcon = null; // 托盤圖標(biāo)
private Timer shanshuo = null;
private ImageIcon icon1 = null;
private ImageIcon icon2 = null;
private SystemTray tray = null; // 本操作系統(tǒng)托盤的實例
boolean gengai = false;
//采用jdk1.6的托盤技術(shù) 實現(xiàn)跨平臺的應(yīng)用
public TrayByJdk() {
//super("托盤技術(shù)演示");
icon1 = new ImageIcon("G:\\javaworks\\eclipsework\\山寨QQClient\\images\\16.gif"); // 將要顯示到托盤中的圖標(biāo)
icon2 = new ImageIcon("G:\\javaworks\\eclipsework\\山寨QQClient\\images\\15.gif"); // 將要顯示到托盤中的圖標(biāo)
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()) {
this.tray();
}
shanshuo = new Timer(1000,this);
shanshuo.start();
this.getContentPane().add(pane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.addWindowStateListener(new WindowStateListener () {
public void windowStateChanged(WindowEvent state) {
if(state.getNewState() == 1 || state.getNewState() == 7) {
yinca();
}
}
});
this.setVisible(false);
System.out.println("=============="+this.isVisible());
}
/**
* 托盤相關(guān)代碼
*/
private void tray() {
label.setText("本操作系統(tǒng)支持托盤");
button.setEnabled(true);
tray = SystemTray.getSystemTray(); // 獲得本操作系統(tǒng)托盤的實例
//ImageIcon icon = new ImageIcon("tray.gif"); // 將要顯示到托盤中的圖標(biāo)
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) 用“圖標(biāo)”來構(gòu)造
* TrayIcon(Image image, String tooltip) 用“圖標(biāo)”和“ToolTip”構(gòu)造
* TrayIcon(Image image, String tooltip, PopupMenu popup) 用“圖標(biāo)”,“ToolTip”,“彈出菜單”來構(gòu)造一個托盤圖標(biāo)
*/
trayIcon = new TrayIcon(icon1.getImage(), "托盤技術(shù)演示", pop);
// 點(diǎn)擊本按鈕后窗口被關(guān)閉,托盤圖標(biāo)被添加到系統(tǒng)的托盤中
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
tray.add(trayIcon); // 將托盤圖標(biāo)添加到系統(tǒng)的托盤實例中
setVisible(false); // 使窗口不可視
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
/**
* 添加鼠標(biāo)監(jiān)聽器,當(dāng)鼠標(biāo)在托盤圖標(biāo)上雙擊時,默認(rèn)顯示窗口
*/
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { // 鼠標(biāo)雙擊
tray.remove(trayIcon); // 從系統(tǒng)的托盤實例中移除托盤圖標(biāo)
setVisible(true); // 顯示窗口
}
}
});
show.addActionListener(new ActionListener() { // 點(diǎn)擊“顯示窗口”菜單后將窗口顯示出來
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon); // 從系統(tǒng)的托盤實例中移除托盤圖標(biāo)
setVisible(true); // 顯示窗口
}
});
exit.addActionListener(new ActionListener() { // 點(diǎn)擊“退出演示”菜單后推出程序
public void actionPerformed(ActionEvent e) {
System.exit(0); // 退出程序
}
});
author.addActionListener(new ActionListener() { // 點(diǎn)擊“退出演示”菜單后推出程序
public void actionPerformed(ActionEvent e) {
showMessage();
}
});
pop.add(show);
pop.add(exit);
pop.add(author);
}
/**
* 顯示信息
*/
private void showMessage() {
JOptionPane.showMessageDialog(this, new JLabel("這是一個系統(tǒng)托盤"), "消息", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
new TrayByJdk().yinca();
}
public void yinca(){
try {
tray.add(trayIcon);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 將托盤圖標(biāo)添加到系統(tǒng)的托盤實例中
setVisible(false); // 使窗口不可視
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(!gengai){
trayIcon.setImage(icon2.getImage());
gengai = true;
}else{
trayIcon.setImage(icon1.getImage());
gengai = false;
}
}
}
相關(guān)文章
Java分布式鎖理論(redis、zookeeper))案例詳解
zookeeper有個節(jié)點(diǎn)路徑的概念,節(jié)點(diǎn)路徑不能重復(fù),保證了唯一性,這篇文章給大家介紹Java分布式鎖理論(redis、zookeeper)?案例詳解,感興趣的朋友跟隨小編一起看看吧2024-01-01使用redisTemplate的scan方式刪除批量key問題
這篇文章主要介紹了使用redisTemplate的scan方式刪除批量key問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12IDEA2022搭建Spring?Cloud多模塊項目的詳細(xì)過程
這篇文章主要介紹了IDEA2022搭建Spring?Cloud多模塊項目,網(wǎng)上有很多教程父模塊都是通過maven的方式創(chuàng)建的,然后子模塊是通過Spring?Initalizr方式創(chuàng)建,這種方式父模塊無法管理子模塊的依賴仲裁,需要每個子模塊自行管理,就失去了父模塊的用處了2022-10-10elasticsearch索引創(chuàng)建create?index集群matedata更新
這篇文章主要介紹了elasticsearch索引創(chuàng)建create?index及集群matedata更新,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04SpringBoot 動態(tài)配置Profile環(huán)境的方式
這篇文章主要介紹了SpringBoot 動態(tài)配置Profile環(huán)境的方式,本文通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10Spring Boot集成Mybatis的實例代碼(簡潔版)
這篇文章主要介紹了Spring Boot集成Mybatis簡潔版的教程,需要的朋友可以參考下2018-02-02