gpt4 book ai didi

java - GridBagLayout 填充

转载 作者:行者123 更新时间:2023-12-03 18:41:50 28 4
gpt4 key购买 nike

我正在使用 GridBagLayout到(当前)显示两行。我知道这种布局对于这项任务来说太过分了,但我正在努力学习如何使用它。问题是我已将两个面板添加到两个单独的行中,并且内容周围存在巨大差距(请参见下面的图像和代码):
alt text http://www.imagechicken.com/uploads/1264533379009864500.png

Image background;
public Table(){
super();
ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
background = ii.getImage();
setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);

setLayout(new GridBagLayout());

GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;

JButton button = new JButton("hello world");
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(800,100));
panel1.add(button, BorderLayout.CENTER);
panel1.setBackground(Color.yellow);
add(panel1, constraints);

constraints.gridx = 0;
constraints.gridy = 1;

JPanel middlePanel = new JPanel();
middlePanel.setPreferredSize(new Dimension(800,350));
middlePanel.add(button, BorderLayout.CENTER);
middlePanel.setBackground(Color.blue);
add(middlePanel, constraints);


}

最佳答案


constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1d;
constraints.weighty = 1d;

JavaDocweightx/ weighty说:

Specifies how to distribute extra horizontal/vertical space.



JavaDocfill :

This field is used when the component's display area is larger than the component's requested size. It determines whether to resize the component, and if so, how.

关于java - GridBagLayout 填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2141594/

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