0

我正在用Java制作一个简单的界面。我想在顶部有一个设置大小为 200*20 的 JButton,在它的正下方有一个封装 JTextArea 的 JScrollPane。此字段的大小应该是放置它的 JPanel 的剩余宽度和高度(但最小为 640*480),并且在使窗口变大/变小时必须调整大小(直到其最小大小)。

我尝试使用 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);

这就是它现在的样子:enter image description here正如我之前所描述的,这不是我想要的;TextArea 需要填满整个屏幕(所以它需要更高)。

我怎样才能做到这一点?

4

1 回答 1

3

this.add( jp, BorderLayout.CENTER );

文档中所述

The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.

于 2013-04-07T21:15:23.253 回答