Arrange labels vertically

2

I wanted to put panels in specific positions, so I tried combining some layout managers, but I still have not got the result I need.

I tried to use gridLayout , so components would not be stretched, but I'm not sure if I'm making a bad use of it.

I tried to do this:

whereblackboardsarecomponents,inthiscaselabels.

AndtheresultIgotwas:

Cananyoneshowmeorindicateawaytodothis?

Hereisanexampleofthecode:

importjava.awt.Color;importjava.awt.GridLayout;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.SwingUtilities;importstaticjavax.swing.WindowConstants.DISPOSE_ON_CLOSE;publicclassPainelPosicao{publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(()->newPainelPosicao());}publicPainelPosicao(){JFramejFrame=newJFrame();jFrame.setUndecorated(true);jFrame.setContentPane(newPane());jFrame.setSize(500,300);jFrame.setVisible(true);jFrame.setLocationRelativeTo(null);jFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);}classPaneextendsJPanel{publicPane(){setLayout(newGridLayout(2,1));JPanelgridPainel=newJPanel();gridPainel.setLayout(newGridLayout(2,1));gridPainel.setBackground(Color.GREEN);JPanelpainel=newJPanel();painel.add(newJLabel("Label 01"));

            JPanel painelProgress = new JPanel();
            painelProgress.setLayout(new GridLayout(2, 1));
            painelProgress.add(new JLabel("Label 01"));
            painelProgress.add(new JLabel(".......Label 02 ......"));

            gridPainel.add(painel);
            gridPainel.add(painelProgress);
            add(gridPainel);
            setBackground(Color.BLACK);
        }
    }
}
    
asked by anonymous 01.04.2018 / 21:58

1 answer

2

If the goal is to align the bottom two labels in the center and bottom, I was able to do the following:

import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PainelPosicao {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new PainelPosicao());
    }

    public PainelPosicao() {
        JFrame jFrame = new JFrame();
        jFrame.setUndecorated(true);
        jFrame.setContentPane(new Pane());
        jFrame.setPreferredSize(new Dimension(500, 300));
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }



    class Pane extends JPanel {

        public Pane() {

            setLayout(new BorderLayout());

            JPanel painel = new JPanel();
            painel.add(new JLabel("Label 01"));

            JPanel painelProgress = new JPanel();
            painelProgress.setLayout(new BoxLayout(painelProgress, BoxLayout.PAGE_AXIS));

            JLabel label1 = new JLabel("Label 01");
            label1.setAlignmentX(Component.CENTER_ALIGNMENT);
            painelProgress.add(label1);

            JLabel label2 = new JLabel(".......Label 02 ......");
            label2.setAlignmentX(Component.CENTER_ALIGNMENT);
            painelProgress.add(label2);

            add(painel, BorderLayout.NORTH);
            add(painelProgress, BorderLayout.SOUTH);
        }
    }
}

Result:

Toorganizethepanelwhereyouaddthetwolabels,Iusedthelayoutmanager BoxLayout ", which allows me to add components in portrait orientation. Then I used the method setAlignmentX() to directing BoxLayout to align the component horizontally in the center.

This is another simplistic approach, without using either of these panels:

class Pane2 extends JPanel {

    public Pane2() {

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

        JLabel label1 = new JLabel("Label 01");  
        label1.setAlignmentX(Component.CENTER_ALIGNMENT);
        add(label1);

        add(Box.createVerticalGlue());

        JLabel label2 = new JLabel("Label 02");
        label2.setAlignmentX(Component.CENTER_ALIGNMENT);
        add(label2);

        JLabel label3 = new JLabel(".......Label 03 ......");
        label3.setAlignmentX(Component.CENTER_ALIGNMENT);
        add(label3);

    }
}

Still using the flexibility of using BoxLayout , in this approach I organize the 3 labels in the same JPanel , and use a fill feature, using the createVerticalGlue() ", which fills the space from the top label, pushing the two labels down the lower limit of the screen.

    
01.04.2018 / 22:32