java - 如何填充有关布局管理器的剩余空间?

标签 java swing layout-manager

我最近一直在开发一个 Swing 应用程序。我遇到了布局管理器的问题。我似乎无法弄清楚如何使布局中的组件一直增长到其父容器的边缘。让我解释一下。

假设我在一行中有 8 个按钮。根据窗口大小将确定它们是否占用所有空间。 GBL 我找到了中心,所以左右都有空间。 BoxLayout 通常将空格放在右侧。这可能是由于它们的 anchor 或对齐方式。

我认为问题在于,当所有组件都具有相同设置时,布局会尝试为每个组件提供相同的空间。因此,那一点额外的空间不能平均分配给他们遗漏的每个组件。

我想知道是否有解决此问题的方法。就像空间太小一样,我希望有一种方法可以让最后一个组件吃掉它,或者在组件之间尽可能地划分它。

这是显示问题的示例代码。请注意,当您调整面板大小时,您会获得额外的空间。

public class LeftoverExample {

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                LeftoverExample.createGUI();
            }
        });
    }

    public static void createGUI(){
        JFrame jF = new JFrame();
        jF.setSize(new Dimension(1333,500));
        jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create ContentPane
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

        GridBagLayout gBL = new GridBagLayout();
        gBL.columnWidths = new int[]{0};
        gBL.rowHeights = new int[]{50, 50, 50 , 50};
        contentPane.setLayout(gBL);

        //Initial Constraints
        GridBagConstraints gBC = new GridBagConstraints();
        gBC.fill = GridBagConstraints.BOTH;
        gBC.gridx = 0;
        gBC.gridy = 0;
        gBC.weightx = 1;
        gBC.weighty = 0;
        gBC.insets = new Insets(10, 0, 10, 0);

        //Add Examples to ContentPane
        contentPane.add(LeftoverExample.createGBL(false), gBC);
        gBC.gridy++;
        contentPane.add(LeftoverExample.createGBL(true), gBC);
        gBC.gridy++;
        contentPane.add(LeftoverExample.createBoxLayout(false), gBC);
        gBC.gridy++;
        contentPane.add(LeftoverExample.createBoxLayout(true), gBC);

        //Final
        jF.setContentPane(contentPane);
        jF.setVisible(true);
    }

    private static JComponent createGBL(boolean addButtons){
        //GBL Example
        JLabel gBLJLabel = new JLabel("GridBagLayout");
        gBLJLabel.setVerticalAlignment(SwingConstants.CENTER);
        gBLJLabel.setLayout(new BoxLayout(gBLJLabel, BoxLayout.X_AXIS));
        gBLJLabel.setBackground(Color.CYAN);
        gBLJLabel.setOpaque(true);
        gBLJLabel.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
        GridBagLayout gBL = new GridBagLayout();
        gBL.columnWidths = new int[]{0};
        gBL.rowHeights = new int[]{50};
        gBLJLabel.setLayout(gBL);

        //Initial Constraints
        GridBagConstraints gBC = new GridBagConstraints();
        gBC.fill = GridBagConstraints.BOTH;
        gBC.gridx = 0;
        gBC.gridy = 0;
        gBC.weightx = 1;
        gBC.weighty = 0;
        gBC.insets = new Insets(0, 0, 0, 0);

        //Add to GBL Panel
        if(addButtons){
            LeftoverExample.addButtons(gBLJLabel, gBC);
            LeftoverExample.addButtons(gBLJLabel, gBC);
            LeftoverExample.addButtons(gBLJLabel, gBC);
            LeftoverExample.addButtons(gBLJLabel, gBC);
            LeftoverExample.addButtons(gBLJLabel, gBC);
            LeftoverExample.addButtons(gBLJLabel, gBC);
        }
        return gBLJLabel;
    }

    private static JComponent createBoxLayout(boolean addButtons){
        //BoxLayout Example
        JLabel boxLayoutJL = new JLabel("BOX_LAYOUT");
        boxLayoutJL.setVerticalAlignment(SwingConstants.CENTER);
        boxLayoutJL.setLayout(new BoxLayout(boxLayoutJL, BoxLayout.X_AXIS));
        boxLayoutJL.setBackground(Color.GREEN);
        boxLayoutJL.setOpaque(true);
        boxLayoutJL.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));

        //Add to BoxLayout Panel
        if(addButtons){
            LeftoverExample.addButtons(boxLayoutJL);
            LeftoverExample.addButtons(boxLayoutJL);
            LeftoverExample.addButtons(boxLayoutJL);
        }
        return boxLayoutJL;
    }

    private static JButton createButton(Color c){
        JButton jB = new JButton();
        jB.setBackground(c);
        jB.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
        return jB;
    }

    private static void addButtons(JComponent jC, GridBagConstraints gBC){
        //Create Buttons
        Color[] colorA = {Color.RED, Color.BLUE, Color.BLACK, Color.GREEN};
        for(Color c : colorA){
            jC.add(LeftoverExample.createButton(c), gBC);
            gBC.gridx++;
        }
    }

    private static void addButtons(JComponent jC){
        //Create Buttons
        Color[] colorA = {Color.BLUE, Color.BLACK, Color.GREEN, Color.RED};
        for(Color c : colorA){
            jC.add(LeftoverExample.createButton(c));
        }
    }
}

