1

这是对这个问题的扩展:Java gridbaglayout questions。由于 panel1 的宽度溢出,Panel1 与 panel2 重叠。但是,如果我删除gc.gridwidth = 2它,它将正确对齐 panel2,但它也会将组合框移动到右侧的原始位置。我一直在玩不同的 gridbaglayout 属性,但无法像我想要的那样对齐。

这是我的代码:

public void createGUI()
{
   main_panel = new JPanel();
   main_panel.setLayout(new GridBagLayout());
   GridBagConstraints gc = new GridBagConstraints();

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(label, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 1;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(band_combobox, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 1;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel1, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 1;
   gc.gridy = 1;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel2, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 2;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel3, gc);
}

在此处输入图像描述

我正在尝试做这样的事情:

在此处输入图像描述

4

4 回答 4

2

试试这个 :

public void createGUI()
{
   main_panel = new JPanel();
   main_panel.setLayout(new GridBagLayout());
   GridBagConstraints gc = new GridBagConstraints();

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(label, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 1;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(band_combobox, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 1;
   gc.gridwidth = 1;
gc.weightx = 1;
gc.weighty = 1;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel1, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 1;
   gc.gridy = 1;
   gc.gridwidth = 1;
gc.weightx = 1;
gc.weighty = 1;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel2, gc);

   gc.fill = GridBagConstraints.BOTH;
   gc.gridx = 0;
   gc.gridy = 2;
gc.weightx = 1;
gc.weighty = 1;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel3, gc);
}

你没有设置重量,你的宽度设置有点狡猾。

还将带标签和组合放在 JPanel 中,并将其宽度设置为 2,并将其添加到顶部。这样看起来好多了。

另请查看我拥有的 GridBagLayout 教程(在我的博客中,在我的个人资料中)以获得更多帮助。

于 2013-05-28T08:57:55.250 回答
2

不要害怕使用复合布局来满足您的需求。您的标签和组合框想要“浮动”在第一行。这将是一件很难实现的事情GridBagLayout,但是通过将它们添加到另一个容器,使用不同的布局管理器,这是可以实现的......

在此处输入图像描述

当您更改组件时gridWidth,或者gridHeight它将允许它们“流动”到其他列和行中,这意味着它们可能位于占据该空间的组件之上或之下,具体取决于添加它们的时间......

另外,查看weight属性。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;

public class TestPane {
    private JPanel main_panel;

    public static void main(String[] args) {
        new TestPane();
    }

    public TestPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                createGUI();;

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(main_panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public void createGUI() {

        main_panel = new JPanel();
        main_panel.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        JLabel label = new JLabel("Band Directory");
        JComboBox band_combobox = new JComboBox();

        JPanel panel1 = new JPanel();
        panel1.add(new JLabel("1"));
        panel1.setBorder(new TitledBorder("Panel1"));
        JPanel panel2 = new JPanel();
        panel2.setBorder(new TitledBorder("Panel2"));
        panel2.add(new JLabel("2"));
        JPanel panel3 = new JPanel();
        panel3.setBorder(new TitledBorder("Panel3"));
        panel3.add(new JLabel("3"));

        JPanel top = new JPanel();
        top.add(label);
        top.add(band_combobox);

        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gc.insets = new Insets(5, 0, 10, 0);
        main_panel.add(top, gc);

        gc.gridwidth = 1;
        gc.weightx = 0.5;
        gc.gridx = 0;
        gc.gridy = 1;
        gc.insets = new Insets(0, 0, 10, 0);
        main_panel.add(panel1, gc);

        gc.gridx = 1;
        gc.gridy = 1;
        gc.insets = new Insets(0, 0, 10, 0);
        main_panel.add(panel2, gc);

        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1;
        gc.gridx = 0;
        gc.gridy = 2;
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gc.insets = new Insets(0, 0, 10, 0);
        main_panel.add(panel3, gc);
    }
}

您可能会发现阅读如何使用 GridBagLayout很有用

于 2013-05-28T09:06:43.057 回答
2

乐队目录标签、组合框、成员面板和 CD 列表面板应分配 gridwidth = 1 并添加 CD 面板为 gridwith = 2。

public JPanel getComponentPanel()
{
   main_panel = new JPanel();
   main_panel.setLayout(new GridBagLayout());
   GridBagConstraints gc = new GridBagConstraints();

   gc.gridx = 0;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(label, gc);

   gc.gridx = 1;
   gc.gridy = 0;
   gc.insets = new Insets(5, 10, 10, 0);
   main_panel.add(band_combobox, gc);

   gc.gridx = 0;
   gc.gridy = 1;
   gc.insets = new Insets(0, 10, 10, 0);
   panel1.setBorder(
           BorderFactory.createLineBorder(Color.BLACK));
   panel1.setMinimumSize(new Dimension(100, 50));
   panel1.setPreferredSize(new Dimension(100, 50));
   panel1.setMaximumSize(new Dimension(100, 50));
   main_panel.add(panel1, gc);

   gc.gridx = 1;
   gc.gridy = 1;
   gc.insets = new Insets(0, 10, 10, 0);
   panel2.setBorder(
           BorderFactory.createLineBorder(Color.BLACK));
   panel2.setMinimumSize(new Dimension(100, 50));
   panel2.setPreferredSize(new Dimension(100, 50));
   panel2.setMaximumSize(new Dimension(100, 50));
   main_panel.add(panel2, gc);

   gc.gridx = 0;
   gc.gridy = 2;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 10, 10, 0);
   panel3.setBorder(
           BorderFactory.createLineBorder(Color.BLACK));
   panel3.setMinimumSize(new Dimension(100, 50));
   panel3.setPreferredSize(new Dimension(100, 50));
   panel3.setMaximumSize(new Dimension(100, 50));
   main_panel.add(panel3, gc);

   return main_panel;
}
于 2013-05-28T09:18:45.953 回答
1
  • GBC 需要在第一行创建列坐标,然后为整个容器创建矩阵,需要柔和的渐变来调整大小而不会闪烁

  • 当然可以在不将不可见的 JComponents(在这种情况下为添加了边框的 JLabel)放置到 contianer 的情况下虚拟地创建这个 GBC 矩阵,也许为什么要打扰,

  • 这是关于使用 GBC 的 AbsoluteLayout,另一种方法是使用不可见的 JComponents

在此处输入图像描述

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 FrameAndGBC {

    private JLabel hidelLabel;
    private JLabel firstLabel;
    private JTextField firstText;
    private JFrame frame = new JFrame("Frame And GBC");

    public FrameAndGBC() {
        JPanel panel = copyTextNorthPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    private JPanel copyTextNorthPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        for (int k = 0; k < 50; k++) {
            hidelLabel = new JLabel("     ");
            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);
        }
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                FrameAndGBC fs = new FrameAndGBC();
            }
        });
    }
}
于 2013-05-28T10:29:33.373 回答