gpt4 book ai didi

java - GridBagLayout 中组件的大小不正确

转载 作者:行者123 更新时间:2023-12-01 23:54:04 24 4
gpt4 key购买 nike

我正在用 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 需要填满整个屏幕(因此它需要更高)。

我该怎么做?

最佳答案

this.add( jp, BorderLayout.CENTER );

按照规定in the documentation :

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.

关于java - GridBagLayout 中组件的大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com