首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GridBagLayout的困难

GridBagLayout的困难
EN

Stack Overflow用户
提问于 2012-11-30 14:43:49
回答 2查看 195关注 0票数 1

我想布局组件,如图所示,只使用GridBagLayout

我已经尝试了几个约束,但始终没有得到预期的结果,所以我开始怀疑是否真的可以只使用GridBagLayout。困难在于C1、C2和C3组件。

C1和C2是JComponent,它们将在内部包含其他组件,如JPanel。我已经设置了他们的最小和首选大小。C3是一个JButton

C1不应该占用额外的空间,所以我将它的权重x设置为0,将网格宽度设置为1(也尝试了2,因为它在C2和C3上跨越)。

C2占用了所有额外的空间,我将它的权重x设置为1,网格宽度设置为3。

GUI不可调整大小。

这个LayoutManager我已经用过好几次了,但还是不太精通,谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-30 15:26:53

  • 我只讨论GridBagLayout,甚至这可能是MigLayout的工作(MigLayout有额外的参数用于填充列数和行数,调整大小,例如),和/或TableLayout(???)只需要在第一行中填充所有所需的列数(只),然后创建矩阵,您可以定义任何GBC weightx, weighty, gridx, gridy和/或Anchor
  • 示例谈论<代码>H212<代码>F213

代码语言:javascript
复制
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();
            }
        });
    }
}
票数 3
EN

Stack Overflow用户

发布于 2012-11-30 15:12:45

恐怕这是不可能的。GridBagLayout无法计算出C1的起始位置和C3的起始位置之间的适当距离。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13640283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档