【发布时间】:2010-02-20 17:22:16
【问题描述】:
在下面的示例中,我希望能够在最大化或最小化窗口时扩展文本区域。现在,textarea 设置为 cols/rows。如果我点击最大化,文本区域应该随着窗口的扩大而扩大。
注意:这是一个伪示例。我可能会添加更多组件,这就是我使用 GridBagLayout 管理器的原因。
下面的版本给了我想要的:
package org.berlin.pino.test.functional.jogl;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class BasicText {
public static JPanel buildPanel() {
final JPanel panel = new JPanel(new GridBagLayout());
final GridBagConstraints gc = new GridBagConstraints();
final JTextArea text = new JTextArea("Text");
final JScrollPane scrollPane = new JScrollPane(text);
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 1;
gc.weighty = 1;
// Add the textarea -> scroll pane -> to the panel -> to the jframe
panel.add(scrollPane, gc);
return panel;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Hello World!");
frame.setLayout(new GridBagLayout());
final GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 1;
gc.weighty = 1;
frame.add(buildPanel(), gc);
frame.setPreferredSize(new Dimension(300, 300));
frame.setLocation(200, 100);
frame.setBackground(Color.white);
frame.pack();
frame.setVisible(true);
}
} // End of the class //
【问题讨论】: