如何让2个JPanels彼此垂直对齐?

时间:2016-06-17 18:00:07

标签: java swing layout-manager gridbaglayout

好吧,我对Java swings很新,并且正在尝试学习它的技巧,但是正在努力解决框架中2个JPanel的对齐问题。如果您查看下面的屏幕截图,我的列名称JPanel对齐方式及其下方的JPanel对齐方式未正确对齐。

以下是我的代码。

    JPanel columns = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weighty = 1.0;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.VERTICAL;


    text = "<html><b>Action</b></html>";        
    JLabel label_action = new JLabel(text);
    columns.add(label_action, gbc);
    columns.setLayout(new GridBagLayout());


    JPanel values= new JPanel(new GridBagLayout());
    values.setBorder(BorderFactory.createRaisedBevelBorder());  

    gbc.weighty = 1.0;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.VERTICAL;

  DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();

    model.addElement("click");
    model.addElement("Input Keys");

    final JComboBox<String> comboBox1 = new JComboBox<String>(model);
    AutoCompleteDecorator.decorate(comboBox1);
    comboBox1.setPreferredSize(new Dimension(200, 22));
    values.add(comboBox1, gbc);
    values.setLayout(new GridBagLayout());

enter image description here

1 个答案:

答案 0 :(得分:2)

甚至不愿意与GridBagLayout合作。它是一个设计糟糕的布局管理器,不符合当今的要求。我也推荐 MigLayoutGroupLayout

了解使用MigLayout创建布局有多容易:

package com.zetcode;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;


public class MigLayoutAlignmentEx extends JFrame {

    public MigLayoutAlignmentEx() {

        initUI();
    }

    private void initUI() {

        setLayout(new MigLayout());

        JLabel lbl1 = new JLabel("Action");
        JLabel lbl2 = new JLabel("Choose Selector");
        JLabel lbl3 = new JLabel("Element Properties");
        JLabel lbl4 = new JLabel("Values");
        JLabel lbl5 = new JLabel("Element Name");
        JLabel lbl6 = new JLabel("Comments");

        JComboBox combo1 = new JComboBox();
        JComboBox combo2 = new JComboBox();

        JTextField field1 = new JTextField(15);
        JTextField field2 = new JTextField(15);
        JTextField field3 = new JTextField(15);
        JTextField field4 = new JTextField(15);

        add(lbl1);
        add(lbl2);
        add(lbl3);
        add(lbl4);
        add(lbl5);
        add(lbl6, "wrap");

        add(combo1, "w 150lp");
        add(combo2, "w 150lp");
        add(field1);
        add(field2);
        add(field3);
        add(field4, "wrap");

        pack();

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutAlignmentEx ex = new MigLayoutAlignmentEx();
            ex.setVisible(true);
        });        
    }
}

您的用户界面非常容易创建;你只需将组件放入单元格中。但是,与MigLayout不同,网格是预先创建的 GridBagLayout您必须手动创建每个单元格,这很困难且容易出错。

enter image description here