难以在多个JPanel中对齐组件

时间:2014-01-06 05:34:26

标签: java swing jpanel gridbaglayout

我正在尝试创建一个布局,其中我有两个带有边框的面板。我希望矩形边框的大小相同。我还希望底部面板中的JTextField具有更小的宽度。

问题是,每当我水平填充底部面板(使用GridBagConstraints.HORIZONTAL)时,该面板内的复选框和标签就会与其上方面板中的复选框和标签不对齐。

我希望有经验的开发人员能够深入了解我如何使两个JPanel和它们的矩形边框相互之间的尺寸相等,同时还要将它们的复选框和标签对齐。

现在看来是这样的:

enter image description here

以下是重现问题的代码:

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class Example
{
    public static void main(String[] args) 
    {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try 
                {
                    MyFrame frame = new MyFrame();
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        });
    }
}

class MyFrame
{
    private JFrame frame;

    private MyDialog dialog;

    public MyFrame()
    {
        frame = new JFrame();

        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        dialog = new MyDialog(frame);
    }
}

class MyDialog
{
    private JDialog dialog;

    private JPanel mainPanel,
                   panel1,
                   inputPanel1,
                   panel2,
                   inputPanel2;

    private JLabel titleLabel,
                   label1,
                   label2;

    private JCheckBox checkBox1,
                      checkBox2;

    private JTextField textField1,
                       textField2;

    public MyDialog(JFrame frame)
    {
        dialog = new JDialog(frame, true);

        buildPanel();

        dialog.add(mainPanel);

        dialog.pack();
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
    }

    private void buildPanel()
    {
        mainPanel = new JPanel(new GridBagLayout());

        titleLabel = new JLabel("Title");



        checkBox1 = new JCheckBox("checkBox1");

        label1 = new JLabel("label1:");

        textField1 = new JTextField(15);

        inputPanel1 = new JPanel(new GridBagLayout());
        inputPanel1.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0));  // indent the label and textfield
        inputPanel1.add(label1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        inputPanel1.add(textField1, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));

        panel1 = new JPanel(new GridBagLayout());
        Border border1 = BorderFactory.createEtchedBorder();
        panel1.setBorder(border1);
        panel1.add(checkBox1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        panel1.add(inputPanel1, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));



        checkBox2 = new JCheckBox("checkBox2");

        label2 = new JLabel("label2:");

        textField2 = new JTextField(8);

        inputPanel2 = new JPanel(new GridBagLayout());
        inputPanel2.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0));  // indent the label and textfield
        inputPanel2.add(label2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        inputPanel2.add(textField2, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));

        panel2 = new JPanel(new GridBagLayout());
        Border border2 = BorderFactory.createEtchedBorder();
        panel2.setBorder(border2);
        panel2.add(checkBox2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        panel2.add(inputPanel2, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));



        mainPanel.add(titleLabel, getConstraints(0, 0, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE));
        mainPanel.add(panel1, getConstraints(0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        // When I fill panel2 horizontally, the checkbox becomes misaligned with the above checkbox:
        //mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL));


  }

  private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
  {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.insets = new Insets(5, 5, 5, 5);
      gbc.ipadx = 0;
      gbc.ipady = 0;
      gbc.gridx = gridx;
      gbc.gridy = gridy;
      gbc.gridwidth = gridwidth;
      gbc.gridheight = gridheight;
      gbc.anchor = anchor;
      gbc.fill = fill;

      return gbc;
  }

}

1 个答案:

答案 0 :(得分:1)

enter image description here

我做的是

  • inputPanelX使用FlowLayout FlowLayout.LEADING,而不是使用GirdBagLayout

    inputPanel1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
    
  • 第二个我和第一个Dimension做了同样的事情

    inputPanel2 = new JPanel(new FlowLayout(FlowLayout.LEADING)){
        Dimension dim = new Dimension(inputPanel1.getPreferredSize());
        public Dimension getPreferredSize(){
            return new Dimension(dim);
        }
    };
    

这就是我改变的一切。请参阅下面的完整代码

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class Example
{
    public static void main(String[] args) 
    {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try 
                {
                    MyFrame frame = new MyFrame();
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        });
    }
}

class MyFrame
{
    private JFrame frame;

    private MyDialog dialog;

    public MyFrame()
    {
        frame = new JFrame();

        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        dialog = new MyDialog(frame);
    }
}

class MyDialog
{
    private JDialog dialog;

    private JPanel mainPanel,
                   panel1,
                   inputPanel1,
                   panel2,
                   inputPanel2;

    private JLabel titleLabel,
                   label1,
                   label2;

    private JCheckBox checkBox1,
                      checkBox2;

    private JTextField textField1,
                       textField2;

    public MyDialog(JFrame frame)
    {
        dialog = new JDialog(frame, true);

        buildPanel();

        dialog.add(mainPanel);

        dialog.pack();
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
    }

    private void buildPanel()
    {
        mainPanel = new JPanel(new GridBagLayout());

        titleLabel = new JLabel("Title");



        checkBox1 = new JCheckBox("checkBox1");

        label1 = new JLabel("label1:");

        textField1 = new JTextField(15);

        inputPanel1 = new JPanel(new FlowLayout(FlowLayout.LEADING));

        inputPanel1.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0));  // indent the label and textfield
        inputPanel1.add(label1);
        inputPanel1.add(textField1);

        panel1 = new JPanel(new GridBagLayout());
        Border border1 = BorderFactory.createEtchedBorder();
        panel1.setBorder(border1);
        panel1.add(checkBox1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        panel1.add(inputPanel1, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));



        checkBox2 = new JCheckBox("checkBox2");

        label2 = new JLabel("label2:");

        textField2 = new JTextField(8);

        inputPanel2 = new JPanel(new FlowLayout(FlowLayout.LEADING)){
            Dimension dim = new Dimension(inputPanel1.getPreferredSize());
            public Dimension getPreferredSize(){
                return new Dimension(dim);
            }
        };
        inputPanel2.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0));  // indent the label and textfield
        inputPanel2.add(label2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        inputPanel2.add(textField2, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));

        panel2 = new JPanel(new GridBagLayout());
        Border border2 = BorderFactory.createEtchedBorder();
        panel2.setBorder(border2);
        panel2.add(checkBox2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        panel2.add(inputPanel2, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));



        mainPanel.add(titleLabel, getConstraints(0, 0, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE));
        mainPanel.add(panel1, getConstraints(0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
        // When I fill panel2 horizontally, the checkbox becomes misaligned with the above checkbox:
        //mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL));


  }

  private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
  {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.insets = new Insets(5, 5, 5, 5);
      gbc.ipadx = 0;
      gbc.ipady = 0;
      gbc.gridx = gridx;
      gbc.gridy = gridy;
      gbc.gridwidth = gridwidth;
      gbc.gridheight = gridheight;
      gbc.anchor = anchor;
      gbc.fill = fill;

      return gbc;
  }

}