JAVA: Practicando con BorderLayout

20 01 2009

Seguimos con los layouts. Recordar que para entender este ejemplo es necesario chekear las anteriores explicaciones sobre FlowLayout y BoxLayoutLayout

BorderLayout, es un layout que ubica los componentes en cualquiera de sus 5 regiones que tiene..

Un componente podemos ubicarlo arriba, abajo, izquierda o a la derecha.

borderlayout

Para establecer a BorderLayout como manegador de Disenio,

JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());

Luego para agregar los componentes

frame.add(etiqeuta, BorderLayout.CENTER);
frame.add(botonIzquierdo,BorderLayout.WEST);
frame.add(botonDerecho,BorderLayout.EAST);
frame.add(cajaTexto,BorderLayout.NORTH);
frame.add(panelInferior,BorderLayout.SOUTH);

No es obligatorio, llenar todas las regiones, en el caso de existir alguna region sin componente,  esta region visiblemente se anulara, es importante esto, ya que si enviamos un componente al centro, y no enviamos nada en la parte izquierda(WESTH), entonces, el componente enviado al centro  se visaluizara en la parte WESTH.

En el ejemplo siguiente se tiene, un JFrame, este mismo los ordenara atravez de un BorLayout,  En el norte, ira una JLabel, en el centro un JTextAre,  y en el sur, este y el oeste un panel para cada region.

Cada panel, ubicara sus componentes de la forma que noosotros queramos,  en este caso el del sur, atravez de un flowLayout, y el de este y oeste con un BoxLayout.

Aqui el demo

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class DemoBorderLayout {
    //variables y comtenedores
    private JLabel etiquetaSu;
    private JPanel panelIzquierdo, panelDerecho, panelInferior;
    private JButton botonIz1, botonIz2, botonIz3;
    private JButton botonDe1, botonDe2, botonDe3;
    private JButton    botonIn1, botonIn2;
    private JScrollPane scroll;

    public void contruyePanelIzquierdo(){
        panelIzquierdo = new JPanel();
        botonIz1=new JButton("Izq1");
        botonIz2=new JButton("Izq2");
        botonIz3=new JButton("Izq3");
        panelIzquierdo.setLayout(new BoxLayout(panelIzquierdo,BoxLayout.Y_AXIS));
        panelIzquierdo.setBackground(Color.red);
        panelIzquierdo.add(botonIz1);
        panelIzquierdo.add(botonIz2);
        panelIzquierdo.add(botonIz3);
    }

    public void contruyePanelDerecho(){
        panelDerecho = new JPanel();
        botonDe1=new JButton("Der1");
        botonDe2=new JButton("Der2");
        botonDe3=new JButton("Der3");
        panelDerecho.setLayout(new BoxLayout(panelDerecho,BoxLayout.Y_AXIS));
        panelDerecho.setBackground(Color.blue);
        panelDerecho.add(botonDe1);
        panelDerecho.add(botonDe2);
        panelDerecho.add(botonDe3);
    }

    public void contruyePanelInferior(){
        panelInferior = new JPanel();
        botonIn1=new JButton("Aceptar");
        botonIn2=new JButton("Cancelar");
        panelInferior.setLayout(new FlowLayout());
        panelInferior.setBackground(Color.green);
        panelInferior.add(botonIn1);
        panelInferior.add(botonIn2);
    }

    public void contruyeVentana(){
        JFrame frame = new JFrame();
        scroll = new JScrollPane(new JTextArea("JTextArea",10,15));
        etiquetaSu = new JLabel("Demostracion por INFORUX ");
        Font aux=etiquetaSu.getFont();
        etiquetaSu.setFont(new Font(aux.getFontName(), aux.getStyle(), 20));
        frame.setLayout(new BorderLayout());

        //agregamos los paneles al frame principal
        frame.add(etiquetaSu,BorderLayout.NORTH);
        frame.add(scroll, BorderLayout.CENTER);
        frame.add(panelIzquierdo,BorderLayout.WEST);
        frame.add(panelDerecho,BorderLayout.EAST);
        frame.add(panelInferior,BorderLayout.SOUTH);
        //Configuramos el frame
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public DemoBorderLayout(){
        contruyePanelInferior();
        contruyePanelIzquierdo();
        contruyePanelDerecho();
        contruyeVentana();
    }

    public static void main (String [] inforux){
        new DemoBorderLayout();
    }
}

Compilamos y ejecutamos.

$javac DemoBorderLayout.java
$java DemoBorderLayout

El resultador ser asi

pantallazo3

pantallazo13

En esta interfaz de usuario, solo se empleo los layouts, explicados FlowLayout, BoxLayout y BorderLayout.

Sencillo.

PD: La API  completa de BorderLayout

Saludos


Acciones

Information

15 responses

23 05 2009
Hor

Mui buen ejemPlo peRO si por ejemplo en la zoma norte no kiero ningun boton

peRO kiero ke c kede un marguen es decir un espacio como le haGO??

31 08 2009
German Espinel Bernal

me parece muy grafico ,
¿ la distribucion ordenada del panael izquierdo y panel derecho es automatica y como se puede distancia mas entre los botoneas?

22 07 2010
cueto

util, y practica la explikacion

4 12 2012
Propane Heater

Hello to all, how is everything, I think every
one is getting more from this website, and your views are fastidious in favor of
new viewers.

5 06 2013
Caroline

Please note doctors don’t prescribe oral medicines for vaginal infections, it can lead to side-effects. And with good reason because sometimes running an app on the big screen is nothing short of magical. Aided by the Pills rapidly getting best tablet pc all of the direct device when using the iphone, it is really distinct that we should expect to witness it develop worldwide recognition for countless years.

26 02 2015
MBojorquez

Gracias, me habían encargado un chat usando el BorderLayout y ni siquera lo conocía. Me ayudó la explicación y el ejemplo.

20 07 2016
Abraxas Demian

gracias 😀

22 01 2017
100000millonesdesoles

Muchas gracias, es un ejemplo perfeto del borderlayout , claro y muy bien explicado. Mañana examen. XD.

1 12 2018
14 03 2024
Anónimo

joputa

14 03 2024
Anónimo

la tuya que se me abre hijoe perra

14 03 2024
Anónimo

chupa mi pinga cabron

14 03 2024
Anónimo

Hola

14 03 2024
Anónimo

pa ti mi cola

14 03 2024
Anónimo

doxeado papu

192.168.0.56

2555.255.255.192

San Sebastian

Martin Calvo Otaegui

Replica a German Espinel Bernal Cancelar la respuesta