2013-06-13 58 views
2

我有以下問題: enter image description here的JTextPane適合墊層JPanel並JFrame的

我就充滿了內容的左側有一個JTextPaneJTextPane放入JScrollPane。此ScrollPane被添加到JPanelBorderLayout.CENTER,並且這個結果JPanel被添加到BorderLayout.CENTER下面的JFrame contentPane()

現在我想分散這JTextPane遍佈JFrame/JPanel。 我該如何做到這一點?

下面是代碼:

public class FinishPanelBuilder extends JPanel 
{ 
    /** 
    * Serial version UID. 
    */ 
    private static final long serialVersionUID = 4373044358025740572L; 
    private Model model; 

    /** 
    * Constructor to call if you want to build up a new finish panel. 
    * 
    * @param model 
    */ 
    public FinishPanelBuilder() 
    { 
    buildFinishPanel(); 
    } 

    /** 
    * Builds up the panel. 
    */ 
    public void buildFinishPanel() 
    { 
    setLayout(new BorderLayout()); 

    JTextWrapPane textPaneResult = new JTextWrapPane(); 
    //This is my own class of JTextPane to wrap text 
    StyledDocument document = textPaneResult.getStyledDocument(); 

    textPaneResult.setEditable(false); 
    textPaneResult.setBackground(Color.white); 
    textPaneResult.setFont(panelLabel.getFont()); 
    textPaneResult.setAutoscrolls(true); 
    textPaneResult.setLineWrap(false); 

    JScrollPane scrollPaneTestResult = new JScrollPane(textPaneResult); 
    scrollPaneTestResult.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); 

    panelLabel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); 
    setBorder(BorderFactory.createEtchedBorder()); 
    add(panelLabel, BorderLayout.PAGE_START); 
    add(scrollPaneTestResult, BorderLayout.CENTER); 
    } 
} 

這個完整的代碼由

JFrame frame = new JFrame(); 
frame.add(new FinishPanelBuilder(); 
+0

發佈一些SSCCE,演示如何定位組件。 –

+0

@ brano88我做到了;) –

+0

一切工作正常我再次檢查您的代碼,也許你把一些面板上'東'。 –

回答

1

好叫,我再次做到這一點,它是好的。我不知道爲什麼。我認爲這是一個被稱爲getScrollableTracksViewportWidth()的錯誤方法的原因,它被忽略了。