gpt4 book ai didi

java - GridBagLayout gridheight 约束不影响结果

转载 作者:行者123 更新时间:2023-12-02 02:05:00 25 4
gpt4 key购买 nike

我正在使用 GridBag 布局开发 Java Swing 项目。我正在尝试制作两个具有相同宽度并水平对齐但高度不同的面板。

像这样: enter image description here

我有以下代码:

import javax.swing.*;
import java.awt.*;

public class Mega extends JFrame {

public static void main(String[] args) {

new Mega();

}

public Mega() {

Dimension minDimension = new Dimension();
minDimension.width = 800;
minDimension.height = 800;

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
this.add(mainPanel);

GridBagConstraints constraints = new GridBagConstraints();

JPanel topPanel = new JPanel();
topPanel.setBackground(Color.GRAY);

constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.fill = GridBagConstraints.BOTH;
mainPanel.add(topPanel, constraints);

JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.GREEN);

constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 3;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.PAGE_END;
constraints.fill = GridBagConstraints.BOTH;
mainPanel.add(bottomPanel, constraints);

this.setVisible(true);

}

}

使用此代码,底部面板的高度应是第一个面板的三倍,因为它占据三行,而顶行仅占据一行。然而,我得到的看起来像这样。

enter image description here

看起来 gridheight = 3 约束没有产生影响,因为两个面板具有相同的高度。我做错了什么?

最佳答案

gridHeight 不会有任何影响,因为没有更多的行可以扩展(将 gridWidthgridHeight 视为“扩展” "),相反,要么从组件中提供更好的大小调整提示,要么使用 weighty 属性

Example

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Mega extends JFrame {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Mega();
}
});

}

public Mega() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel() {
// This is done for demonstration purposes
// it would be better for the child components
// to provide appropriate sizing hints
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
};
mainPanel.setLayout(new GridBagLayout());
this.add(mainPanel);

GridBagConstraints constraints = new GridBagConstraints();

JPanel topPanel = new JPanel();
topPanel.setBackground(Color.GRAY);

constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1;
constraints.weighty = 0.25;
constraints.gridwidth = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5, 5, 5, 5);
mainPanel.add(topPanel, constraints);

JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.GREEN);

constraints.gridy = 1;
constraints.weighty = 0.75;
mainPanel.add(bottomPanel, constraints);

pack();
setLocationRelativeTo(null);
this.setVisible(true);

}

}

关于java - GridBagLayout gridheight 约束不影响结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50977851/

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