簡體   English   中英

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

[英]How to set minimum sizes with GridBagLayout?

我有一個簡單的GUI使用GridBagLayout ,頂部有一個按鈕面板,一個自定義可調整大小的組件占用了剩余的空間,如下圖所示:

啟動時的GUI。

自定義組件(紅色組件)的首選大小為(400,300),最小大小為(40,30),並且很樂意將其調整為大於此值的任何大小。 但是,我希望我的框架能夠尊重按鈕面板的最小尺寸,並且不允許調整框架的大小,以便任何按鈕都不會完全顯示在屏幕上。 目前這不是行為,因為我可以遠遠超過這些邊界調整它的大小,如下所示:

GUI的大小調整得非常小。

我目前的代碼如下:

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) ,這正是我想要的,但是當我調整框架尺寸時,它就超出了它。

注意:我已經查看了這個相關的問題,但它似乎沒有回答我的問題。

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

frame.setMinimumSize(frame.getMinimumSize());

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

我“認為”它與((WindowPeer)peer).updateMinimumSize(); WindowsetMinimumSize size方法中...

如果在框架上設置了最小大小,則可以向框架添加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);
    }
}

我常常遇到GridBagConstraints默認設置的問題:我很久以前就養成了設置它們(weightx,gridwidth等)的習慣。 這通常會讓GridBagLayout尊重我的最小尺寸。 如果有人知道我最重要的是哪個默認,我很好奇:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM