我有一個簡單的問題,但我沒有找到它的好方案。我有一個JFrame
,其組件爲SOUTH
,NORTH
,EAST
和CENTER
。在CENTER
是我的問題。在中心我有一個JPanel
與Borderlayout
和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);
}
}
中央面板的可能的情景:
解決方案的幫助從@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;
}
}
圖片/您想要的圖像,請。 –
「我希望第一塊麪板始終位於面板頂部,並且最大限度地伸展(如果需要)面板的中間位置。」 - 如果它伸展到中間,它從哪裏開始?你的意思是拉低/向上還是向兩側?是waaaay更具體 –
我添加了不同的szenarios圖片,我希望它有助於理解我的問題。雙邊框應該是一個滾動窗格^^ – pL4Gu33