Java TrayIcon.displayMessage() 和换行符

发布于 2024-10-21 12:59:03 字数 550 浏览 7 评论 0原文

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

在下面的示例中,trayIcon 变量的类型为“java.awt.TrayIcon”。

<代码> 托盘Icon.displayMessage("标题", "

Blah

\r\n Blah
blah ...blah", TrayIcon.MessageType.INFO);

Java 忽略 \r\n,但显示 html 标签。

有什么想法吗?

如果没有,我会使用 JFrame 之类的。

更新:这似乎是一个特定于平台的问题,我应该在问题中指定我的操作系统:我需要它在 Windows 和 Linux 上工作。

Nishan 展示了 \n 在 Windows 上运行,我用我旁边的 Vista 盒子进行了确认。 看起来我需要用 JFrame 或消息框定制一些东西,

干杯

I'm writing a Java app with a SystemTray icon, and I'd like to put line-breaks into the TrayIcon's "display message", but the normal html trick doesn't seem to work (like it does inside JLabels).

In the example below, the trayIcon variable below is of type "java.awt.TrayIcon".


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

Java ignores the \r\n, but displays the html tags.

Any ideas?

If not, I'll use a JFrame or something.

UPDATE: it seems to be a platform-specific issue, and I should have specified my OS in the question: I need this to work on Windows and Linux.

Nishan showed that a \n works on Windows, and I confirmed with a Vista box I now have next to me.
It looks like I'll need to make something custom with a JFrame or a messagebox

Cheers guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两人的回忆 2024-10-28 12:59:03

附加 \n 对我有用:

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

Appending \n worked for me :

"<HtMl><p>Blah</p> \n Blah <br> blah ... blah"
り繁华旳梦境 2024-10-28 12:59:03

正如此处所述,无法在托盘图标消息中显示新行在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);

As mentioned here, it is not possible to show new lines in tray icon messages in Linux.

Just now got a tricky idea which worked for me. Observed number of characters getting displayed per line in the message. For me it is showing 56 characters per line.

So, if a line has got less than 56 characters, fill the gap with spaces to make it 56 characters.

Knew this is not a proper way but could not find other alternatives. Now my output is as expected.

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 += " ";
            }
        }
    }

So, i tried another approach

/*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;
    }
}

And finally

trayIcon.displayMessage("My Title", this.messageToShow, TrayIcon.MessageType.INFO);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文