Java程式添加托盤表徵圖和托盤快顯功能表

來源:互聯網
上載者:User

Java 1.6後可以添加托盤表徵圖和托盤表徵圖快顯功能表變的很方便,一般代碼如下:

if (SystemTray.isSupported()) { // 判斷是否支援系統托盤URL url = this.getClass().getResource("/images/Trayicon.gif"); // 擷取圖片所在的URLImageIcon icon = new ImageIcon(url); // 執行個體化映像對象Image image = icon.getImage(); // 獲得Image對象TrayIcon trayIcon = new TrayIcon(image); // 建立托盤表徵圖trayIcon.addMouseListener(new MouseAdapter() { // 為托盤添加滑鼠適配器public void mouseClicked(MouseEvent e) { // 滑鼠事件if (e.getClickCount() == 2) { // 判斷是否雙擊了滑鼠showFrame(); // 調用方法顯示表單}}});trayIcon.setToolTip("系統托盤"); // 添加工具提示文本PopupMenu popupMenu = new PopupMenu(); // 建立快顯功能表MenuItem exit = new MenuItem("退出"); // 建立功能表項目//回應程式法exit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub}});popupMenu.add(exit); // 為快顯功能表添加功能表項目trayIcon.setPopupMenu(popupMenu); // 為托盤表徵圖加彈出菜彈SystemTray systemTray = SystemTray.getSystemTray(); // 獲得系統托盤對象try {systemTray.add(trayIcon); // 為系統托盤加托盤表徵圖} catch (Exception e) {e.printStackTrace();}}

調用的ShowFrame()方法就是顯示主視窗。一般如下:

public void showFrame() {this.setVisible(true); // 顯示表單this.setState(Frame.NORMAL);}

注意幾點;

1、圖片不支援ico的,用jpg、gif、png,16*16大小的最合適。

2、JDK必須1.6以上。

3、這段代碼一般放在構造方法中,快顯功能表的回應程式法自己添加。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.