2011-03-11 70 views
2

我正在寫一個帶有SystemTray圖標的Java應用程序,並且我想將換行符放到TrayIcon的「顯示消息」中,但正常的html技巧並沒有實現,似乎工作(就像它在JLabels裏面)。Java TrayIcon.displayMessage()和換行符

在下面的示例中,以下trayIcon變量的類型爲「java.awt.TrayIcon」。

trayIcon.displayMessage("Title", "<p>Blah</p> \r\n Blah <br> blah ... blah", TrayIcon.MessageType.INFO);

的Java忽略\ r \ n,但顯示的HTML標記。

任何想法?

如果沒有,我會使用JFrame或其他東西。

更新:它似乎是一個平臺特定的問題,我應該在問題中指定我的操作系統:我需要這個在Windows和Linux上工作。

尼山顯示,\ n在Windows上工作,我確認了一個Vista框,我現在已經在我旁邊。 它看起來像我需要做一些定製與一個JFrame或一個消息

乾杯傢伙

+0

試試這個:' 「

胡說

\ r \ n布拉赫
等等...等等」' – 2011-03-11 05:01:06

+0

感謝哈利,但唉。不起作用 – laher 2011-03-11 09:47:43

+0

此問題尚未解決。訪問此鏈接 - http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6440297 – 2013-04-29 19:56:09

回答

2

追加\ n爲我工作:

"<HtMl><p>Blah</p> \n Blah <br> blah ... blah" 
+0

不適用於我(Ubuntu上的Oracle JDK 6u24)。只需將標籤顯示爲純文本。 – 2011-03-11 07:06:57

+0

我也在使用Ubuntu。尼山,你在運行什麼操作系統? – laher 2011-03-11 09:57:40

+0

我在一個窗口框中,jdk 6u06 – Nishan 2011-03-11 10:10:15

2

如前所述here,它是不能在Linux中的托盤圖標消息中顯示新行。

剛剛得到了一個棘手的想法,爲我工作。觀察到消息中每行顯示的字符數。對我來說,每行顯示56個字符。

所以,如果一行少於56個字符,請用空格填充空格以使其長度爲56個字符。

知道這不是一個正確的方法,但找不到其他的選擇。現在我的輸出如預期的那樣。

private java.lang.String messageToShow = null; 
private int lineLength = 56; 
/*This approach is showing ellipses (...) at the end of the last line*/ 
public void addMessageToShow(java.lang.String messageToShow) { 
     if (this.messageToShow == null){ 
      this.messageToShow = messageToShow; 
     }else{ 
      this.messageToShow += "\n" + messageToShow;//Working perfectly in windows. 
     } 
     if (System.getProperty("os.name").contains("Linux")){ 
      /* 
      Fill with blank spaces to get the next message in next line. 
      Checking with mod operator to handle the line which wraps 
      into multiple lines 
      */ 
      while (this.messageToShow.length() % lineLength > 0){ 
       this.messageToShow += " "; 
      } 
     } 
    } 

所以,我嘗試另一種方法

/*This approach is not showing ellipses (...) at the end of the last line*/ 
public void addMessageToShow(java.lang.String messageToShow) { 
    if (this.messageToShow == null){ 
     this.messageToShow = messageToShow; 
    }else{ 
     if (System.getProperty("os.name").contains("Linux")){ 
      /* 
      Fill with blank spaces to get the next message in next line. 
      Checking with mod operator to handle the line which wraps 
      into multiple lines 
      */ 
      while (this.messageToShow.length() % lineLength > 0){ 
       this.messageToShow += " "; 
      } 
     }else{ 
      this.messageToShow += "\n";// Works properly with windows 
     } 
     this.messageToShow += messageToShow; 
    } 
} 

最後

trayIcon.displayMessage("My Title", this.messageToShow, TrayIcon.MessageType.INFO); 
+0

Thx的解決方法,我現在也使用它。你確定了56個字符的行長?嘗試和錯誤?無論如何,我會建議不要硬編碼數字56或至少給它一個名字在一個專門的變量與評論和原因,爲什麼你這樣做,以避免幻數。 – user573215 2013-09-05 12:36:19

+0

我做了跟蹤和錯誤的方法,只是爲了得到這個數字56.更新我的代碼在您的建議,以避免「幻數」的使用。 – 2013-09-05 13:27:39