gpt4 book ai didi

java - GridBagLayout 的奇怪行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:30 26 4
gpt4 key购买 nike

我最近在这段代码中发现了一些奇怪的事情:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Test {

public static void main(String [] args) throws IOException {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets.top = 5;
gbc.insets.right = 5;

add(0, 0, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(2, 0, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(0, 1, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(2, 1, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(0, 2, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(1, 2, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(3, 2, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(0, 3, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(1, 3, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(3, 3, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(0, 4, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(1, 4, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(3, 4, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
add(0, 5, 1, 1, 4, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());

frame.setContentPane(panel);

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

private static void add(int gridx, int gridy, int weightx, int weighty, int gridw, int gridh, int fill, int anchor, GridBagConstraints gbc, Container container, JComponent component) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.gridwidth = gridw;
gbc.gridheight = gridh;
gbc.fill = fill;
gbc.anchor = anchor;

component.add(Box.createHorizontalStrut(5));
component.add(new JLabel("(" + gridx + "; " + gridy + ")"));
component.add(Box.createHorizontalStrut(5));
component.add(new JLabel("gridwidth = " + gridw));
component.add(Box.createHorizontalStrut(5));

container.add(component, gbc);
}

private static JPanel createPanel() {
JPanel panel1 = new JPanel();
panel1.setBackground(Color.WHITE);
panel1.setBorder(BorderFactory.createLineBorder(Color.RED));

return panel1;
}
}

运行时的样子:

java_weird

每个面板的括号和网格宽度中有坐标。从最后一行可以看出,全局网格宽度为 4。第 1 列(x=0)中的第 3-5 行(y=2、y=3、y=4)的网格宽度等于 1。但在前两行中第一列的网格宽度等于 2,它应该在框架的中间划分,但我们不是。为什么?

最佳答案

我自己发现的:

public class Test {

public static void main(String [] args) throws IOException {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets.top = 5;
gbc.insets.right = 5;

JPanel topPanel = new JPanel();
topPanel.setLayout(new GridBagLayout());
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridBagLayout());

add(0, 0, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, topPanel, createPanel());
add(1, 0, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, topPanel, createPanel());
add(0, 0, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, bottomPanel, createPanel());
add(1, 0, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, bottomPanel, createPanel());
add(2, 0, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, bottomPanel, createPanel());
add(0, 2, 1, 1, 3, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, bottomPanel, createPanel());

panel.add(topPanel);
panel.add(bottomPanel);

frame.setContentPane(panel);

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

private static void add(int gridx, int gridy, int weightx, int weighty, int gridw, int gridh, int fill, int anchor, GridBagConstraints gbc, Container container, JComponent component) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.gridwidth = gridw;
gbc.gridheight = gridh;
gbc.fill = fill;
gbc.anchor = anchor;

component.add(Box.createHorizontalStrut(5));
component.add(new JLabel("(" + gridx + "; " + gridy + ")"));
component.add(Box.createHorizontalStrut(5));
component.add(new JLabel("gridwidth = " + gridw));
component.add(Box.createHorizontalStrut(5));

container.add(component, gbc);
}

private static JPanel createPanel() {
JPanel panel1 = new JPanel();
panel1.setBackground(Color.WHITE);
panel1.setBorder(BorderFactory.createLineBorder(Color.RED));

return panel1;
}
}

结果如下:

java weird

我所做的:将内容 Pane 分成两个单独的面板 - 顶部(一行有两个等分的单元格)和底部(两行:一个有 3 个单元格,另一个有 1 个宽单元格) .内容 Pane 现在有 BoxLayout 而不是 GBL,并且它按预期工作。但我对 GridBagLayout 感到失望,它是我之前的完美布局。

关于java - GridBagLayout 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32861903/

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