2013-12-20 38 views
1

我無法使水平滾動條出現。我試過對JTextPane使用JTextPane.setSize(),JTextPane.setPreferredSize()和無大小方法。我也使用JScrollPane.setPreferredSize()。如何使水平滾動條出現在包含JTextPane組件的JScrollPane中

垂直滾動條出現,但水平滾動條不出現。

下面是一個例子:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Test { 

    private int   textPaneWidth = 500; 
    private int   textPaneHeigth = 200; 
    private int   scrollPaneWidth = 100; 
    private int   scrollPaneHeigth = 100; 
    private JTextPane textPane; 
    private JButton  button; 
    private JFrame  frame; 
    private JScrollPane scrollPane; 

    public static void main(String[] args) { 

     Test gui = new Test(); 
     gui.go(); 

    } 

    private void go() { 

     frame  = new JFrame("Test"); 
     button  = new JButton("button"); 
     textPane = new JTextPane(); 
     scrollPane = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

     frame.setLayout(new FlowLayout()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     button.addActionListener(new ButtonListener()); 
     textPane.setFont(new Font("Courier", Font.PLAIN, 12)); 

     // Sizes: 
//  textPane.setSize(textPaneWidth, textPaneHeigth);        // ??? 
//  textPane.setPreferredSize(new Dimension(textPaneWidth, textPaneHeigth));  // ??? 
     scrollPane.setPreferredSize(new Dimension(scrollPaneWidth, scrollPaneHeigth)); 

     frame.add(button); 
     frame.add(scrollPane, BorderLayout.CENTER); 
     frame.pack(); 
     frame.setVisible(true); 

    } 

    private class ButtonListener implements ActionListener { 

     public void actionPerformed(ActionEvent event) { 
      textPane.setText("==================================================================== \n" + 
         "==================================================================== \n" + 
         "==================================================================== \n" + 
         "==================================================================== \n" + 
         "==================================================================== \n"); 
     } 

    } 
} 

當按下按鈕時,該字符串被添加到的JTextPane。有垂直滾動,但沒有水平滾動。

這就是我按下按鈕後看到:

no horizontal scrolling :(

我在做什麼錯?

+0

是否有足夠的文字底線不會出現?相反,是否出現了所有五行'= ='? – Xynariz

+0

@Xynariz添加了問題發生的圖像。 – Rob

+0

謝謝,現在就寫答案。 – Xynariz

回答

1

好像你需要延長的JTextPane避免包裝

https://forums.oracle.com/thread/1210688

class JTextWrapPane extends JTextPane { 

    boolean wrapState = true; 
    JTextArea j = new JTextArea(); 

    JTextWrapPane() { 
     super(); 
    } 

    public JTextWrapPane(StyledDocument p_oSdLog) { 
     super(p_oSdLog); 
    } 


    public boolean getScrollableTracksViewportWidth() { 
     return wrapState; 
    } 


    public void setLineWrap(boolean wrap) { 
     wrapState = wrap; 
    } 


    public boolean getLineWrap(boolean wrap) { 
     return wrapState; 
    } 
} 

編輯:

// instead of private JTextPane jtp = new JTextPane(sdLog); 
    private JTextWrapPane jtp = new JTextWrapPane(sdLog); 
//and set LineWrap = (false): 
     jtp.setLineWrap(false); 
+0

super(p_oSdLog);拋出異常:構造函數對象(StyledDocument)未定義 – Rob

+0

我可以在這裏看到它,http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html – gaurav5430

+0

是啊,我是一個白癡,忘了擴展JTextPane。我已經在使用擴展類,但它不起作用。仍然與使用JTextPane相同的結果。 – Rob

1

有點挖後,我想通了這個問題。

JTextPane不僅僅顯示文檔,而是顯示樣式化文檔。引用java tutorials:「一個JTextComponent子類,JTextPane,要求它的文檔是一個StyledDocument,而不僅僅是一個Document。」

當您使用默認構造函數創建JTextPane時,您將自動創建DefaultStyledDocument。從documentation for DefaultStyledDocument:

這些樣式運行被映射到段落元素結構(可能駐留在某個其他結構中)。由於邏輯樣式分配給段落邊界,因此樣式在段落邊界處運行中斷。

這意味着JTextPane究竟是幹什麼的自動換行你。一種解決方案是嘗試使用JTextArea代替。

編輯,每評論: 另一種解決辦法是StyledDocument(可能由AbstractDocument擴展)來創建自己的,設置它怎麼想,並把它傳遞給JTextPane構造。

+0

問題是我需要文本區域來支持樣式文本,而JTextArea只支持純文本。 – Rob

+0

JEditorPane會工作嗎? (http://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html)JEditorPane支持比JTextArea更多的樣式。 – Xynariz

+0

它可以與JEditorPane一起使用!爲什麼? JTextPane擴展JEditorPane不是嗎?有沒有辦法使它與JTextPane一起使用? – Rob

1

只有垂直滾動沒有滾動

progressTextArea = new JTextPane(); 

JScrollPane scroller = new JScrollPane() 
{ 
    public Dimension getPreferredSize() 
    { 
     return new Dimension(300, 100); 
    } 

    public float getAlignmentX() 
    { 
     return LEFT_ALIGNMENT; 
    } 
}; 

scroller.getViewport().add(progressTextArea);