如何使用GridBagLayout设置最小尺寸? [英] How to set minimum sizes with GridBagLayout?

查看:111
本文介绍了如何使用GridBagLayout设置最小尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple GUI using GridBagLayout with a button panel at the top, and a custom resizable component taking up the rest of the space, as shown in the following image:

自定义组件(红色组件)的首选大小为(400,300),最小大小为(40,30),并且很高兴将其调整为大于该大小的任何大小. 但是,我希望我的框架遵守按钮面板的最小尺寸,并且不允许调整框架的大小,以使任何按钮都不能完全显示在屏幕上.这不是当前的行为,因为我可以将其调整为远远超出以下所示的边界:

The custom component (the red one) has a preferred size of (400, 300) and a minimum size of (40, 30) and is happy to be resized to any size greater than that. However, I would like my frame to respect the minimum size of the button panel and not allow the frame to be resized such that any of the buttons are not fully shown on the screen. This is not currently the behaviour as I can resize it far past those boundaries as seen here:

我当前的代码如下:

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)上进行了测试,并观察到相同的情况.

I have tested this on both Mac OS X 10.8 (Java 6) and Ubuntu 3.2.8 (Java 6) and observed the same thing.

如何防止调整框架大小以覆盖任何按钮?更一般而言,如何获得GridBagLayout以实际遵守组件的最小尺寸?当我打印出框架的最小尺寸时,我得到的正是(291, 81),但是当我调整框架的尺寸时,它超出了这个范围.

How can I prevent the frame from being resized to cover any of the buttons? More generally, how can I get GridBagLayout to actually respect the minimum sizes of my components? When I print out the minimum size of my frame I get (291, 81), which is exactly what I want, but when I resize the frame, it goes beyond that.

注意::我查看了

Note: I have looked at this related question but it doesn't appear to answer my question.

推荐答案

我不确定为什么,但是如果我使用...

I'm not sure why, but if I use...

frame.setMinimumSize(frame.getMinimumSize());

创建UI(显然)之后,它就可以工作.

After the UI has being created (obviously), it works.

我认为"这与WindowsetMinimumSize size方法中的((WindowPeer)peer).updateMinimumSize();有关...

I "think" it has something to do with ((WindowPeer)peer).updateMinimumSize(); in the setMinimumSize size method of Window...

这篇关于如何使用GridBagLayout设置最小尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