JTextPane 网上说 它内置了 不能显示水平滚动条 没办法 只好看它的源码:
JTextPane 中 没找到相似的 JEditorPane 都是有相似的.
解决的办法如下:
把父类中的
public boolean getScrollableTracksViewportWidth()
{
if(getParent() instanceof JViewport)
{
JViewport jviewport = (JViewport)getParent();
TextUI textui = getUI();
int i = jviewport.getWidth();
Dimension dimension = textui.getMinimumSize(this);
Dimension dimension1 = textui.getMaximumSize(this);
if(i >= dimension.width && i <= dimension1.width)
return true;
}
return false;
}
为
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
就可以了.
另外有一种方法:
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
JScrollPane scrollPane = new JScrollPane(pane);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);