Java TrayIcon.displayMessage()和换行符
我正在编写一个带有SystemTray图标的Java应用程序,我想在TrayIcon的“显示消息”中添加换行符,但是普通的html技巧似乎不起作用(就像在JLabels中一样) 在下面的示例中,下面的trayIcon变量的类型为“java.awt.trayIcon”Java TrayIcon.displayMessage()和换行符,java,trayicon,line-breaks,Java,Trayicon,Line Breaks,我正在编写一个带有SystemTray图标的Java应用程序,我想在TrayIcon的“显示消息”中添加换行符,但是普通的html技巧似乎不起作用(就像在JLabels中一样) 在下面的示例中,下面的trayIcon变量的类型为“java.awt.trayIcon” trayIcon.displayMessage(“Title”,“Blah\r\n BlahBlah…Blah”,trayIcon.MessageType.INFO); Java忽略html标记,但显示html标记 有什么想法吗
trayIcon.displayMessage(“Title”,“Blah\r\n Blah
Blah…Blah”,trayIcon.MessageType.INFO);
Java忽略html标记,但显示html标记
有什么想法吗
如果没有,我会用JFrame之类的
更新:这似乎是一个特定于平台的问题,我应该在问题中指定我的操作系统:我需要它才能在Windows和Linux上工作
Nishan展示了a\n在Windows上工作,我用我旁边的Vista框确认了这一点。
看起来我需要用JFrame或messagebox定制一些东西
干杯伙计们追加\n为我工作:
"<HtMl><p>Blah</p> \n Blah <br> blah ... blah"
“废话\n废话
废话……废话”
如前所述,在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);
试试这个:
“诸如此类\r\n诸如此类
诸如此类……诸如此类”
谢谢Harry,但遗憾的是,不行。这个问题还没有解决。访问此链接-不适用于我(Ubuntu上的Oracle JDK 6u24)。简单地将标签显示为纯文本。我也在使用Ubuntu。Nishan,你在运行什么操作系统?我在一个windows机箱上,jdk 6u06I可以确认它在windows Vista上工作+1对于Windows,呃:(接受这个答案是因为我相信它不在Java的控制范围内。它是OS-specific.Thx解决方案。我现在也使用它。您是如何确定56个字符的行长度的?尝试并出错?无论如何,我建议不要硬编码数字56,或者至少在一个专用变量中给它一个带有注释的名称,以及您这样做的原因。),以避免使用幻数。我使用trail-and-error方法仅推导出该数字56。根据您的建议更新了我的代码,以避免使用“幻数”。