如何使用BoxLayout在容器内设置组件大小

时间:2013-08-23 14:44:02

标签: java swing layout layout-manager boxlayout

面对使用BoxLayout的问题

在我的示例中,我尝试降低文本字段的高度并更改按钮的宽度(如图中的绿色标记所示)。我知道setPrefferedSize()和setMaximumSize()技术,但它没有按预期工作。行添加(Box.createHorizo​​ntalGlue())也没有帮助。

感谢您的任何想法

public class Testy extends JPanel {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                constructGUI();
            }
        });
    }

    private static void constructGUI() {
        JFrame frame = new JFrame("Testy");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.DARK_GRAY);
        centerPanel.setPreferredSize(new Dimension(100, 400));
        frame.add(centerPanel, BorderLayout.CENTER);

        Testy eastPanel = new Testy();
        frame.add(eastPanel, BorderLayout.EAST);

        frame.pack();
        frame.setVisible(true);
    }

    public Testy() {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        JButton button = new JButton("Button ...... 1");
        //button.setPreferredSize(...);
        //button.setMaximumSize(...);
        add(button);

        button = new JButton("Button 2");
        //button.setPreferredSize(...);
        //button.setMaximumSize(...);
        add(button);

        button = new JButton("Button ........... 3");
        //button.setPreferredSize(...);
        //button.setMaximumSize(...);
        add(button);

        JLabel label = new JLabel("Label");
        //label.setPreferredSize(...);
        //label.setMaximumSize(...);
        add(label);

        JTextField textField = new JTextField();
        //textField.setPreferredSize(...);
        //textField.setMaximumSize(...);
        add(textField);

        button = new JButton("Button 4");
        //button.setPreferredSize(...);
        //button.setMaximumSize(...);
        add(button);

        //add(Box.createHorizontalGlue());
    }
}

picture

3 个答案:

答案 0 :(得分:24)

首先,您必须意识到Java Swing中的组件位置和大小取决于布局管理器(如果设置了布局管理器),而不是组件本身。该组件向经理请求大小。

对于这种情况,我会使用不同的布局 - GridLayout和BorderLayout的组合足够且非常简单和直接。但如果想使用BoxLayout,那么......

  1. 文档说:

      

    BoxLayout值得关注       组件要求的最小,首选和最大尺寸。       在对布局进行微调时,可能需要对其进行调整       大小。 ...例如,按钮的最大尺寸通常是       与其首选尺寸相同。如果您希望将按钮绘制得更宽       当有额外的空间时,你需要改变它       最大尺寸。

  2. 然后设置组件的最大尺寸:c.setMaximumSize(new Dimension(Integer.MAX_VALUE, c.getMinimumSize().height));c表示buttonlabeltextField在您的示例中)

    < / LI>

    编辑1:

    以下是工作源代码:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;
    
    public class Testy extends JPanel {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    constructGUI();
                }
            });
        }
    
        private static void constructGUI() {
            JFrame frame = new JFrame("Testy");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            JPanel centerPanel = new JPanel();
            centerPanel.setBackground(Color.DARK_GRAY);
            centerPanel.setPreferredSize(new Dimension(100, 400));
            frame.add(centerPanel, BorderLayout.CENTER);
    
            Testy eastPanel = new Testy();
            frame.add(eastPanel, BorderLayout.EAST);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public Testy() {
            setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    
            JButton button = new JButton("Button ...... 1");
            //button.setPreferredSize(...);
            button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
            add(button);
    
            button = new JButton("Button 2");
            //button.setPreferredSize(...);
            button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
            add(button);
    
            button = new JButton("Button ........... 3");
            //button.setPreferredSize(...);
            button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
            add(button);
    
            JLabel label = new JLabel("Label");
            //label.setPreferredSize(...);
            label.setMaximumSize(new Dimension(Integer.MAX_VALUE, label.getMinimumSize().height));
            add(label);
    
            JTextField textField = new JTextField();
            //textField.setPreferredSize(...);
            textField.setMaximumSize(new Dimension(Integer.MAX_VALUE, textField.getMinimumSize().height));
            add(textField);
    
            button = new JButton("Button 4");
            //button.setPreferredSize(...);
            button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
            add(button);
    
            // add(Box.createVerticalGlue());
        }
    }
    

    Screenshot

    编辑2:

    如果您希望在右栏底部布置按钮4,请在add(Box.createVerticalGlue());add(textField);之间添加此行button = new JButton("Button 4");

答案 1 :(得分:4)

作为一种快速补救措施,您可以使用嵌套布局,在某种意义上,在右侧创建JPanel BorderLayout,在{{1}放置JPanel(say compPanel)CENTER位置有一个JPanel(say buttonPanel)。现在使用带有PAGE_END的新JPanel(say panel)并将所有组件放在其上,并将此GridLayout放在compPanel中。将centerPanel放在JButton(button4)内。

相反,

buttonPanel尊重给定BoxLayout的首选大小,通常根据JComponent持有或明确的内容计算,因此组件不倾向于与其他给定组件保持一致。

以下是工作示例:

JComponent

输出:

LAYOUT EXAMPLE

答案 2 :(得分:1)

这应该接近,根据您的绘制,只需要处理该组件 在JLabel下面(使用setPreferredSize()):

JPanel main = new JPanel(new GridLayout(1, 2));

JPanel left = new JPanel();
//left.setPreferredSize(some size);
JPanel right = new JPanel(new GridLayout(6, 1));
//right.setPreferredSize(some size);

right.add(new JButton("Button 1"));
//...
right.add(new JButton("Button 4"));

main.add(left);
main.add(right);