如何使用GridBagLayout实现此布局?

问题描述 投票:0回答:1

enter image description here

我对如何开始执行此操作感到困惑,我应该创建一个基本上包含4行的GridBagLayout,然后在每行中输入组件吗?

java swing layout-manager gridbaglayout jcomponent
1个回答
0
投票

要精确地复制此内容,将非常棘手,因为gridBagLayout完全按照其说的...将项目放置在网格上。

为了使项目像图像一样显示精确地,您将需要跨越多个单元格并将它们重叠。您必须先四处游玩,然后才能完全看到自己想要的样子。

话虽如此,使用GridBagLayout时应牢记这些:

gbc.gridx = 0; // The x poisiton, greater value means further east
gbc.gridy = 0; // The y position, greater value means further south

因此,如果仅使用两列,则退出按钮为:

gbc.gridx = 1;
gbc.gridy = 2;

但是,如果要使其看起来像图像,则必须使用跨越多个网格或单元格的项目来播放。

gbc.gridwidth = 2; // To span 2 cells horizontally
gbc.gridheight = 2; // To span 2 cells vertically

您也可以尝试使用体重。例如:

gbc.weightx = 0.8; // Will cause the item at this cell to span 80% of the width, if there are only two columns

[将项目添加到组件中时不要忘记包含gbc,并且添加每个组件之前重置每个gbc值,即使它们是相同的]以提高可读性。

© www.soinside.com 2019 - 2024. All rights reserved.