我在擴展JEditorPane的ListCellRenderer中有此代碼。編輯器窗格不顯示圖像,而是顯示「破碎的圖標」。它出什麼問題了?用JEditorPane/ListCellRenderer破壞img標記
public class TweetCellRenderer extends JEditorPane implements ListCellRenderer {
public Component getListCellRendererComponent(
javax.swing.JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus
) {
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 6));
StringBuffer sb = new StringBuffer();
setContentType("text/html");
sb.append("<html><body>");
sb.append("<img src='http://www.google.co.uk/images/firefox/video.png' />");
sb.append("</body></html>");
System.out.println(sb);
setText(sb.toString());
setBackground(isSelected ? SELECTED_BG : BG);
setForeground(isSelected ? SELECTED_FG : FG);
return this;
}
}
我試過使用ImageIcon。當我的CellRenderer是帶有附加圖標和文本的JLabel時,它正在工作。但我需要在文本中有超鏈接,所以我需要使用JEditorPane。 我試過擴展JPanel並在JPanel中添加一個ImageIcon(內部JLabel)和JEditorPane,但這也不起作用 - 圖片/文本由於某種原因被重複多次。 我希望JList中的每個項目在左側和右側顯示一個48x48圖片,以便使用超鏈接格式化文本。我怎樣才能做到這一點? 謝謝 –