0
我在Java中做了一個簡單的接口。我想在上面有一個設置爲200 * 20的JButton,並且在它的正下方有一個封裝了JTextArea的JScrollPane。此字段的大小應該是放置它的JPanel的剩餘寬度和高度(但最小爲640 * 480),並且必須在使窗口變大/變小(直到其最小大小)時調整其大小。GridBagLayout中組件的大小不正確
我試着用GridBagLayout做這個,但是這似乎沒有工作。有人知道我能如何解決這個問題嗎?
//Initialize components
Dimension minSize = new Dimension(640, 480);
info = new JTextArea("hello");
info.setEditable(false);
JScrollPane scrollPane = new JScrollPane(info);
scrollPane.setMinimumSize(minSize);
JButton update = new JButton("update");
JPanel jp = new JPanel();
jp.setLayout(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();
//add components
grid.fill = GridBagConstraints.NONE;
grid.anchor = GridBagConstraints.NORTHWEST;
grid.gridheight = 1;
grid.gridwidth = 1;
grid.weighty = 1.0;
grid.weightx = 1.0;
grid.gridx = 0;
grid.gridy = 0;
jp.add(update, grid);
grid.fill = GridBagConstraints.BOTH;
grid.gridx = 0;
grid.gridy = 1;
grid.weighty = 15;
grid.gridheight = 15;
grid.gridwidth = 15;
jp.add(scrollPane, grid);
this.add(jp,BorderLayout.NORTH);
這是現在的樣子:如我之前描述這是不是我想要的; TextArea需要填滿整個屏幕(所以它需要更高)。
我該怎麼做?
謝謝!這就是訣竅:)爲什麼它的工作? – 2013-04-07 21:17:02
粘貼文檔以解釋其工作原理。 – Aubin 2013-04-08 17:09:03