Gridbaglayout问题

时间:2012-11-29 07:34:16

标签: java swing jpanel joptionpane gridbaglayout

我正在设计一个面板并在OptionPane Confirm Dialog中使用此面板,如下所示:

JOptionPane.showConfirmDialog(null, slaveGroupPanel, messages.getString("centrum.manageGroups"), JOptionPane.OK_CANCEL_OPTION);

我的网格限制是这样的:

    addGroupButton.setText(messages.getString("centrum.slavegroup.add"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    add(addGroupButton, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 5;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    add(addGroupTextField, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    add(groupNameLabel, gridBagConstraints);

    groupComboBox.setModel(new DefaultComboBoxModel(slaveGroupHash.keySet().toArray() ));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 5;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.weightx = 1;
    add(groupComboBox, gridBagConstraints);

    allSlavesList.setModel(new DefaultListModel());

    for (int i = 0; i < allSlaves.length; i++) {
        ((DefaultListModel)allSlavesList.getModel()).addElement(allSlaves[i]);
    }

    allSlavesList.setVisibleRowCount(4);
    jScrollPane2.setViewportView(allSlavesList);
    jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);


    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 0.45;
    gridBagConstraints.weighty = 1.0;
    add(jScrollPane2, gridBagConstraints);

    slavesInGroupList.setModel(new DefaultListModel());
    slavesInGroupList.setVisibleRowCount(4);
    jScrollPane3.setViewportView(slavesInGroupList);
    jScrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 0.45;
    gridBagConstraints.weighty = 1.0;
    add(jScrollPane3, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    add(allSlavesLabel, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    add(slavesInGroupLabel, gridBagConstraints);

    addSlaveButton.setIcon(GeneralObjects.getrightIcon());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.weightx = 0.1;
    gridBagConstraints.weighty = 1.0;
    add(addSlaveButton, gridBagConstraints);

    deleteSlaveButton.setIcon(GeneralObjects.getleftIcon());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.weightx = 0.1;
    gridBagConstraints.weighty = 1.0;
    add(deleteSlaveButton, gridBagConstraints);

    renameButton.setText(messages.getString("centrum.slavegroup.rename"));

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    add(renameButton, gridBagConstraints);

    deleteButton.setText(messages.getString("centrum.slavegroup.delete"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 5;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    add(deleteButton, gridBagConstraints);

但正如您在附带的屏幕截图中看到的那样,文本字段和添加按钮之间存在两个间隙。删除和重命名按钮之间也会出现这种差距。但是我不想要那些差距。我没有定义任何插图,我的文本字段都是weightx = 1;fill = java.awt.GridBagConstraints.HORIZONTAL;的组合框所以我希望它们尽可能地水平扩展而不会有间隙。但为什么会出现这些差距?

其次,您可以清楚地看到右侧的Jlist比左侧的Jlist宽。但是,我的两个JLists(实际上是包含JLists的ScrollPanes)都将gridwidthweightxfill参数设置为相同的值。所以我希望它们的大小相同。看起来我错过了一些东西。

enter image description here

下面添加了SSCCE:

import javax.swing.*;
import java.awt.*;

public class SSCCE {

private javax.swing.JButton addGroupButton;
private javax.swing.JTextField addGroupTextField;
private javax.swing.JButton addSlaveButton;
private javax.swing.JList allSlavesList;
private javax.swing.JButton deleteButton;
private javax.swing.JButton deleteSlaveButton;
private javax.swing.JComboBox groupComboBox;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JButton renameButton;
private javax.swing.JLabel groupNameLabel;
private javax.swing.JLabel allSlavesLabel;
private javax.swing.JLabel slavesInGroupLabel;
private javax.swing.JList slavesInGroupList;

public SSCCE() {

}

public JPanel createPanel(){
    JPanel panel = new JPanel();
    panel.setLayout(new java.awt.GridBagLayout());


    addGroupButton=new javax.swing.JButton();
    addGroupTextField=new javax.swing.JTextField();
    addSlaveButton=new javax.swing.JButton();
    allSlavesList=new javax.swing.JList();
    deleteButton=new javax.swing.JButton();
    deleteSlaveButton=new javax.swing.JButton();
    groupComboBox=new javax.swing.JComboBox();
    jScrollPane2=new javax.swing.JScrollPane();
    jScrollPane3=new javax.swing.JScrollPane();
    renameButton=new javax.swing.JButton();
    groupNameLabel=new javax.swing.JLabel();
    allSlavesLabel=new javax.swing.JLabel();
    slavesInGroupLabel=new javax.swing.JLabel();
    slavesInGroupList=new javax.swing.JList();

    allSlavesList.setModel(new DefaultListModel());
    slavesInGroupList.setModel(new DefaultListModel());



    GridBagConstraints gridBagConstraints;

    addGroupButton.setText("add");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    panel.add(addGroupButton, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 5;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    panel.add(addGroupTextField, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panel.add(groupNameLabel, gridBagConstraints);

    groupComboBox.setModel(new DefaultComboBoxModel(new Object[]{"one","two","three"}));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 5;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.weightx = 1;
    panel.add(groupComboBox, gridBagConstraints);



    allSlavesList.setVisibleRowCount(4);
    jScrollPane2.setViewportView(allSlavesList);
    jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);


    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 0.45;
    gridBagConstraints.weighty = 1.0;
    panel.add(jScrollPane2, gridBagConstraints);

    slavesInGroupList.setVisibleRowCount(4);
    jScrollPane3.setViewportView(slavesInGroupList);
    jScrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 0.45;
    gridBagConstraints.weighty = 1.0;
    panel.add(jScrollPane3, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    panel.add(allSlavesLabel, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    panel.add(slavesInGroupLabel, gridBagConstraints);

    //addSlaveButton.setIcon(GeneralObjects.getrightIcon());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.weightx = 0.1;
    gridBagConstraints.weighty = 1.0;
    panel.add(addSlaveButton, gridBagConstraints);

    //deleteSlaveButton.setIcon(GeneralObjects.getleftIcon());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.weightx = 0.1;
    gridBagConstraints.weighty = 1.0;
    panel.add(deleteSlaveButton, gridBagConstraints);

    renameButton.setText("rename");

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    panel.add(renameButton, gridBagConstraints);

    deleteButton.setText("delete");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 5;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    panel.add(deleteButton, gridBagConstraints);




    ((DefaultListModel)allSlavesList.getModel()).addElement("one");
    ((DefaultListModel)allSlavesList.getModel()).addElement("two");
    ((DefaultListModel)allSlavesList.getModel()).addElement("three");

    ((DefaultListModel)slavesInGroupList.getModel()).addElement("one");
    ((DefaultListModel)slavesInGroupList.getModel()).addElement("two");
    ((DefaultListModel)slavesInGroupList.getModel()).addElement("three");
    panel.setPreferredSize(new Dimension(600,300));


    return panel;

}

public static void main(String[] args){
    SSCCE a = new SSCCE();
    JOptionPane.showConfirmDialog(null, a.createPanel(), "centrum.manageGroups", JOptionPane.OK_CANCEL_OPTION);
}


}

1 个答案:

答案 0 :(得分:2)

您应该尝试使用更多的嵌套,因为您无法在平面布局中设置此类布局。

根据你的描述,我看到:

  • 包含3行的根面板(使用GridBagLayout进行设置,并为最后一行提供所有额外的垂直空间)
  • 第1行:您的标签,文字字段和按钮 - &gt;使用BorderLayout
  • 第2行:组合框和2个按钮 - &gt;使用GridBagLayout并为组合框提供所有额外空间
  • 第3行:2个标签,2个列表和2个按钮 - &gt;使用GridBagLayout进行设置

现在,GridBagLayout要理解的一点是,weightx/weighty不是它分配的宽度/高度的百分比,而是“额外”空间的分布情况。它将首先为每个组件提供其首选大小,然后,如果有任何额外空间,它将根据每个组件的重量分配该空间。

对于最后一行,每个“列”的宽度基于2个标签的首选大小(一个明显宽于另一个)和2个JList的首选大小(基于内容的JList)。

这是你的SSCCE的更新(虽然我不得不重做很多,因为代码有点乱:尝试按顺序做事,首先设置你的组件,然后按它们出现的顺序排列,阅读起来要简单得多。

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

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class SSCCE {

    private javax.swing.JButton addGroupButton;
    private javax.swing.JTextField addGroupTextField;
    private javax.swing.JButton addSlaveButton;
    private javax.swing.JList allSlavesList;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton deleteSlaveButton;
    private javax.swing.JComboBox groupComboBox;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton renameButton;
    private javax.swing.JLabel allSlavesLabel;
    private javax.swing.JLabel slavesInGroupLabel;
    private javax.swing.JList slavesInGroupList;
    private JLabel groupNameLabel;

    public SSCCE() {

    }

    public JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new java.awt.GridBagLayout());

        addGroupButton = new javax.swing.JButton();
        addGroupTextField = new javax.swing.JTextField();
        addSlaveButton = new javax.swing.JButton();
        allSlavesList = new javax.swing.JList();
        deleteButton = new javax.swing.JButton();
        deleteSlaveButton = new javax.swing.JButton();
        groupComboBox = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        renameButton = new javax.swing.JButton();
        allSlavesLabel = new javax.swing.JLabel("All slaves");
        slavesInGroupLabel = new javax.swing.JLabel("Devices in this group");
        groupNameLabel = new JLabel("Groupd name:");
        slavesInGroupList = new javax.swing.JList();
        groupComboBox.setModel(new DefaultComboBoxModel(new Object[] { "one", "two", "three" }));
        addGroupButton.setText("add");
        allSlavesList.setModel(new DefaultListModel());
        slavesInGroupList.setModel(new DefaultListModel());
        renameButton.setText("rename");
        deleteButton.setText("delete");
        addSlaveButton.setText("->");
        deleteSlaveButton.setText("<-");
        allSlavesList.setVisibleRowCount(4);
        jScrollPane2.setViewportView(allSlavesList);
        jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        ((DefaultListModel) allSlavesList.getModel()).addElement("one");
        ((DefaultListModel) allSlavesList.getModel()).addElement("two");
        ((DefaultListModel) allSlavesList.getModel()).addElement("three");

        ((DefaultListModel) slavesInGroupList.getModel()).addElement("one");
        ((DefaultListModel) slavesInGroupList.getModel()).addElement("two");
        ((DefaultListModel) slavesInGroupList.getModel()).addElement("three");
        slavesInGroupList.setVisibleRowCount(4);
        jScrollPane3.setViewportView(slavesInGroupList);
        jScrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        // Let's split your display in 3 rows
        panel = new JPanel(new GridBagLayout());
        JPanel row1 = new JPanel(new BorderLayout(3, 3));
        JPanel row2 = new JPanel(new GridBagLayout());
        JPanel row3 = new JPanel(new GridBagLayout());

        // Use GBL to make three rows and give all extra vertical space to the last one
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(3, 3, 3, 3);
        panel.add(row1, gbc);
        panel.add(row2, gbc);
        // Gives all extra v-space to last row
        gbc.weighty = 1.0;
        panel.add(row3, gbc);

        GridBagConstraints gridBagConstraints;

        // Set up row 1
        row1.add(groupNameLabel, BorderLayout.WEST);
        row1.add(addGroupButton, BorderLayout.EAST);
        row1.add(addGroupTextField);// In the middle so that it takes all the width and it won't stretch vertically because row1 has weighty
                                    // set to 0.0

        // Set up row 2
        // We give all extra space to the combo box
        gbc = new java.awt.GridBagConstraints();
        gbc.insets = new Insets(3, 3, 3, 3);
        gbc.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;
        row2.add(groupComboBox, gbc);
        gbc.weightx = 0;
        row2.add(deleteButton, gbc);
        row2.add(renameButton, gbc);

        // Set up row3
        gbc = new java.awt.GridBagConstraints();
        gbc.insets = new Insets(3, 3, 3, 3);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        row3.add(allSlavesLabel, gbc);
        gbc.gridx = 2;
        row3.add(slavesInGroupLabel, gbc);

        gbc.weighty = 1.0;
        gbc.weightx = 1.0;
        gbc.gridheight = 2;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;

        row3.add(jScrollPane2, gbc);
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridheight = 1;
        gbc.gridx = 1;
        gbc.weightx = 0;
        row3.add(addSlaveButton, gbc);
        gbc.gridy = 2;
        row3.add(deleteSlaveButton, gbc);

        gbc.gridheight = 2;
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;

        row3.add(jScrollPane3, gbc);

        return panel;

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SSCCE a = new SSCCE();
                JDialog dialog = new JDialog();
                dialog.add(a.createPanel());
                dialog.pack();
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
                // JOptionPane.showConfirmDialog(null, a.createPanel(), "centrum.manageGroups", JOptionPane.OK_CANCEL_OPTION);
            }
        });
    }

}