【发布时间】:2013-01-18 02:49:44
【问题描述】:
这是我用来添加类型(一般)、(耳语)、(公会)或(全局)消息的“协议语法”
// add the message in list box
ChatJInternalFrame.modelChatList.addElement("(general)" + characterName + ": " + chatMessage);
这里是我设置列表模型和单元格渲染器的地方:
modelChatList = new DefaultListModel<String>();
listForChat = new JList<String>(modelChatList);
listForChat.setFont(new Font("Lucida Console", Font.PLAIN, 14));
listForChat.setCellRenderer(new ColoredChatListRenderer());
这是我的自定义 cellRenderer:
public class ColoredChatListRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
String s = String.valueOf(value);
String splitPipe[] = s.split("\\)");
if(s.length() > 7 && s.substring(0, 8).equals("~SERVER~")){
setForeground(Color.red);
} else if (splitPipe[1].length() > 11 && (splitPipe[1].substring(0,12).equals("DEV Proskier") || splitPipe[1].substring(0,11).equals("DEV Sparkle"))){
setForeground(Color.orange);
} else {
if (splitPipe[0].equals("(general")){
setForeground(Color.black);
} else if (splitPipe[0].equals("(whisper")){
setForeground(Color.magenta);
} else if (splitPipe[0].equals("(guild")){
setForeground(Color.blue);
} else if (splitPipe[0].equals("(global")){
setForeground(Color.pink);
}
}
return(this);
}
}
现在这很好用,但是我想我不希望聊天中出现类型(一般)、(耳语)等,只是颜色变化。抱歉,如果这是一个非常简单的问题,我的大脑会因为在聊天窗口上工作而受到我用来切换聊天模式的焦点遍历废话。
有什么简单的方法吗??? 就像只是切掉前几个字符的子字符串一样,我可以使模式长度相同......又名(GEN),(GLO),(GUI),(WHI)
****编辑****
感谢您的帮助,但这对我来说是最简单的解决方案。如果这在某些方面不好,请告诉我。
if (splitPipe[0].equals("(general")){
setText(splitPipe[1]);
setForeground(Color.black);
} else if (splitPipe[0].equals("(whisper")){
setText(splitPipe[1]);
setForeground(Color.magenta);
} else if (splitPipe[0].equals("(guild")){
setText(splitPipe[1]);
setForeground(Color.blue);
} else if (splitPipe[0].equals("(global")){
setText(splitPipe[1]);
setForeground(Color.pink);
}
【问题讨论】:
标签: java render jlist cellrenderer