0

当我缩小 JPanel 的高度时,我遇到了 GridBagLayout 的问题。一旦我解决了这个问题,我就在填充同一个 jpanel 的宽度时遇到了问题。

现在的问题是所有组件的大小都被调整为覆盖 jpanel 宽度所需的宽度。但是,如果我改变他们的 weightx 或者我改变填充属性,那么我会遇到 jpanel 高度缩小的新问题。

请复制粘贴此类以查看:

public class DateFormatDemo extends JFrame
{
    JLabel eventLabel = new JLabel("Label : ", SwingConstants.LEFT);
    JTextField eventLabelField = new JTextField("myFirstEvent", 30);
    JLabel timestampLabel = new JLabel("Time stamp : ");
    JLabel timestammpToLabel = new JLabel(" to ");
    JTextField timestampStartField = new JTextField("2011-11-30 12:30:45", 15);
    JTextField timestampEndField = new JTextField("2011-10-12 07:22:50", 15);
    JLabel durationMagnitudeLabel = new JLabel("Duration Magnitude : ");
    JTextField durationMagnitudeMinField = new JTextField("2011-10-12 09:12:40", 15);
    JLabel durationMagnitudeToLabel = new JLabel(" to ");
    JTextField durationMagnitudeMaxField = new JTextField("2011-10-12 11:13:34", 15);
    JButton searchButton = new JButton("search");

    public DateFormatDemo() {
        JPanel searchPanel = new JPanel(new GridBagLayout()) {
            Dimension minSize = new Dimension(200, 0);
            Border border = BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Search query"),
                    BorderFactory.createEmptyBorder(20, 5, 10, 5));

            @Override
            public Dimension getMinimumSize() {
                return minSize;
            }

            @Override
            public Border getBorder() {
                return border;
            }
        };

        GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.LINE_START,
                GridBagConstraints.BOTH, new Insets(5, 2, 5, 3), 0, 0);
        searchPanel.add(eventLabel, updateGridBagConstraints(gbc, 0, 0, 1, 1, 0f, 0f));
        searchPanel.add(eventLabelField, updateGridBagConstraints(gbc, 1, 0, 3, 1, 1f, 0f));
        searchPanel.add(timestampLabel, updateGridBagConstraints(gbc, 0, 1, 1, 1, 0f, 0f));
        searchPanel.add(timestampStartField, updateGridBagConstraints(gbc, 1, 1, 1, 1, 1f, 0f));
        searchPanel.add(timestammpToLabel, updateGridBagConstraints(gbc, 2, 1, 1, 1, 0f, 0f));
        searchPanel.add(timestampEndField, updateGridBagConstraints(gbc, 3, 1, 1, 1, 1f, 0f));
        searchPanel.add(durationMagnitudeLabel, updateGridBagConstraints(gbc, 0, 2, 1, 1, 0f, 0f));
        searchPanel.add(durationMagnitudeMinField, updateGridBagConstraints(gbc, 1, 2, 1, 1, 1f, 0f));
        searchPanel.add(durationMagnitudeToLabel, updateGridBagConstraints(gbc, 2, 2, 1, 1, 0f, 0f));
        searchPanel.add(durationMagnitudeMaxField, updateGridBagConstraints(gbc, 3, 2, 1, 1, 1f, 0f));
        gbc = updateGridBagConstraints(gbc, 0, 4, 3, 1, 0f, 0f);
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.ipadx = 5;
        gbc.ipady = 5;
        gbc.insets = new Insets(15, 0, 0, 0);
        searchPanel.add(searchButton, gbc);

        super.getContentPane().add(searchPanel);
        super.pack();
        super.setVisible(true);
    }

    private GridBagConstraints updateGridBagConstraints(GridBagConstraints gbc, int gridx, int gridy, int gridwidth,
            int gridheight, float weightx, float weighty) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        return gbc;
    }

    public static void main(String[] args) throws ParseException {
        new DateFormatDemo();
    }
}

如果您调整 Jpanel 的宽度,您会看到文本字段也被调整了大小。

您也可以就这个 jpanel 的边框提出建议吗?如您所见,标题边框与最上面的标签 eventLabel 太接近。

非常感谢!

4

2 回答 2

1

我不建议你这样做:

JPanel searchPanel = new JPanel(new GridBagLayout()) {
        Dimension minSize = new Dimension(200, 0);
        Border border = BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Search query"),
                BorderFactory.createEmptyBorder(20, 5, 10, 5));

        @Override
        public Dimension getMinimumSize() {
            return minSize;
        }

        @Override
        public Border getBorder() {
            return border;
        }
    };

为此,请按以下方式进行:

JPanel searchPanel = new JPanel(new GridBagLayout());
Border border = BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Search query"),
            BorderFactory.createEmptyBorder(20, 5, 10, 5));
searchPanel.setBorder(border);

它解决了边界问题。

setMinimum 大小到父组件:

DateFormatDemo dateFormatDemo = new DateFormatDemo();
dateFormatDemo.setMinimumSize(new Dimension(200, 0));
于 2013-11-01T17:09:17.153 回答
1

如果您调整 Jpanel 的宽度,您会看到文本字段也被调整了大小。

网格包布局管理器将列的权重计算为列weightx中所有组件的最大值。如果生成的布局在水平方向上小于它需要填充的区域,则额外的空间将按其权重按比例分配到每一列。权重为 的列不zero接收额外空间。因此,您不想调整其宽度的一组wieghtx文本字段。zero

编辑: GridBagLayout尊重由它布置的组件的最小和首选大小。尝试设置它们,它应该可以工作。

于 2013-11-01T17:21:24.297 回答