繁体   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