how to order JPanel in different positions?

0

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

Edit: I have the PanelX Class, which I set in JFrame as position SOUTH , inside this class I have 2 panels (Panel 02 and 03), I want to orient one panel to the left and the other one to the right.

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

ExampleofwhatIdid:

packagetool;importjava.awt.BorderLayout;importjava.awt.Color;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;publicclassTesteextendsJFrame{publicstaticvoidmain(String[]args){Testeteste=newTeste();teste.setVisible(true);}privatePainelXpainel=newPainelX();publicTeste(){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;
    }    
}
    
asked by anonymous 27.06.2017 / 18:10

1 answer

2

First of all, a warning:

  

Always start the screen within the Event-Dispatch-Thread ,   because swing is not Thread-Safe , and the whole GUI needs to start in.   of this unique Thread. Nesta   answer better explains the   reason for this and possible problems that may occur. This Other   answer shows some   ways to start the application within this Thread.

You can do this in a variety of ways, and using the vast majority of layouts managers , but what I found the least expensive is to use BoxLayout .

You should set BoxLayout to the layout of your PainelX :

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

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

Once you've done this, you can add the two panes, but between them, add an invisible component through the createHorizontalStrut()

add(Box.createHorizontalStrut(50));

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

The adapted code looks like this:

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:

I want to leave a recommendation, since you're working a lot with layouts, prefer to set preferred sizes with the setPreferredSize method instead of setSize , since most layouts completely ignore the layouts.

    
27.06.2017 / 20:51