Arrange Boxes vertically

Asked

Viewed 364 times

2

I wanted to put panels in specific positions, so I tried to combine some layout managers, but I still didn’t get the result I need.

I tried to use the gridLayout, so the components wouldn’t be stretched, but I’m not sure I’m making a wrong use of it.

I tried to do that:

inserir a descrição da imagem aqui

where the black boards are components, in that case labels.

And the result I got was:

inserir a descrição da imagem aqui

Can anyone show me or indicate a way to do that?

Follow an example of the code:

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

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.setSize(500, 300);
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    class Pane extends JPanel {

        public Pane() {
            setLayout(new GridLayout(2, 1));
            JPanel gridPainel = new JPanel();
            gridPainel.setLayout(new GridLayout(2, 1));
            gridPainel.setBackground(Color.GREEN);

            JPanel painel = new JPanel();
            painel.add(new JLabel("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);
        }
    }
}
  • I don’t understand what you want to do. Want to put the two bottom Abels in the black frame?

  • Actually, it was for when gray was inside the frame, I don’t know why he didn’t stay. The Abels I wanted to center under the gray quandro

  • Do you need that kind of panel there? I can do without it, it is quite useless in the code

  • You can do it all there with just one panel. Using Gridbaglayout.

  • Can yes, I’ll match the other question I asked, then just do what you explained to me there

  • I made a more simplistic approach using only a jpanel, as I had commented. See in the answer, it is as an alternative.

  • 1

    @Articuno The two turned out excellent, it was exactly what I needed ! Thank you

Show 2 more comments

1 answer

1


If the goal is to align the center and bottom position of the two bottom planes, I was able to do so:

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);
        }
    }
}

Upshot:

inserir a descrição da imagem aqui

To organize the panel where you add the two Labels, I used the layout manager BoxLayout, which allows me to add components in vertical orientation. Then I used the method setAlignmentX() to guide the BoxLayout align the component horizontally in the center.


There’s this other more simplistic approach, without using that much of 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 use of BoxLayout, in this approach I organize the 3 Abels in it JPanel, and use a fill feature, through the method createVerticalGlue(), that fills the space from the top label, pushing the two low Labels up to the bottom edge of the screen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.