如何使用BoxLayout在容器内设置组件大小 [英] how to set component size inside container with BoxLayout

查看:408
本文介绍了如何使用BoxLayout在容器内设置组件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

面对使用BoxLayout的问题

Faced with the problem of using BoxLayout

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

In my example, I try to decrease the height of the text field and change the width of the buttons (as shown in green marker in the picture). I know about the techniques setPrefferedSize () and setMaximumSize (), but it did not work as it should. The line add(Box.createHorizontalGlue ()) also did not help.

感谢您的任何想法

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());
    }
}

推荐答案

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

First you have to realize that component position and size in Java Swing depends on Layout manager (if layout manager is set) not on the component itself. The component requests the manager for size.

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

For this case I would use different layout - combination of GridLayout and BorderLayout is enough and very simple and straightforward. But if want use BoxLayout, then...


  1. 文档说:

  1. Documentation says:


BoxLayout关注组件要求的最小,首选和最大尺寸

在调整布局时,您可能需要调整这些
大小。 ...例如,按钮的最大大小通常是
与其首选大小相同。如果你想在额外空间可用的情况下将按钮绘制得更宽
,那么你需要更改它的
最大尺寸。

BoxLayout pays attention to a component's requested minimum, preferred, and maximum sizes. While you are fine-tuning the layout, you might need to adjust these sizes. ... For example, a button's maximum size is generally the same as its preferred size. If you want the button to be drawn wider when additional space is available, then you need to change its maximum size.


  • 然后设置组件最大大小: c.setMaximumSize(new Dimension(Integer.MAX_VALUE,c.getMinimumSize()。height)); c 表示按钮标签 textField 在你的例子中)

  • Then set components maximum size: c.setMaximumSize(new Dimension(Integer.MAX_VALUE, c.getMinimumSize().height)); (c means button, label and textField in your example)

    编辑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());
        }
    }
    

    编辑2:

    如果你想在右栏的底部布置按钮4,请在 add(Box.createVerticalGlue()); > add(textField); 和 button = new JButton(Button 4);

    If you want laid out Button 4 at the bottom of right column add this line add(Box.createVerticalGlue()); between add(textField); and button = new JButton("Button 4");.

    这篇关于如何使用BoxLayout在容器内设置组件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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