如何因为weightx = 1f而停止调整JTextField的大小?

时间:2013-11-01 16:52:24

标签: java swing gridbaglayout

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

现在的问题是所有组件的大小都调整到需要的宽度以覆盖jpanel的宽度。但是如果我改变他们的权重x或者我改变了填充属性,那么我就会因为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的宽度,您会看到文本字段也会调整大小。

你也可以告诉smth关于这个jpanel的边界。如您所见,标题边框太靠近最顶层的标签eventLabel。

非常感谢!

2 个答案:

答案 0 :(得分: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 size to Parent component:

DateFormatDemo dateFormatDemo = new DateFormatDemo();
dateFormatDemo.setMinimumSize(new Dimension(200, 0));

答案 1 :(得分:1)

  

如果您调整Jpanel的宽度,您将看到文本字段的大小调整   太

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

修改  GridBagLayout尊重由其布局的组件的最小和首选大小。尝试设置它们,它应该工作。