查看每个西侧和东侧如何留有一些空间被父级(在本例中为 JLabel)占据但按钮没有。我希望按钮也能占据那个空间。

图片展示示例:

最佳答案

问题是由 Swing 使用整数值而不是 double 值引起的。

考虑到这个事实,容器宽度除以 Component 的数量的余数 r(在您的例子中是 JButton objects) 它包含的对象可用于将第一个 r Component 对象的大小增加 1 以进行补偿。显然,这意味着第一个 r Component 对象将比其他 Components 大 +1,但这应该不会引起注意。

为了更新 Component 对象的宽度,我们需要访问那里的容器(例如 JPanel)和所有 Component我们希望更新的对象。在我的示例中,我将为此目的使用一个 List

这里有一个方法可以相应地调整 Component 对象的大小。

private static void fixComponentWidths(Component container,
    List<? extends Component> componentList, int componentHeight) {

    if (!componentList.isEmpty()) { // Avoid possible division by zero

        // get the desired component width for the container using integer division
        int baseComponentWidth = container.getWidth() / componentList.size();

        // find the remainder
        int remainder = container.getWidth() % componentList.size();

        // update all the components
        for (int i = 0; i < componentList.size(); i++) {

            // the component width will be the base width plus 1 iff i < remainder
            int componentWidth = baseComponentWidth;
            if (i < remainder) {
                componentWidth++;
            }

            // update the maximum size
            componentList.get(i).setMaximumSize(new Dimension(componentWidth, componentHeight));
        }

        // be sure to revalidate otherwise it may not work
        container.revalidate();
    }
}

为了使其在调整大小时起作用,必须为我们的容器实现一个ComponentListener。这可以是 JFrame 或只是一个 JPanel(按照我的示例)。请注意,只有 componentResized(ComponentEvent) 方法需要为此任务实现。

buttonContainer.addComponentListener(new ComponentListener() {
    @Override
    public void componentResized(ComponentEvent ce) { // just implementing this
        fixComponentWidths(buttonContainer, buttons, BUTTON_HEIGHT);
        // where buttonContainer is a JPanel,
        // buttons is a List of JButtons
        // BUTTON_HEIGHT, well the height of the button!
    }

    @Override
    public void componentMoved(ComponentEvent ce) { // not needed
    }

    @Override
    public void componentShown(ComponentEvent ce) { // not needed
    }

    @Override
    public void componentHidden(ComponentEvent ce) { // not needed
    }
});

这就是所有需要的。但为了完整起见,这里有一个基于作者问题的小示例,后面是 JPanel 的子类,它使用 BoxLayout 可用于为 解决此行为>BoxLayout.X_AXISBoxLayout.Y_AXIS

