简体   繁体   English

使用Java中的MigLayout将高度或宽度设置为100%时,子级会超出其父级的边界

[英]Child expands past the bounds of its parent when height or width set to 100% using MigLayout in Java

I'm trying to use MigLayout to set the height of certain elements to take up the full height of their parent's container using the expression "height 100%" however there is some kind of feedback loop in MigLayout that causes the height to jump to the maximum value when loaded. 我正在尝试使用MigLayout使用表达式“高度100%”来设置某些元素的高度,以占用其父容器的全部高度,但是MigLayout中存在某种反馈循环,导致高度跳至加载时的最大值。 This issue only happens when the MiGPanel is embedded into a JScrollPane, if I remove the lines of code dealing with the JScrollPane and add the MiGPanel to my JFrame then the program runs as intended. 仅当将MiGPanel嵌入到JScrollPane中时,才会发生此问题,如果我删除了处理JScrollPane的代码行并将MiGPanel添加到我的JFrame中,则程序将按预期运行。 I added the boolean statement in there to make this eaiser to see. 我在其中添加了布尔型语句,以使其易于理解。

NOTE: I've seen this fix but I'd like to use a solution where I don't have a margin on the bottom of my program. 注意:我已经看到了此修复程序,但是我想使用一种在程序底部没有空白的解决方案。 How to prevent MigLayout from exceeding a container's bounds 如何防止MigLayout超出容器范围

Here is the code: 这是代码:

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;

public class StackOverFlowQuestion {
    public static void main(String[] args) {

        JPanel MiGPanel = new JPanel();
        MiGPanel.setBackground(Color.red);
        MiGPanel.setLayout(new MigLayout());  

        JPanel greenPanel = new JPanel();
        greenPanel.setBackground(Color.green);
        MiGPanel.add(greenPanel, "pos 0 0, width 33%, height 100%");

        JPanel yellowPanel = new JPanel();
        yellowPanel.setBackground(Color.yellow);
        MiGPanel.add(yellowPanel, "pos 33% 0, width 33%, height 100%");

        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.blue);
        MiGPanel.add(bluePanel, "pos 66% 0, width 34%, height 100%");


        JFrame mainFrame = new JFrame();
        mainFrame.setLayout(new BorderLayout());

        boolean scroll = true;
        if(scroll){
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.add(MiGPanel);
            scrollPane.setViewportView(MiGPanel);
            mainFrame.add(scrollPane);  
        }else{
            mainFrame.add(MiGPanel);
        }

        mainFrame.add(scrollPane);  
        mainFrame.pack();
        mainFrame.setSize(900, 600); 
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    }
}

After many sleepless nights, I came up with a solution: 经过许多不眠之夜,我想出了一个解决方案:

    greenPanel.setLayout(new BorderLayout());
    greenPanel.add(new JLabel("test"));
    MiGPanel.add(greenPanel, "pos 0 0 33% 100%");

    JPanel yellowPanel = new JPanel();
    yellowPanel.setBackground(Color.yellow);
    MiGPanel.add(yellowPanel, "pos 33% 0 66% 100%");

    JPanel bluePanel = new JPanel();
    bluePanel.setBackground(Color.blue);
    MiGPanel.add(bluePanel, "pos 66% 0 100% 100%");

Absolute positioned elements should not use with and height attributes, instead you should use the "pos xy x1 y1" method. 绝对定位的元素不应与with和height属性一起使用,而应使用“ pos xy x1 y1”方法。

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

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