JAVA: Practicando con GridLayout.

21 03 2009

GridLayout, no es mas q otro manejador de disenio, la cual su partcularidad, conciste en crear una matriz, totalmente configurada.

Primero tenemos que crear el contenedor(JFrame, JPanel, etc), y luego atravez del metodo “setLayout()” asignarle el layout correspondiente:

JFrame frame = new JFrame();
frame.setLayout(new GridLayout(Filas, Columnas));

Tambien  podemos indicar el espacio entre cada objeto de la matriz.

frame.setLayout(new GridLayout(Filas, Columnas,EspacioEntreFilas, EspacioEntreColumnas));

Creoq con un ejemplo, se entenderia mejor.

PAra el ejemplo, he intentado, realizar una pantalla de calculadora…jejeje,

Aqui el fuente.

/*Demo hecho por INFORUX*/
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.WindowConstants;

public class DemoGridLayout{

    JFrame frame;
    JPanel panelSuperior, panelInferior;
    JButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt0, btRT, btCE, btCL, btMas, btMenos, btMul, btDiv, btIgual, btMN, btPunto;
    JTextField pantalla;

    public DemoGridLayout(){
        construyePanelSuperior();
        construyePanelInferior();
        construyeVentana();}

    void construyePanelSuperior(){
        panelSuperior = new JPanel ();
        panelSuperior.setLayout(new FlowLayout());
        pantalla = new JTextField(20);
        panelSuperior.add(pantalla);}

    void construyePanelInferior(){
        panelInferior= new JPanel();
        panelInferior.setLayout(new GridLayout(5,4,8,8));
        bt1=new JButton("1");
        bt2=new JButton("2");
        bt3=new JButton("3");
        bt4=new JButton("4");
        bt5=new JButton("5");
        bt6=new JButton("6");
         bt7=new JButton("7");
        bt8=new JButton("8");
        bt9=new JButton("9");
        bt0=new JButton("0");
        btRT=new JButton("Rtc");
        btCE=new JButton("CE");
        btCL=new JButton("CL");
        btMas=new JButton("+");
        btMenos=new JButton("-");
        btMul=new JButton("x");
        btDiv=new JButton("/");
        btIgual=new JButton("=");
        btMN=new JButton("+/-");    
        btPunto=new JButton(".");
        panelInferior.add(btRT);
        panelInferior.add(btCE);
        panelInferior.add(btCL);
        panelInferior.add(btMN);
        panelInferior.add(bt7);
        panelInferior.add(bt8);
        panelInferior.add(bt9);
        panelInferior.add(btDiv);
        panelInferior.add(bt4);
        panelInferior.add(bt5);
        panelInferior.add(bt6);
        panelInferior.add(btMul);
        panelInferior.add(bt1);
        panelInferior.add(bt2);
        panelInferior.add(bt3);
        panelInferior.add(btMenos);
        panelInferior.add(bt0);
        panelInferior.add(btPunto);
        panelInferior.add(btIgual);
        panelInferior.add(btMas);}

    void construyeVentana(){
        frame =new JFrame("Calculadora ");
        frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
        frame.add(panelSuperior);
        frame.add(panelInferior);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}

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

Una compilada, y ejecutamos.

javac DemoGridLayout.java
java DemoGridLayout

El resultado sera  asi.

pantallazo-calculadora

pantallazo-calculadora-1

La idea, es manejar el GridLayout.

Saludos.


Acciones

Información

8 respuestas

22 10 2009
Dark

queria saber para que sirve el codigo Inforux ?? dentro de la aplicación

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

17 11 2009
Anónimo

mandame ejercicios a mi correo porfavor.
necesito para practicaar.

soy de INACAP VALDIVIA

17 11 2009
Anónimo

ESTE ES MI CORREO mardehebra@hotmail.es

25 06 2011
joel

MUY BUEN EJEMPLO:
FAVOR PONER EJEMPLO DE USO DE COMBOS CON BASES DE DATOS

SALUDOS DESDE GUADALAJARA
UNIVERSIDAD DE GUADALAJARA

20 07 2011
Richard

Muchas gracias por su aportacion, he revisado muchos sitios y todos se complican demasiado, me ha ayudado mucho, siga haciendo publicaciones como etas, gracias de antemano…

21 09 2011
Diego

Una pregunta, que UIManager estás usando en el ejemplo??

saludos, Diego

11 12 2011
madelainecolicheo

Buen Ejemplo Gracias :)

17 05 2012
os

Muy bien gracias

Deja un comentario

Fill in your details below or click an icon to log in:

Logo de WordPress.com

You are commenting using your WordPress.com account. Log Out / Cambiar )

Twitter picture

You are commenting using your Twitter account. Log Out / Cambiar )

Facebook photo

You are commenting using your Facebook account. Log Out / Cambiar )

Connecting to %s




Seguir

Get every new post delivered to your Inbox.