Alessandro
Alessandro

Reputation: 75

horizontal alignment of two JLabels using BoxLayout

i just want to align horizontally two JLabels using a BoxLayout, is it possible? this is my code:

public class CreditsPanel extends JPanel {

private static final long serialVersionUID = 1L;
private static final int GAP = 75;

public CreditsPanel() {
    super();

    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    JLabel title = new JLabel("The developers of the game: ");
    this.add(title);
    title.setAlignmentX(Component.CENTER_ALIGNMENT);
    this.add(Box.createVerticalStrut(GAP));

    JLabel[] names = new JLabel[2];
    names[0] = new JLabel("Pippo");
    names[1] = new JLabel("Pluto");
    int i = 0;
    for (JLabel l : names) {
        ImageIcon icon = getIcon(i);
        l.setAlignmentX(Component.CENTER_ALIGNMENT);
        l.setHorizontalTextPosition(JLabel.CENTER);
        l.setVerticalTextPosition(JLabel.TOP);
        l.setIcon(icon);
        this.add(l, BorderLayout.EAST);
        this.add(Box.createVerticalStrut(25));
        i++;
    }
}

thank you in advance. getIcon is only a private method to get different icon to set to my labels.

Now I have something like this: http://postimg.org/image/tml9wmr4f/ and i want this: http://postimg.org/image/nlg0fvygf/

Upvotes: 0

Views: 163

Answers (1)

camickr
camickr

Reputation: 324098

Basic code looks fine to me.

The only thing out of the ordinary is:

this.add(l, BorderLayout.EAST);

Don't know if the BorderLayoutEAST will cause a problem.

Post a SSCCE if you need more help.

Upvotes: 1

Related Questions