如何使用GridBagLayout设置最小尺寸?

时间:2013-10-22 01:20:46

标签: java swing layout gridbaglayout minimum-size

我有一个使用GridBagLayout的简单GUI,顶部有一个按钮面板,一个自定义可调整大小的组件占用了剩下的空间,如下图所示:

The GUI at start-up.

自定义组件(红色组件)的首选大小为(400,300),最小大小为(40,30),并且很乐意将其调整为大于此值的任何大小。 但是,我希望我的框架能够尊重按钮面板的最小尺寸,并且不允许调整框架的大小,以便任何按钮都不会完全显示在屏幕上。这当前不是行为,因为我可以远远超过这些边界调整它:

The GUI resized ridiculously small.

我目前的代码如下:

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

public class Example {
    public static void main(String[] args) {
        // Setup JFrame and GridBagLayout.
        JFrame frame = new JFrame("Example");
        Container contentPane = frame.getContentPane();
        GridBagLayout layout = new GridBagLayout();
        contentPane.setLayout(layout);
        layout.rowWeights = new double[] {0.0, 1.0};
        layout.columnWeights = new double[] {1.0};
        GridBagConstraints cons = new GridBagConstraints();

        // Add button panel with a BoxLayout.
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.add(new JButton("Button 1"));
        panel.add(new JButton("Button 2"));
        panel.add(new JButton("Button 3"));
        cons.anchor = GridBagConstraints.NORTHWEST;
        cons.gridx = 0;
        cons.gridy = 0;
        layout.setConstraints(panel, cons);
        contentPane.add(panel);

        // Add custom component, resizable.
        JComponent custom = new JComponent() {
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }

            public Dimension getMinimumSize() {
                return new Dimension(40, 30);
            }

            public void paintComponent(Graphics g) {
                g.setColor(Color.RED);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        cons.gridx = 0;
        cons.gridy = 1;
        cons.fill = GridBagConstraints.BOTH;
        layout.setConstraints(custom, cons);
        contentPane.add(custom);

        // Pack and show frame.
        frame.pack();
        frame.setVisible(true);
    }
}

我已经在Mac OS X 10.8(Java 6)和Ubuntu 3.2.8(Java 6)上测试了这一点,并观察到同样的事情。

如何防止框架调整大小以覆盖任何按钮?更一般地说,如何让GridBagLayout真正尊重我的组件的最小尺寸?当我打印出框架的最小尺寸时,我得到了(291, 81),这正是我想要的,但是当我调整框架大小时,它就超出了它。

注意:我看过this related question,但似乎没有回答我的问题。

3 个答案:

答案 0 :(得分:3)

我不知道为什么,但如果我使用......

frame.setMinimumSize(frame.getMinimumSize());

在创建UI之后(很明显),它可以正常工作。

我认为它与((WindowPeer)peer).updateMinimumSize();的{​​{1}}大小方法中的setMinimumSize有关...

答案 1 :(得分:2)

如果您的框架设置了最小尺寸,那么您可以向框架添加ComponentListener并实施componentResized方法。

就像这个例子一样

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Test {
    public static void main(String[] args) 
    {
        final JFrame frame = new JFrame("My Frame");
        frame.setMinimumSize(new Dimension(200,200));
        frame.addComponentListener( new ComponentAdapter()
        {
            public void componentResized(ComponentEvent evt)
            {
                Dimension size = frame.getSize();
                Dimension min = frame.getMinimumSize();
                if (size.getWidth() < min.getWidth())
                {
                    frame.setSize((int)min.getWidth() ,(int) size.getHeight());
                }
                if (size.getHeight() < min.getHeight())
                {
                    frame.setSize((int)size.getWidth() ,(int) min.getHeight());
                }
            }
        });
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

答案 2 :(得分:-1)

我常常遇到GridBagConstraints默认设置的问题:我很久以前就养成了设置它们(weightx,gridwidth等)的习惯。这通常会让GridBagLayout尊重我的最小尺寸。如果有人知道我为了达到这个目的而凌驾于哪个默认值,我很好奇: - )