完整示例

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FillExample extends JFrame {

    private static final int FRAMEL_DEFAULT_WIDTH = 700;
    private static final int FRAME_DEFAULT_HEIGHT = 400;

    private static final int BUTTON_HEIGHT = Integer.MAX_VALUE;

    private final List<JButton> buttons;

    public FillExample() {
        buttons = new ArrayList<>();
    }

    public void createAndShow() {
        setTitle("Fill Example");
        setSize(FRAMEL_DEFAULT_WIDTH, FRAME_DEFAULT_HEIGHT);

        final JPanel buttonContainer = new JPanel();
        buttonContainer.setLayout(new BoxLayout(buttonContainer, BoxLayout.X_AXIS));

        for (int i = 0; i < 3; i++) {
            addButtons(buttonContainer);
        }

        getContentPane().add(buttonContainer);

        buttonContainer.addComponentListener(new ComponentListener() {
            @Override
            public void componentResized(ComponentEvent ce) {
                fixComponentWidths(buttonContainer, buttons, BUTTON_HEIGHT);
            }

            @Override
            public void componentMoved(ComponentEvent ce) {
            }

            @Override
            public void componentShown(ComponentEvent ce) {
            }

            @Override
            public void componentHidden(ComponentEvent ce) {
            }
        });

        setVisible(true);
    }

    private static void fixComponentWidths(Component container, List<? extends Component> componentList, int componentHeight) {
        if (!componentList.isEmpty()) {
            int baseComponentWidth = container.getWidth() / componentList.size();
            int remainder = container.getWidth() % componentList.size();
            for (int i = 0; i < componentList.size(); i++) {
                int componentWidth = baseComponentWidth;
                if (i < remainder) {
                    componentWidth++;
                }
                componentList.get(i).setMaximumSize(new Dimension(componentWidth, componentHeight));
            }
            container.revalidate();
        }
    }

    private void addButtons(JComponent component) {
        Color[] colorA = {Color.RED, Color.BLUE, Color.BLACK, Color.GREEN};
        for (Color c : colorA) {
            JButton button = createButton(c);
            buttons.add(button);
            component.add(button);
        }
    }

    private static JButton createButton(Color color) {
        JButton button = new JButton();
        button.setBackground(color);
        button.setMaximumSize(new Dimension(Integer.MAX_VALUE, BUTTON_HEIGHT));
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FillExample().createAndShow();
            }
        });
    }

}

填充框布局面板

这个小类可用于快速解决 BoxLayout.X_AXISBoxLayout.Y_AXIS 的间距问题。请注意,创建 BoxLayoutLayoutManager 的类不能更改。

可以使用 add(Component comp)add(Component comp, int index)

Component 对象添加到面板。请注意,并不是所有的 add 方法都会被覆盖,应谨慎使用该类。

import java.awt.Component;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

public class FillBoxLayoutPanel extends JPanel {

    public static final int X_AXIS = BoxLayout.X_AXIS;
    public static final int Y_AXIS = BoxLayout.Y_AXIS;

    private final List<Component> components;

    private final int direction;
    private boolean layoutSet;

    public FillBoxLayoutPanel(int direction) {
        components = new ArrayList<>();
        this.direction = direction;
        setLayout(new BoxLayout(this, direction));
        layoutSet = true;
        addComponentListener(new ComponentListener() {
            @Override
            public void componentResized(ComponentEvent ce) {
                adjustComponents();
            }

            @Override
            public void componentMoved(ComponentEvent ce) {
            }

            @Override
            public void componentShown(ComponentEvent ce) {
            }

            @Override
            public void componentHidden(ComponentEvent ce) {
            }
        });
    }

    @Override
    public void setLayout(LayoutManager mgr) {
        if (layoutSet) {
            throw new UnsupportedOperationException("FillPanel's layout manager cannot be changed.");
        } else {
            super.setLayout(mgr);
        }
    }

    @Override
    public Component add(Component comp) {
        comp = super.add(comp);
        components.add(comp);
        return comp;
    }

    @Override
    public Component add(Component comp, int i) {
        comp = super.add(comp, i);
        components.add(i, comp);
        return comp;
    }

    private void adjustComponents() {            
        if (!components.isEmpty()) {
            int size = direction == X_AXIS ? getWidth() : getHeight();
            int baseComponentSize = size / components.size();
            int remainder = size % components.size();

            for (int i = 0; i < components.size(); i++) {
                int componentSize = baseComponentSize;
                if (i < remainder) {
                    componentSize++;
                }
                Dimension dimension;
                if (direction == X_AXIS) {
                    dimension = new Dimension(componentSize, components.get(i).getHeight());
                } else {                    
                    dimension = new Dimension(components.get(i).getWidth(), componentSize);
                }
                components.get(i).setMaximumSize(dimension);
            }
            revalidate();
        }
    }

}

关于java - 如何填充有关布局管理器的剩余空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46863055/

相关文章:

java - 当 JTextField 为 'filled' 达到特定字符限制时执行函数

java - 如何使用 Java 打印文档?

java - 如何使 Jpanel 的所有区域可拖动,包括其中的 JSVGCanvas

java - 如何使我的应用程序在选择时以及用户选择文件夹时读取我的文件,然后使应用程序读取其中的所有文件?

java - 使用 JAXB 创建不可变对象(immutable对象)

java - 根据主体tomcat重定向请求

java - 从按钮启动 JFrame

java - GridBagConstraints 网格宽度意外影响组件

java - 按钮和窗口之间的空间

java - 如何动态更改 JFrame 内的 JPanel?