How to order Jpanel in different positions?

Asked

Viewed 658 times

0

Which Layout manager should I use to position the panels as in the illustrative image below.

Edit: I have the Painelx Class, which I am in JFrame as position SOUTH, within that class I have 2 panels (Panel 02 and 03), I want to orient one panel to the left and the other to the right.

I tried to apply the BorderLayout to position the secondary panels (panel 2 and 3), but it did not work.

inserir a descrição da imagem aqui

Example of what I did:

package tool;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Teste extends JFrame
{
    public static void main(String[] args) {
        Teste teste = new Teste();
        teste.setVisible(true);                
    }

    private PainelX painel = new PainelX();

    public Teste()
    {
        setSize(600, 400);
        getContentPane().add("South", painel);        
        setDefaultCloseOperation(EXIT_ON_CLOSE);        
    }
}

class PainelX extends JPanel
{       
    public PainelX() 
    {
        setBackground(Color.red);
        add(painel02(), BorderLayout.EAST);       
        add(painel03(), BorderLayout.WEST);        
    }

    private JComponent painel02()
    {
        JPanel painel = new JPanel();
        JLabel teste = new JLabel("Painel 02");

        painel.add(teste);
        return painel;
    }

    private JComponent painel03()
    {
        JPanel painel = new JPanel();
        JLabel teste = new JLabel("Painel 03");

        painel.add(teste);
        return painel;
    }    
}
  • I don’t see pictures, so I couldn’t really understand what you want to do.

  • @diegofm the image does not appear ?

  • It is blocked for me. Why it is important to always explain verbatim, the image should be complementary and not "the" explanation.

  • @diegofm edited the question, give a look if this understandable.

  • Want to position the two panels side by side? Just that?

  • @diegofm not literally, I want to leave one on the "star left and right", the way they are in the example, they are side by side, only that they are together, I wanted them to be separated. Between them there was a space, perhaps it would be necessary to have a half empty panel, to be able to give this space ?

Show 1 more comment

1 answer

2


First of all, a warning:

Always start the screen inside the Event-Dispatch-Thread, because swing is not Thread-Safe, and the entire GUI needs to start in this one Thread. In this reply explains better the reason for this and any problems that may occur. This other reply shows some ways to start the application within this thread.


It is possible to do this in various ways, and using the vast majority of layouts managers, but what I found less expensive is to use BoxLayout.

You must define the BoxLayout as layout of your PainelX:

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

The constructor of this class requires you to pass the reference of the container to which it will be applied and the guidance that the layout will use to organize the added components. In this case, BoxLayout.X_AXIS was chosen because it organizes the components by adding horizontally from left to right.

Once done, you can add the two panels, but between them, add an invisible component through the method createHorizontalStrut()

add(Box.createHorizontalStrut(50));

Note that it gets a fixed size, which will be kept between the components independent of the screen size. You can change as needed.

The adapted code is thus:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class Teste extends JFrame
{
    public static void main(String[] args) {

        SwingUtilities.invokeLater(() ->{
            Teste teste = new Teste();
            teste.setVisible(true);
        });                
    }

    private PainelX painel = new PainelX();

    public Teste()
    {
        setSize(600, 400);
        getContentPane().add(painel, BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);        
    }
}

class PainelX extends JPanel
{       
    public PainelX() 
    {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));        
        setBackground(Color.red);
        add(painel02());
        add(Box.createHorizontalStrut(50));
        add(painel03());
    }

    private JComponent painel02()
    {
        JPanel painel = new JPanel();
        JLabel teste = new JLabel("Painel 02");
        painel.add(teste);
        return painel;
    }

    private JComponent painel03()
    {
        JPanel painel = new JPanel();
        JLabel teste = new JLabel("Painel 03");
        painel.add(teste);
        return painel;
    }    
}

To demonstrate better, see the result in the image below:

inserir a descrição da imagem aqui

I want to leave a recommendation, since you are working a lot with layouts, prefer to define preferred sizes with the method setPreferredSize instead of setSize, because most layouts completely ignore this last.

  • 1

    Thanks, alias thanks twice, one for the layout and one for this createHorizontalStrut(), I didn’t even know I had this, I used Jtextfield with setVisible(false). ; to do what it does. Now I can remove the gambiarras from my codes, thank you ! :)

Browser other questions tagged

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