当前位置: 首页 > 知识库问答 >
问题:

swing自定义布局-组件包装和高度计算

荆利
2023-03-14

我试图创建自定义布局,它允许我指定一个组件的百分比宽度,并根据该百分比宽度布局组件。下面是我最后得到的实现。

我遇到的问题是,最内部面板的一个计算宽度不足以在一行中容纳其所有组件,下面的实现将它们包装到下一行,但父级[层次结构中的所有容器]的高度固定为一些像素[在我的例子中我使用了40px],并且它不允许显示包装的组件。

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.awt.Rectangle;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


/**
 * @author Rakesh.A
 *
 */
public class Example extends JPanel {

public Example() {
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    for (int i = 0; i < 1; i++) {
        JPanel row = new JPanel();
        row.setLayout(new PercentageWidthLayout(5, 5));

        JPanel column1 = new JPanel();
        column1.setOpaque(true);
        column1.setBackground(Color.white);

        JPanel column2 = createColumn2();

        row.add(column1, new MyConstraints(15, false)); // uses 15% of the available size
        row.add(column2, new MyConstraints(50, false, true, true)); // uses 50% of the available size and wraps its contents
        row.add(new JPanel(), new MyConstraints(25, false)); // uses 25% of the available size
        add(row);
    }
}

private JPanel createColumn2() {
    JPanel column = new JPanel();
    column.setOpaque(true);
    column.setBackground(Color.green);

    column.setLayout(new PercentageWidthLayout(3, 3, 35));
    // total percentage is 100% for all the below components
    column.add(new MyComponent(30, 28), new MyConstraints(20, true, false, true));
    column.add(new MyComponent(30, 28), new MyConstraints(10, true, false, true));
    column.add(new MyComponent(30, 28), new MyConstraints(20, true, false, true));
    column.add(new MyComponent(30, 28), new MyConstraints(20, true, false, true));
    column.add(new MyComponent(30, 28), new MyConstraints(10, true, false, true));
    column.add(new MyComponent(30, 28), new MyConstraints(10, true, false, true));
    column.add(new MyComponent(30, 28), new MyConstraints(10, true, false, true));
    return column;
}

public static void main(final String[] args) {
    JDialog dialog = new JDialog();
    dialog.setSize(500, 150);
    Example content = new Example();
    JScrollPane scrl = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    dialog.getContentPane().add(scrl);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setVisible(true);
}

public static class MyComponent extends JPanel {
    private Dimension minSize;
    public MyComponent(final int minWidth, final int minHeight) {
        minSize = new Dimension(minWidth, minHeight);

        setOpaque(true);
        setBackground(Color.yellow);

        add(new JLabel("Block"));
    }

    @Override
    public Dimension getPreferredSize() {
        return minSize;
    }

    @Override
    public Dimension getMaximumSize() {
        return minSize;
    }
    @Override
    public Dimension getMinimumSize() {
        return minSize;
    }
}

public static class PercentageWidthLayout implements LayoutManager2 {
    private LinkedHashMap<Component, MyConstraints> components;

    private final int leftMargin;
    private final int topMargin;
    private final int rowHeight;

    // default size of the block
    public static final Dimension minimumSize = new Dimension(10, 40);
    public static final Dimension preferredSize = new Dimension(100, 40);

    // default left margin between components
    public static final int defaultLeftMargin = 5;
    // default bottom margin between components
    public static final int defaultTopMargin = 5;
    // default row height
    public static final int defaultRowHeight = 0;

    public PercentageWidthLayout() {
        this(defaultLeftMargin, defaultTopMargin);
    }

    public PercentageWidthLayout(final int leftMargin, final int topMargin) {
        this(leftMargin, topMargin, defaultRowHeight);
    }

    public PercentageWidthLayout(final int leftMargin, final int topMargin, final int rowHeight) {
        this.leftMargin = leftMargin;
        this.topMargin = topMargin;
        this.rowHeight = rowHeight;

        components = new LinkedHashMap<Component, MyConstraints>();
    }

    @Override
    public Dimension preferredLayoutSize(final Container parent) {
        int maxX = 0;
        int maxY = 0;
        for (Entry<Component, MyConstraints> compEntry : components.entrySet()) {
            Rectangle bounds = compEntry.getKey().getBounds();
            maxX = Math.max(maxX, (int) bounds.getMaxX());
            maxY = Math.max(maxY, (int) bounds.getMaxY());
        }
        if (maxX == 0 || maxY == 0) {
            return preferredSize;
        }
        return new Dimension(maxX, maxY);
    }

    @Override
    public Dimension minimumLayoutSize(final Container parent) {
        return minimumSize;
    }

