How to prevent MigLayout from exceeding a containe

2019-04-29 14:55发布

问题:

I'm trying to construct a simple status panel using MigLayout as follows:

setLayout(new MigLayout("fillx", "[][p]")); // removing constructor args makes no difference
add(createStatusLabel(), "span 2, wrap");
add(createProgressBar(), "growx, pushx");
add(createCancelButton(), "");

This works fine as long as the status message displayed by the status label is short enough to fit within the current panel's size (the cancel button remains right-justified, and the progress bar resizes to take up the remaining space). If the status message is too long, it is not cropped, and causes the area to exceed the bounds of the container, resulting in the cancel button being pushed off screen.

Any suggestions on how to prevent this from happening?

Thanks

回答1:

Try setting the maximum width of the label to 100%.

You can do this by changing the layout for the label to "span 2, wrap, wmax 100%"

In my tests, I found that it still didn't look quite right, so you may want to subtract a little bit of length (something like wmax 100% - 10px) to bring it away from the edge.