【问题标题】:set maximum size of JPanel inside BorderLayout.CENTER在 BorderLayout.CENTER 中设置 JPanel 的最大尺寸
【发布时间】:2011-10-14 04:58:19
【问题描述】:

我在 BorderLayout.CENTER 中有一个 JPanel

JPanel 有一个网格布局,我希望它以 CENTER 扩展其宽度,但高度必须停止在最大值并尽可能使用 preferredSize。

我有这个代码

JPanel wrapperCenterPanel = new JPanel(new FlowLayout());
wrapperCenterPanel.add(centerPanel);    
panel.add(wrapperCenterPanel, BorderLayout.CENTER);

centerPanel 是我的面板(使用 GridLayout),我用 FlowLayout 面板包装它,并将最后一个放在 CENTER 中。

现在尺寸是首选,但它是固定的!!如果需要,高度不会缩小,宽度也不会缩小。

我该怎么做?

【问题讨论】:

    标签: java swing


    【解决方案1】:

    尝试使用 BoxLayout 作为包装面板。 BoxLayout 尊重组件的最大/最小和首选大小。

    【讨论】:

    • 许多布局管理器忽略了最大尺寸。我不认为您提到的错误是布局管理器问题。
    【解决方案2】:

    我认为使用 BorderLayout 是不可能的,尤其是对于 BorderLayout.CENTER 区域,也没有副作用,如代码中屏幕上闪烁的 UFO

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ComponentEvent;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    public class CustomComponent extends JFrame {
    
        private static final long serialVersionUID = 1L;
    
        public CustomComponent() {
            setTitle("Custom Component Graphics2D");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public void display() {
            CustomComponents cc = new CustomComponents();
            cc.addComponentListener(new java.awt.event.ComponentAdapter() {
    
                @Override
                public void componentResized(ComponentEvent event) {
                    setSize(Math.min(getPreferredSize().width, getWidth()),
                            Math.min(getPreferredSize().height, getHeight()));
                }
            });
            add(cc, BorderLayout.CENTER);
    
            CustomComponents cc1 = new CustomComponents();
            add(cc1, BorderLayout.EAST);
    
            pack();
            // enforces the minimum size of both frame and component
            setMinimumSize(getSize());
            //setMaximumSize(getMaximumSize());
            setVisible(true);
    
        }
    
        public static void main(String[] args) {
            CustomComponent main = new CustomComponent();
            main.display();
        }
    }
    
    class CustomComponents extends JComponent {
    
        private static final long serialVersionUID = 1L;
    
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(100, 100);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 300);
        }
    
        @Override
        public Dimension getMaximumSize() {
            return new Dimension(800, 600);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            int margin = 10;
            Dimension dim = getSize();
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
        }
    }
    

    【讨论】:

      【解决方案3】:

      FLowLayout 布局管理器不会重新分配可用空间;它使用每个组件的首选大小(请参阅 Java API 中的FlowLayout 文档)。

      我个人会将您的包装面板的布局管理器更改为GridBagLayout,并将您的centerPanel 添加到其中,指定适当的GridBagConstraints 对象以根据需要处理空间分配。

      【讨论】:

        猜你喜欢
        • 2013-04-30
        • 2022-01-18
        • 2011-10-14
        • 2017-03-27
        • 2020-10-31
        • 1970-01-01
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        相关资源
        最近更新 更多