【问题标题】:GridBagLayout difficultiesGridBagLayout 的困难
【发布时间】:2012-11-30 06:43:49
【问题描述】:

我想只使用GridBagLayout 来布置如图所示的组件。
我已经尝试了几个限制,但它从来没有达到预期的结果,所以我想知道它是否真的可能只使用GridBagLayout。困难在于 C1、C2 和 C3 组件。
C1 和 C2 是 JComponent,它将包含其他组件,例如 JPanel。我已经设置了它们的最小和首选尺寸。 C3 是JButton
C1 不应占用额外空间,因此我将其 weightx 设置为 0,将 gridwidth 设置为 1(也尝试使用 2,因为它跨越 C2 和 C3)。
C2 占用所有额外空间,我将其 weightx 设置为 1,将 gridwidth 设置为 3。
GUI 不可调整大小。
这个LayoutManager我用过好几次了,还是没掌握,谢谢大家帮忙。

【问题讨论】:

  • 1) 为区域 C1、2 和 3 提供一些背景信息。这些区域是什么? 2)它应该可以调整大小吗?应该在哪里为 GUI 分配额外的空间和高度?
  • 对(正如@Andrew Thompson 提到的),必须将不可见的 JComponents 或 JLabel 放在那里,但必须正确设置 GBC,或者您可以在顶部,然后只使用 GBC 列数

标签: java swing awt layout-manager gridbaglayout


【解决方案1】:
  • 我将只谈论GridBagLayout,即使这也可能是MigLayout 的工作(MigLayout 具有用于填充列和行数、调整大小、e.i. 的附加参数)和/或TableLayout(???)

  • GridBagLayout 只需要在第一行(仅)填充所有所需的列数,然后创建矩阵,您可以定义任何 GBC weightx, weighty, gridx, gridy 和/或 Anchor

  • 举例说明

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class GbcLayout {

    private JFrame frame = new JFrame("GbcLayoutGbcLayout");
    private JPanel panel = new JPanel();
    private JLabel hidelLabel;
    private JLabel firstLabel;
    private JTextField firstText;

    public GbcLayout() {
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        for (int k = 0; k < 50; k++) {
            hidelLabel = new JLabel("     ");
            hidelLabel.setOpaque(true);
            hidelLabel.setBackground(Color.orange);
            hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 0.5;
            gbc.weighty = 0.5;
            gbc.gridx = k;
            gbc.gridy = 0;
            panel.add(hidelLabel, gbc);
        }
        for (int k = 0; k < 5; k++) {
            firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
            firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
            firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 5, 0);
            gbc.gridx = 0;
            gbc.gridwidth = 8;
            gbc.gridy = k + 1;
            panel.add(firstLabel, gbc);
        }
        for (int k = 0; k < 5; k++) {
            firstText = new JTextField("Testing TextField");
            firstText.setFont(new Font("Serif", Font.BOLD, 20));
            firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 5, 0);
            gbc.gridx = 9;
            gbc.gridwidth = k + 8;
            gbc.gridy = k + 1;
            panel.add(firstText, gbc);
        }
        for (int k = 0; k < 5; k++) {
            firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
            firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
            firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 5, 0);
            gbc.gridx = 20 + k;
            gbc.gridwidth = 8;
            gbc.gridy = k + 1;
            panel.add(firstLabel, gbc);
        }
        for (int k = 0; k < 5; k++) {
            firstText = new JTextField("Testing TextField");
            firstText.setFont(new Font("Serif", Font.BOLD, 20));
            firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 5, 0);
            gbc.gridx = 29 + k;
            gbc.gridwidth = 21 - k;
            gbc.gridy = k + 1;
            panel.add(firstText, gbc);
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                GbcLayout gbcl = new GbcLayout();
            }
        });
    }
}

【讨论】:

    【解决方案2】:

    恐怕这是不可能的。 GridBagLayout 无法确定 C1 起点和 C3 起点之间的正确距离。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-16
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多