簡體   English   中英

Java TrayIcon.displayMessage()和換行符

[英]Java TrayIcon.displayMessage() and line-breaks

我正在編寫帶有SystemTray圖標的Java應用程序,並且我想在TrayIcon的“顯示消息”中添加換行符,但是普通的html技巧似乎不起作用(就像在JLabel中一樣)。

在下面的示例中,下面的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上需要使用此操作系統。

Nishan展示了\\ n在Windows上可以使用,並且我旁邊有一個Vista盒確認。 看來我需要使用JFrame或消息框進行自定義

歡呼的家伙

如前所述在這里 ,它是不可能展現在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);

附加\\ n對我有用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM