gpt4 book ai didi

java - 如何调整GridBagLayout中JPanel的大小?

转载 作者:行者123 更新时间:2023-11-30 07:59:20 25 4
gpt4 key购买 nike

我确信这个问题可能是重复的,但我仍然发布,因为我无法找到解决方案/纠正代码中的错误。我的 Java GUI 的一部分使用了 GridBagLayout。此布局将有 3 个组件,2 个单选按钮将位于顶部(并排放置),其余空间应有一个 JPanel(从单选按钮下方的下一行开始,直到可用空间的末尾)。我在论坛和外部查看了不同的示例,但无法解决问题。

使用以下代码,我的 GUI 部分如下所示:

enter image description here

ImageDisplay = new JPanel(new GridBagLayout());
GridBagConstraints g = new GridBagConstraints();
g.insets = new Insets(5, 5, 5, 5); // insets for all components


rawImage = new JRadioButton("Raw", true);
peakPickedImage = new JRadioButton("Peak picked");
radioButtonGroup.add(rawImage);
radioButtonGroup.add(peakPickedImage);

g.fill = GridBagConstraints.HORIZONTAL;
g.gridx = 0;
g.gridy = 0;
g.gridwidth = 1;
g.gridheight = 1;

ImageDisplay.add(rawImage, g);

g.gridx = 1;
g.gridy = 0;
g.gridwidth = 1;
g.gridheight = 1;
g.weightx = 0;
g.weighty = 0;

ImageDisplay.add(peakPickedImage, g);

JPanel imagePanel = new JPanel();
g.gridx = 0;
// g.gridy = 0;
g.weightx = 1.0;
g.weighty = 0.75;
g.gridwidth = 3;
g.gridheight = 3;
// g.fill = GridBagConstraints.BOTH;

// g.fill = GridBagConstraints.SOUTH;

imagePanel.setBorder(BorderFactory.createEtchedBorder());

ImageDisplay.add(imagePanel, g);

并且,取消注释

g.fill = GridBagConstraints.BOTH;

我得到了 JPanel,其中包含两个单选按钮。如何解决这个问题?

最佳答案

尝试这样:

JPanel ImageDisplay = new JPanel(new GridBagLayout());
GridBagConstraints g = new GridBagConstraints();
g.insets = new Insets(5, 5, 5, 5); // insets for all components
g.weightx = 0.0;
g.weighty = 0.0;

JRadioButton rawImage = new JRadioButton("Raw", true);
JRadioButton peakPickedImage = new JRadioButton("Peak picked");
ButtonGroup radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(rawImage);
radioButtonGroup.add(peakPickedImage);

g.fill = GridBagConstraints.HORIZONTAL;
g.gridx = 0;
g.gridy = 0;
g.gridwidth = 1;
g.gridheight = 1;

ImageDisplay.add(rawImage, g);

g.gridx = 1;
g.gridy = 0;

ImageDisplay.add(peakPickedImage, g);

JPanel imagePanel = new JPanel();
g.gridx = 0;
g.gridy = 1;
g.gridwidth = 2;
g.weightx = 1.0; // fill the rest of the space
g.weighty = 1.0;
g.fill = GridBagConstraints.BOTH;

imagePanel.setBorder(BorderFactory.createEtchedBorder());

ImageDisplay.add(imagePanel, g);

关于java - 如何调整GridBagLayout中JPanel的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32181878/

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