我重写了ListCellRenderer作为单元渲染器,然后继承JTextpane为了实现字体的不同属性显示,按理说JTextpane应该自动换行的啊~但我这个程序最后运行却没自动换行,请大牛们帮忙解答下~~感激不尽~~~~~~~附上源代码:
//CList的单元渲染器
class OutPutCellRenderer extends JTextPane implements ListCellRenderer {
    // This is the only method defined by ListCellRenderer.
    // We just reconfigure the JLabel each time we're called.
    public Component getListCellRendererComponent(
      JList list,              // the list
      Object value,            // value to display
      int index,               // cell index
      boolean isSelected,      // is the cell selected
      boolean cellHasFocus)    // does the cell have focus
    {
     //setLineWrap(true); //自动换行
     //setWrapStyleWord(true); //保持换行不会影响单词的完整性(被中间切开的情况)
    
     // 加个边框
     setBorder(BorderFactory.createLineBorder(Color.lightGray));
        String[] s = value.toString().split(";");
        int s_flag = s.length; //记录explanation和example的分界线(第一个example对应数组中的下标位置)
        for(int i = 0; i < s.length; ++i) {
         if(s[i].startsWith(" \"")) {
         s_flag = i;
         break;
         }
        }
        //对字符串格式和属性的处理
        String tmp = null;
        Document docs = null;
        SimpleAttributeSet sa = null;
        {
         sa = new SimpleAttributeSet(); //设置字体属性的类
         docs = this.getDocument(); //利用getDocument()方法取得JTextPane的Document instance
         try {
         //清空docs原来的内容
docs.remove(0, docs.getLength());
} catch (BadLocationException e1) {e1.printStackTrace();}

//序列号处理
        index += 1;
         StyleConstants.setFontSize(sa, 11);
         StyleConstants.setBold(sa, true);
         StyleConstants.setForeground(sa, Color.magenta);
         try {
docs.insertString(docs.getLength(), ""+index+".\r\n", sa);
} catch (BadLocationException e) {e.printStackTrace();}
         //"explanation"字符串的处理
         StyleConstants.setFontSize(sa, 14);
         StyleConstants.setBold(sa, true);
         StyleConstants.setUnderline(sa, true);
         StyleConstants.setLineSpacing(sa, 8);
         StyleConstants.setForeground(sa, Color.blue);
         tmp = "Explanation:\n";
         try {
docs.insertString(docs.getLength(), tmp, sa);
} catch (BadLocationException e) {e.printStackTrace();}
//explanation块的处理
StyleConstants.setFontSize(sa, 12);
StyleConstants.setUnderline(sa, false);
StyleConstants.setLeftIndent(sa, 15); //不起作用?
StyleConstants.setForeground(sa, Color.darkGray);
for(int i = 0; i < s_flag; ++i) {
try {
docs.insertString(docs.getLength(), (i+1)+"."+s[i]+"\r\n", sa);
} catch (BadLocationException e) {e.printStackTrace();}
}
if(s_flag == s.length) {} //如果没有example,什么都不做
else {
//"example"字符串的处理
         StyleConstants.setFontSize(sa, 14);
         StyleConstants.setUnderline(sa, true);
         StyleConstants.setForeground(sa, Color.blue);
         StyleConstants.setLeftIndent(sa, 0);
         tmp = "Example:\n";
         try {
docs.insertString(docs.getLength(), tmp, sa);
} catch (BadLocationException e) {e.printStackTrace();}
//example块的处理
StyleConstants.setFontSize(sa, 12);
StyleConstants.setUnderline(sa, false);
StyleConstants.setLeftIndent(sa, 15);
StyleConstants.setForeground(sa, Color.darkGray);
for(int i = s_flag; i < s.length; ++i) {
try {
docs.insertString(docs.getLength(), (i-s_flag+1)+"."+s[i]+"\r\n", sa);
} catch (BadLocationException e) {e.printStackTrace();}
}

}

        }         setDocument(docs);
        
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        setOpaque(true);
        return this;
    }
}