2014-05-04 93 views
1

我有一個簡單的問題,但我沒有找到它的好方案。我有一個JFrame,其組件爲SOUTH,NORTH,EASTCENTER。在CENTER是我的問題。在中心我有一個JPanelBorderlayout和2 JTextAreas(一個NORTH,一個int CENTER)。Borderlayout中Textarea maximalsize

我希望第一塊麪板始終位於面板的頂部,並將最大值(如果需要)拉伸到面板中間,而不是更多。第二個區域應該從第一個區域的末尾開始。如果兩個文本區域中的一個較大,則應該出現JScrollPane

什麼是最好的方法來實現呢?我應該使用面板的其他佈局嗎?

這是我的一點它執行例如:

public class myGUI { 

    public static void main(String[] args) { 
     new myGUI(); 
    } 

    public myGUI() { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(400, 400); 

    //Add Content at South, West and North.... 
     JPanel centerPanel = new JPanel(); 
     centerPanel.setLayout(new BorderLayout()); 
     centerPanel.add(new JScrollPane(new JTextArea("AREA")), BorderLayout.NORTH); 
     centerPanel.add(new JScrollPane(new JTextArea("AREA2")), BorderLayout.CENTER); 
     frame.add(centerPanel); 
     frame.setVisible(true); 
    } 
} 

中央面板的可能的情景:

enter image description here

解決方案的幫助從@Hovercraft全部鰻魚

public class MyGUI { 
public static void main(String[] args) { 
    new MyGUI(); 
} 

private static GridBagConstraints constraint = new GridBagConstraints(); 

public MyGUI() { 
    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 400); 

    frame.add(new JLabel("NORTH!"), BorderLayout.NORTH); 
    frame.add(new JLabel("EAST!"), BorderLayout.EAST); 
    frame.add(new JLabel("SOUTH!"), BorderLayout.SOUTH); 

    JPanel centerPanel = new JPanel(); 
    centerPanel.setLayout(new GridBagLayout()); 
    changeConstraint(0,0); 

    JTextArea text1 = createTextArea("11111111111111"); 
    centerPanel.add(text1, constraint); 
    changeConstraint(0,1); 

    JTextArea text2 = createTextArea("2222222222222"); 
    centerPanel.add(text2, constraint); 
    JScrollPane scroll = new JScrollPane(centerPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
    frame.add(scroll, BorderLayout.CENTER); 
    frame.setVisible(true); 
} 

/** 
* Create a Gridbag Constraint 
* @param text 
* @return 
*/ 
private void changeConstraint(int gridx, int gridy){ 
    constraint.anchor = GridBagConstraints.FIRST_LINE_START; 
    constraint.fill = GridBagConstraints.HORIZONTAL; 
    constraint.weighty = 0; 
    constraint.weightx = 1.0; 
    constraint.gridx = gridx; 
    constraint.gridy = gridy; 
} 

/** 
* Create a Textarea 
* @param text 
* @return 
*/ 
private JTextArea createTextArea(String text){ 
    JTextArea textarea = new JTextArea(text); 
    textarea.setWrapStyleWord(true); 
    textarea.setLineWrap(true); 
    return textarea; 

} 

}

+1

圖片/您想要的圖像,請。 –

+0

「我希望第一塊麪板始終位於面板頂部,並且最大限度地伸展(如果需要)面板的中間位置。」 - 如果它伸展到中間,它從哪裏開始?你的意思是拉低/向上還是向兩側?是waaaay更具體 –

+0

我添加了不同的szenarios圖片,我希望它有助於理解我的問題。雙邊框應該是一個滾動窗格^^ – pL4Gu33

回答

3

一種可能性:考慮創建一個使用GridBagLayout的JPanel,並將兩個JTextAreas添加到這個JPanel中,確保將它們的weightx設置爲0,並將weight設置爲非零值,比如1.0。然後將該JPanel放入JScrollPane和JScrollPane到您的GUI中。

+0

謝謝。我在第一篇文章中添加了我的解決方案。我只有一個問題:面板/文本框不在「CENTER」位置的頂部開始,但我不能使用網格包的錨來更改它。 – pL4Gu33