2

我正在编写一个带有 SystemTray 图标的 Java 应用程序,我想将换行符放入 TrayIcon 的“显示消息”中,但普通的 html 技巧似乎不起作用(就像它在 JLabels 中一样)。

在下面的示例中,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 或消息框进行自定义

干杯伙计们

4

2 回答 2

2

附加 \n 对我有用:

"<HtMl><p>Blah</p> \n Blah <br> blah ... blah"
于 2011-03-11T06:57:24.063 回答
2

如此处所述在 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);
于 2013-04-29T20:50:13.870 回答