sách gpt4 ai đã đi

java - GridBagLayout 填充

In lại 作者:行者123 更新时间:2023-12-03 18:41:50 29 4
mua khóa gpt4 Nike

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

Image background;
public Table(){
siêu();
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);


}

1 Câu trả lời


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

JavaDocweightx/ weightygiải thích:

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/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com