java将标签添加到面板_Java,当使用GridBagLayout布局面板时,向面板添加按钮会改变尺寸,我该如何防止这种情况?...

本文探讨了在使用GridBagLayout布局管理器时遇到的问题,当向面板添加组件后,面板尺寸不再受权重值控制。作者通过代码示例展示了在JFrame中使用GridBagLayout布局多个JPanels的情况,当面板内容为空时布局表现正常,但添加按钮后,某些面板尺寸发生变化。问题集中在如何保持面板尺寸仅由权重值决定,而不受组件大小影响。
摘要由CSDN通过智能技术生成

好的,所以我正在搞乱GridBagLayout以掌握它 . 我想用它来在JFrame中布局JPanels,但这些单独的面板将使用不同的布局管理器,GridBagLayout可能不适合 .

但我有一个问题 . 如果我没有向面板添加任何内容,则所有比例都完美地完成,如下例所示:

public class RealMess extends JFrame{

private JPanel panel;

public RealMess(){

initUI();

}

public final void initUI(){

Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();

int width = (int) screensize.getWidth();

int height = (int) screensize.getHeight();

panel = new JPanel();

panel.setLayout(new GridBagLayout());

JPanel left = new JPanel();

left.setBackground(Color.GREEN);

JPanel centre = new JPanel();

centre.setBackground(Color.PINK);

JPanel right = new JPanel();

right.setBackground(Color.CYAN);

JPanel leftBottom = new JPanel();

leftBottom.setBackground(Color.LIGHT_GRAY);

leftBottom.setLayout(new FlowLayout(0));

JPanel bottom = new JPanel();

bottom.setBackground(Color.MAGENTA);

GridBagConstraints gbc = new GridBagConstraints();

gbc.weightx = 0.11;

gbc.weighty = 0.9;

gbc.gridwidth = 1;

gbc.gridheight = 1;

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.gridx = 0;

gbc.gridy = 0;

gbc.fill = GridBagConstraints.BOTH;

panel.add(left, gbc);

gbc.weightx = 0.11;

gbc.weighty = 0.1;

gbc.anchor = GridBagConstraints.LAST_LINE_START;

gbc.gridx = 0;

gbc.gridy = 1;

gbc.fill = GridBagConstraints.BOTH;

panel.add(leftBottom, gbc);

gbc.weightx = 0.78;

gbc.weighty = 0.9;

gbc.anchor = GridBagConstraints.PAGE_START;

gbc.gridx = 1;

gbc.gridy = 0;

gbc.fill = GridBagConstraints.BOTH;

panel.add(centre, gbc);

gbc.weightx = 0.78;

gbc.weighty = 0.1;

gbc.anchor = GridBagConstraints.PAGE_END;

gbc.gridx = 1;

gbc.gridy = 1;

gbc.fill = GridBagConstraints.BOTH;

panel.add(bottom, gbc);

gbc.weightx = 0.11;

gbc.weighty = 1;

gbc.anchor = GridBagConstraints.FIRST_LINE_END;

gbc.gridx = 2;

gbc.gridy = 0;

gbc.gridheight = 2;

gbc.fill = GridBagConstraints.BOTH;

panel.add(right, gbc);

add(panel);

setTitle("Jack");

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setExtendedState(MAXIMIZED_BOTH);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable(){

public void run(){

RealMess rm = new RealMess();

rm.setVisible(true);

}

});

}

}

我知道我不必要地重新定义了一些GridBagConstraints,但我不想错过任何东西 .

无论如何,当我向任何面板添加按钮时,我的问题就出现了 . 像这样:

public class RealMess extends JFrame{

private JPanel panel;

public RealMess(){

initUI();

}

public final void initUI(){

Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();

int width = (int) screensize.getWidth();

int height = (int) screensize.getHeight();

panel = new JPanel();

panel.setLayout(new GridBagLayout());

JPanel left = new JPanel();

left.setBackground(Color.GREEN);

JPanel centre = new JPanel();

centre.setBackground(Color.PINK);

JPanel right = new JPanel();

right.setBackground(Color.CYAN);

JPanel leftBottom = new JPanel();

leftBottom.setBackground(Color.LIGHT_GRAY);

leftBottom.setLayout(new FlowLayout(0));

JButton rewind = new JButton("1");

rewind.setPreferredSize(new Dimension(50, 25));

leftBottom.add(rewind);

JPanel bottom = new JPanel();

bottom.setBackground(Color.MAGENTA);

GridBagConstraints gbc = new GridBagConstraints();

gbc.weightx = 0.11;

gbc.weighty = 0.9;

gbc.gridwidth = 1;

gbc.gridheight = 1;

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.gridx = 0;

gbc.gridy = 0;

gbc.fill = GridBagConstraints.BOTH;

panel.add(left, gbc);

gbc.weightx = 0.11;

gbc.weighty = 0.1;

gbc.anchor = GridBagConstraints.LAST_LINE_START;

gbc.gridx = 0;

gbc.gridy = 1;

gbc.fill = GridBagConstraints.BOTH;

panel.add(leftBottom, gbc);

gbc.weightx = 0.78;

gbc.weighty = 0.9;

gbc.anchor = GridBagConstraints.PAGE_START;

gbc.gridx = 1;

gbc.gridy = 0;

gbc.fill = GridBagConstraints.BOTH;

panel.add(centre, gbc);

gbc.weightx = 0.78;

gbc.weighty = 0.1;

gbc.anchor = GridBagConstraints.PAGE_END;

gbc.gridx = 1;

gbc.gridy = 1;

gbc.fill = GridBagConstraints.BOTH;

panel.add(bottom, gbc);

gbc.weightx = 0.11;

gbc.weighty = 1;

gbc.anchor = GridBagConstraints.FIRST_LINE_END;

gbc.gridx = 2;

gbc.gridy = 0;

gbc.gridheight = 2;

gbc.fill = GridBagConstraints.BOTH;

panel.add(right, gbc);

add(panel);

setTitle("Jack");

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setExtendedState(MAXIMIZED_BOTH);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable(){

public void run(){

RealMess rm = new RealMess();

rm.setVisible(true);

}

});

}

}

bottomLeft面板变得更大,因此网格方块的大小与之相关 .

在第一个示例中,所有内容占据窗口宽度或高度的百分比:

gbc.weightx

gbc.weighty

这样做可以很容易地使一切都正确 .

是否有一种解决方法允许我强制面板尺寸仅受重量值的影响?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值