    @Override
    public Dimension maximumLayoutSize(final Container target) {
        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

    @Override
    public void layoutContainer(final Container parent) {
        synchronized (parent.getTreeLock()) {
            // validate total percentage
            validatePercentages();
            // calculate available width & height for the components
            Insets insets = parent.getInsets();
            // available width after removing border space
            int maxClientWidth = parent.getWidth() - insets.left - insets.right;
            // calculated available width for the components
            int clientWidth = maxClientWidth - (parent.getComponents().length * leftMargin);
            // calculated available height for the components
            int clientHeight = ((rowHeight > 0) ? rowHeight : preferredSize.height) - insets.top - insets.bottom - topMargin * 2;

            // layout the components
            int x = insets.left + leftMargin;
            int y = insets.top + topMargin;

            if (clientWidth > 0 && clientHeight > 0) {
                for (Component component : parent.getComponents()) {
                    // get the constraints to be applied
                    MyConstraints constraints = components.get(component);
                    // calculate component width according to the given percentage
                    int componentWidth = clientWidth * constraints.percentage / 100;

                    // calculate the preferred size of the component
                    int prefW = component.getPreferredSize().width;
                    if (constraints.usePreferredSize && componentWidth < prefW) {
                        // configured to use preferred size if calculated size is less than the
                        // preferred size
                        componentWidth = prefW;
                    }

                    // calculate the minimum size of the component
                    int minW = component.getMinimumSize().width;
                    if (constraints.useMinWidth && componentWidth < minW) {
                        // configured to use minimum width if calculated size is less than the
                        // minimum size
                        componentWidth = minW;
                    }
                    // check and wrap component to next row if needed
                    if (constraints.wrapComponents && x + componentWidth > parent.getWidth()) {
                        x = insets.left + leftMargin;
                        y += clientHeight + insets.top;
                        // update height of the parent component if it doesn fit
                        // if (parent.getHeight() < y + clientHeight) {
                        // parent.setSize(parent.getWidth(), parent.getHeight() + rowHeight);
                        // }
                    }
                    component.setBounds(x, y, componentWidth, clientHeight);
                    // update x coordinate
                    x += componentWidth + leftMargin;
                }
            }
        }
    }

    @Override
    public void addLayoutComponent(final String name, final Component comp) {
    }

    @Override
    public void removeLayoutComponent(final Component comp) {
        components.remove(comp); // remove component from map
    }

    @Override
    public void addLayoutComponent(final Component comp, final Object constraints) {
        if (constraints == null || !(constraints instanceof MyConstraints)) {
            throw new IllegalArgumentException("Invalid constraints object! - " + constraints);
        }
        MyConstraints myConstraints = (MyConstraints) constraints;
        if (myConstraints.percentage > 100) {
            throw new IllegalArgumentException("Invalid percentage value [" + myConstraints.percentage + "]!");
        }
        components.put(comp, myConstraints);
    }

    @Override
    public float getLayoutAlignmentX(final Container target) {
        return 0;
    }

    @Override
    public float getLayoutAlignmentY(final Container target) {
        return 0;
    }

    @Override
    public void invalidateLayout(final Container target) {
    }

    public int getLeftMargin() {
        return leftMargin;
    }

    public int getTopMargin() {
        return topMargin;
    }

    public int getRowHeight() {
        return rowHeight;
    }

    public static Integer calculatePercentage(final float value, final int total) {
        return new Integer((int) (value / total * 100));
    }

    private void validatePercentages() {
        int total = 0;
        for (Entry<Component, MyConstraints> compEntry : components.entrySet()) {
            total += compEntry.getValue().percentage;
        }
        if (total > 100) {
            throw new IllegalArgumentException("Total percentage [" + total + "] of the components in the layout is more than 100!");
        }
    }

}

/**
 * @author Rakesh.A
 *
 */
public static class MyConstraints {
    public final int percentage;
    public final boolean usePreferredSize, useMinWidth, wrapComponents;

    public MyConstraints(final int percentage, final boolean usePreferredSize) {
        this(percentage, usePreferredSize, false);
    }

    public MyConstraints(final int percentage, final boolean usePreferredSize, final boolean useMinWidth) {
        this(percentage, usePreferredSize, useMinWidth, false);
    }

    public MyConstraints(final int percentage, final boolean usePreferredSize, final boolean useMinWidth, final boolean wrapComponents) {
        this.percentage = percentage;
        this.usePreferredSize = usePreferredSize;
        this.useMinWidth = useMinWidth;
        this.wrapComponents = wrapComponents;
    }
}
}

共有1个答案

潘哲
2023-03-14

谢谢大家的投入。我选择了一个解决这个问题的方法,不是包装组件,而是添加了一个水平滚动条:)

 类似资料:
  • 我有一个Vaadin自定义布局,我想在其中添加一个JavaScript组件——jQuery bxSlider。 在我的page1.html我有列表: 在我看来,我正在尝试执行JS: 但什么也没发生——图像并没有包装到滑块中。如何在Vaadin应用程序中运行此JS?

  • 但是,在Log4JV2中,PatternLayout类被设置为“final”,整个体系结构也被更改。似乎不再有一种简单的方法来拦截/覆盖对PatternLayout对象的调用。我查看了Apache文档,但没有太多信息。 我检查了这个问题和这个问题,但都没有太多的帮助。 我意识到这是一个非常“一般”的问题,但是有没有人知道在Log4j V2中实现这一点的简单方法,或者对此有什么建议?

  • 有没有办法为不同的日志级别打印不同的布局?例如: 记录器。警告(“消息”);打印如下内容:2016-06-20 13:34:41245 INFO(main:)Message and for logger。信息(“消息2”);仅打印:消息2 有可能做到吗?定义一个布局以警告其他布局以获取信息 log4j.properties

  • 我正在使用PreferenceActivity设置我的应用程序。我想添加一个新的首选项,允许用户选择一个图标。对于这个任务,我想使用ListPreference,但我也想在列表中显示图标。 我尝试自定义List首选项以使用自定义布局,但问题是一旦我这样做了,列表项就不可单击(它确实显示了我的自定义布局并使用当前选择的默认值)。 我在不同的模拟器版本和银河S2上测试了它。当按下项目时,我可以看到一些

  • 问题内容: 我正在尝试使用Android中的XML文件定义GUI布局。据我所知,没有办法指定您的小部件应在XML文件中使用一种自定义字体(例如,您放置在资产/字体/中的字体),并且只能使用系统安装的字体。 我知道,在Java代码中,我可以使用唯一ID手动更改每个小部件的字体。另外,我可以遍历Java中的所有小部件以进行此更改,但这可能会很慢。 我还有什么其他选择?有没有更好的方法来制作具有自定义外