简体   繁体   English

两个JPanel之间的水平胶合不正常

[英]Horizontal glue between two JPanels not functioning as intended

I'm having an issue with a BoxLayout JPanel not properly displaying horizontal glue. 我遇到BoxLayout JPanel无法正确显示水平胶水的问题。 I believe that I've narrowed down the issue to there being no extra horizontal space for the glue to pull, as a rigid area creates space between each panel with no problems. 我相信我已经将问题范围缩小到没有多余的水平空间可以拉胶,因为刚性区域会在每个面板之间创建空间而没有问题。 With that being said, I can't seem to find what component or setting is causing this. 话虽如此,我似乎找不到导致此问题的组件或设置。

Here's the method that contains the code in question (I've surrounded the section of code where I add the horizontal glue with a multiline comment): 这是包含所讨论代码的方法(我在代码部分中用多行注释添加了水平粘连):

public void initialize() {
    scrollPanel.removeAll();

    for (Item item : getController().getCart().getItemList()) {

        //Container
        itemContainerPanel = new JPanel();
        itemContainerPanel.setBorder(new EmptyBorder(0, 10, 10, 0));
        itemContainerPanel.setLayout(new BoxLayout(itemContainerPanel, BoxLayout.Y_AXIS));

        //Content panel
        itemPanel = new JPanel();
        itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.X_AXIS));
        itemPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        //itemPanel.setBorder(new LineBorder(Color.BLACK));
        itemContainerPanel.add(itemPanel);

        //Details left
        detailsLeftPanel = new JPanel();
        detailsLeftPanel.setLayout(new BoxLayout(detailsLeftPanel, BoxLayout.Y_AXIS));
        detailsLeftPanel.setAlignmentY(Component.TOP_ALIGNMENT);

        titlePanel = new JPanel();
        FlowLayout titlePanelLayout = new FlowLayout(FlowLayout.LEFT);
        titlePanelLayout.setHgap(0);
        titlePanelLayout.setVgap(0);
        titlePanel.setLayout(titlePanelLayout);
        productNameLabel = new JLabel();
        productNameLabel.setAlignmentX(LEFT_ALIGNMENT);
        productNameLabel.setAlignmentY(TOP_ALIGNMENT);
        titlePanel.add(productNameLabel);
        detailsLeftPanel.add(titlePanel);

        datesBookedPanel = new JPanel();
        FlowLayout datesBookedPanelLayout = new FlowLayout(FlowLayout.LEFT);
        datesBookedPanelLayout.setHgap(0);
        datesBookedPanelLayout.setVgap(0);
        datesBookedPanel.setLayout(datesBookedPanelLayout);
        datesBookedLabel = new JLabel();
        datesBookedLabel.setAlignmentX(LEFT_ALIGNMENT);
        datesBookedPanel.setAlignmentY(TOP_ALIGNMENT);
        datesBookedPanel.add(datesBookedLabel);
        detailsLeftPanel.add(datesBookedPanel);

        //Details right
        detailsRightPanel = new JPanel();
        detailsRightPanel.setLayout(new BoxLayout(detailsRightPanel, BoxLayout.Y_AXIS));
        detailsRightPanel.setAlignmentY(Component.TOP_ALIGNMENT);

        removePanel = new JPanel();
        removePanel.setLayout(new BoxLayout(removePanel, BoxLayout.X_AXIS));
        detailsRightPanel.add(removePanel);

        cartItemPriceLabel = new JLabel();
        cartItemPriceLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
        removePanel.add(cartItemPriceLabel);

        removeBtn = new JButton("Remove");
        removeBtn.setAlignmentY(Component.CENTER_ALIGNMENT);
        removePanel.add(removeBtn);

        /* ITEM PANEL BUILD WITH HORIZONTAL GLUE */
        itemPanel.add(detailsLeftPanel);
        itemPanel.add(Box.createHorizontalGlue());
        itemPanel.add(detailsRightPanel);
    }

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks and have a nice day. 感谢,并有一个愉快的一天。

EDIT: 编辑:

I've included an image 我已包含一张图片

http://i.stack.imgur.com/B0wYs.png

that shows the output of the code section above. 显示上面代码部分的输出。 The red lines I've drawn on the image show where the horizontal glue is placed. 我在图像上绘制的红线显示了放置水平胶水的位置。 The glue should push the panel with the price tag and remove button to the right to fill the width of the window, but it doesn't. 胶水应推动带有价格标签的面板,并向右移去按钮以填充窗口的宽度,但事实并非如此。

The problem is that the "itemPanel" is being displayed at its preferred size so you will never see the "horizontal glue" 问题是“ itemPanel”以其首选大小显示,因此您将永远不会看到“水平胶”

You add you "itemPanel" to the "itemContainerPanel". 您将“ itemPanel”添加到“ itemContainerPanel”。 You should not need this second panel. 您不需要第二个面板。 In any case I don't see where you add the "itemContainerPanel" to its parent container. 无论如何,我看不到将“ itemContainerPanel”添加到其父容器的位置。

I would guess you should be adding the "itemPanel" directly to your "scrollPanel". 我猜您应该将“ itemPanel”直接添加到“ scrollPanel”中。 So the "scrollPanel" needs to use a layout that will allow its child components to expand horizontally to fill the entire space. 因此,“ scrollPanel”需要使用允许其子组件水平扩展以填充整个空间的布局。 So I would guess the "scrollPanel" should be using the vertical BoxLayout. 因此,我猜想“ scrollPanel”应该使用垂直的BoxLayout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM