如此处所述,在 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);