2012-05-19 11 views
4

我想在Mac上製作一個Java應用程序,在狀態欄中放置一個圖標,但我不想Mac平臺上的jar圖標(有一個咖啡杯在紙上)。所以我嘗試使用System.setProperty(java.awt.headless,true)技術,但是我不能在SystemTray菜單欄中放入任何東西,因爲我得到了一個HeadlessException。如果有人知道解決這個問題的方法,將不勝感激。Mac狀態欄項目,但不在碼頭上

+0

理想情況下,你會希望你的應用程序是一個「UI元素」 。對於Cocoa應用程序,這意味着將Info.plist中的'LSUIElement'鍵設置爲1.不知道Java應用程序。 –

+0

「狀態欄」是指菜單欄?您顯然可以更改停靠欄圖標,但我不知道有什麼方法以編程方式刪除停靠欄圖標。這需要原生的Cocoa代碼和管理權限。 – Gray

+1

由於「headlessexception」,我想知道這是否因爲Apple安全問題(即由感染網站引發的無聲和惡意Java應用程序的潛力)而無法解決***。 –

回答

4

很容易......如果你知道如何:)

首套你的jar文件中的Mac應用程序包

然後進入你的生成包的內容,並打開info.plist中。 那裏只需添加LSUIElement屬性並將其設置爲1.這會在啓動時從應用程序代碼中移除應用程序。 也看到了蘋果的文檔在這裏:http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html


的完整性:這裏還有另一種方式來做到這一點,但它是更痛苦的。 有一個cocoa命令,允許您動態顯示/隱藏停靠欄圖標: SetSystemUIMode(https://developer.apple.com/library/mac/#documentation/Carbon/reference/Dock_Manager/Reference/reference.html) 您可以嘗試使用rococoa調用此命令或編寫自己的jni庫。 或者我會有一個xcode項目,做一些非常相似的事情 - 隱藏菜單欄 - 在我的github帳戶中:https://github.com/kritzikratzi/jAppleMenuBar/ 您只需要更改src/native/jAppleMenuBar.m文件中的某些參數。

+0

非常感謝!這是一個很大的幫助。 –

+0

非常酷,但現在作爲一個意想不到的副作用,當我嘗試啓動應用程序時,它已經運行,它不會啓動。當有人試圖再次啓動它時,有沒有辦法在最初運行的應用程序中知道?如果在應用程序已經運行時雙擊它,我想打開UI。 – phreakhead

+0

不太優雅,但我認爲你可以做到這一點: – kritzikratzi

0

這避免了在碼頭上任何東西:

System.setProperty("apple.awt.UIElement", "true"); 

,這增加了托盤圖標,如圖https://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html

//Check the SystemTray is supported 
     if (!SystemTray.isSupported()) { 
      System.out.println("SystemTray is not supported"); 
      return; 
     } 
     final PopupMenu popup = new PopupMenu(); 
     final TrayIcon trayIcon = 
       new TrayIcon(createImage("images/bulb.gif", "tray icon")); 
     final SystemTray tray = SystemTray.getSystemTray(); 

     // Create a pop-up menu components 
     MenuItem aboutItem = new MenuItem("About"); 
     CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size"); 
     CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip"); 
     Menu displayMenu = new Menu("Display"); 
     MenuItem errorItem = new MenuItem("Error"); 
     MenuItem warningItem = new MenuItem("Warning"); 
     MenuItem infoItem = new MenuItem("Info"); 
     MenuItem noneItem = new MenuItem("None"); 
     MenuItem exitItem = new MenuItem("Exit"); 

     //Add components to pop-up menu 
     popup.add(aboutItem); 
     popup.addSeparator(); 
     popup.add(cb1); 
     popup.add(cb2); 
     popup.addSeparator(); 
     popup.add(displayMenu); 
     displayMenu.add(errorItem); 
     displayMenu.add(warningItem); 
     displayMenu.add(infoItem); 
     displayMenu.add(noneItem); 
     popup.add(exitItem); 

     trayIcon.setPopupMenu(popup); 

     try { 
      tray.add(trayIcon); 
     } catch (AWTException e) { 
      System.out.println("TrayIcon could not be added."); 
     }