java中gridbaglayout,面板在Java中没有使用GridBagLayout正确调整大小

所以我使用GridBagLayout作为我的GUI,我遇到了一些问题,没有正确调整大小 . 在任何人建议替代布局之前,我很肯定我想使用GridBagLayout;我将拥有大量的组件,我想正确地将它们布局,当前的代码/图像仅显示我现在拥有的内容 .

无论如何,问题是它没有正确调整自身大小 . 我希望它在调整大小时保持当前的宽高比,但事实并非如此 . 某些面板在调整大小时会被赋予某种优先级,这样当我调整大小时,它会破坏纵横比 . 基本上,我有一些面板比另一面板大,但在调整大小后,它变得比另一个小,如下:

现在,我真正想要的是让他们保持已经呈现的比例 . 正如您所看到的,我希望GUI的聊天部分比左侧面板中的任何一个都小,就像它最初一样 .

下面是我现在生成的代码:

jpPack = new JPanel();

jpCards = new JPanel();

jpInfo = new JPanel();

jpChat = new JPanel();

jpPack.setBackground(Color.red);

jpCards.setBackground(Color.blue);

jpInfo.setBackground(Color.green);

//create the Constraints and set some that will apply to each panel

GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.BOTH;

//set the number of columns/rows to use

c.gridwidth = 1;

c.gridheight = 1;

//set the x and y position

c.gridx = 0;

c.gridy = 0;

//set the weight properties

c.weightx = 1.0;

c.weighty = 1.0;

getContentPane().add(jpCards, c);

//set the number of columns/rows to use

c.gridwidth = 1;

c.gridheight = 1;

//set the x and y position

c.gridx = 0;

c.gridy = 1;

//set the weight properties

c.weightx = 1.0;

c.weighty = 1.0;

getContentPane().add(jpPack, c);

//set the number of columns/rows to use

c.gridwidth = 1;

c.gridheight = 1;

//set the x and y position

c.gridx = 1;

c.gridy = 0;

//set the weight properties

c.weightx = 0.2;

c.weighty = 0.2;

getContentPane().add(jpInfo, c);

//set the number of columns/rows to use

c.gridwidth = 1;

c.gridheight = 1;

//set the x and y position

c.gridx = 1;

c.gridy = 1;

//set the weight properties

c.weightx = 0.2;

c.weighty = 0.2;

getContentPane().add(jpChat, c);

jpChat.setLayout(new GridLayout());

jpChat.add(client.gui.getContentPane(), c);

setVisible(true);

虽然我很喜欢,但我也可以问我的其他问题 . 我想为整个GUI和聊天面板设置最小大小限制 . 对于第一个,我希望它禁止用户将其大小调整为小于某个x和某个y . 对于后者,我希望它保持我所说的我想要的宽高比,然后在我达到两个右面板的最小值之后调整左侧面板的大小 . 也就是说,它会调整每个面板的大小直到某个尺寸,然后继续调整整个GUI的大小,但只需调整左侧面板的大小,使右侧面板保持最小尺寸 .

谢谢你的帮助 . 这真是让我烦恼,我花了几个小时试图做到这一点,乱搞GridBagLayoutConstraints并没有得到任何地方 . 这是我得到的最好的 .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值