Question

I'm trying to use MigLayout for my project but i'm struggling to understand the logic.

I'd like to place 2 labels on top and at the bottom of a certain panel with a margin of 20px between the labels and the ends of the panel.

Moreover, he labels must be centered horizontally.

Any ideas on how to do this ?

Was it helpful?

Solution

There are several ways to do this (we could use insets instead of gaptop/bottom for example). Here is a simple example:

JPanel p = new JPanel();
p.setLayout(new MigLayout("fill"));
p.add(new JLabel("1"), "top, gaptop 20px, center, wrap");
p.add(new JLabel("2"), "bottom, gapbottom 20px, center");

The above example uses top and bottom to position the labels at the top and bottom of the panel. A more realistic example, where we have content between these labels, may be something like this:

JPanel p = new JPanel();
p.setLayout(new MigLayout("debug, fill, insets 20 0 20 0", "", "[][grow][]"));
p.add(new JLabel("Top"), "center, wrap");
p.add(new JLabel("Center"), "center, grow, wrap");
p.add(new JLabel("Bottom"), "center");

This example uses insets for the desired 20px gap top and bottom, this way the 20px gap isnt added to the labels row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top