研究往JTextPane中插入圖片的時候,插入了一張壁紙,爲了能更好的瀏覽,將JTextPane放到了JScrollPane中。但是不知道爲什麼水平滾動條總是不顯示。
設置JScrollPane的 setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS ); 方法,scrollbar出來了,但是沒有滾動條。測試了一下,發現是JTextPane的寬度導致。於是在網上查了讓JTextPane不自動換行的方法,除了設置成text/html,還可以重寫JTextPane的 getScrollableTracksViewportWidth() 方法:
JTextPane tp = new JTextPane(){
public boolean getScrollableTracksViewportWidth() {
return false;
}
};