• Inicio
  • Buscar
  • Ingresar
  • Registrarse

    Evil Dead: The Game el juego multijugador basado en la trilogía Evil DeaD y Ash vs Evil DeaD

    • Hack x Crack - Comunidad de Seguridad informática »
    • Programación »
    • Java y Android »
    • [Aporte] GridLayout
    • Imprimir
    Páginas: [1]   Ir Abajo

    Autor Tema: [Aporte] GridLayout  (Leído 1467 veces)

    Desconectado SMS

    • { L2 } Nativo Digital
    • **
    • Mensajes: 148
      • Ver Perfil
    [Aporte] GridLayout
    « en: Diciembre 13, 2012, 05:04:54 pm »
    Bueno el programa va a quedar algo así:



    Creamos 1 clase llamada gridlayout

    Código: [Seleccionar]
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import javax.swing.*;

    public class gridlayout extends JFrame {

    gridlayout(){

    setTitle("GridLayout");
    setBounds(0, 0, 350, 300);
    setVisible(true);
    Container contentpane = getContentPane();
    contentpane.setLayout(new GridLayout());
    prueba panel1 = new prueba();
    contentpane.add(panel1);


    }
    public static void main(String[]args){
    gridlayout laventana = new gridlayout();

    }

    }

    Ahora creamos otra clase llamada prueba

    Código: [Seleccionar]
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class prueba extends JPanel {

    prueba(){

    setLayout(new GridLayout(5,4));
       add(new JButton("CLs"));
       add(new JButton("Bck"));
       add(new JLabel(""));
       add(new JButton("Close"));
       add(new JButton("7"));
       add(new JButton("8"));
       add(new JButton("9"));
       add(new JButton("/"));
       add(new JButton("4"));
       add(new JButton("5"));
       add(new JButton("6"));
       add(new JButton("*"));
       add(new JButton("1"));
       add(new JButton("2"));
       add(new JButton("3"));
       add(new JButton("-"));
       add(new JButton("0"));
       add(new JButton("."));
       add(new JButton("="));
       add(new JButton("+"));

    }


    }

    Saludos y  espero que os guste :)
    En línea

    Desconectado Kenkox

    • { L0 } Ñuub
    • Mensajes: 20
      • Ver Perfil
    Re:[Aporte] GridLayout
    « Respuesta #1 en: Diciembre 16, 2012, 04:16:18 am »
    Citar
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import javax.swing.*;

    public class gridlayout extends JFrame {

       gridlayout(){
          
    setTitle("GridLayout");      
    setBounds(0, 0, 350, 300);
    setVisible(true);
    Container contentpane = getContentPane();
    contentpane.setLayout(new GridLayout());
    prueba panel1 = new prueba();
    contentpane.add(panel1);

          
       }
       public static void main(String[]args){
          gridlayout laventana = new gridlayout();
          
       }

    }

    Consejos:
    1°Los nombres de las clases siempre inician con mayuscula
    2°En esta clase estas agregando lineas que practicamente son inecesarias, como la que puse en rojo..
    puedes solamente poner "setLayout( new GridLayout() ); .. sin necesidad de crear el Container.... y pues asi obviamente ya eliminas el "contentpane.add(panel1);" y ya solo pones "add(panel1);"

    Ahora creamos otra clase llamada prueba

    Citar
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class prueba extends JPanel {

       prueba(){   
          
       setLayout(new GridLayout(5,4));
       add(new JButton("CLs"));
       add(new JButton("Bck"));
       add(new JLabel(""));
       add(new JButton("Close"));
       add(new JButton("7"));
       add(new JButton("8"));
       add(new JButton("9"));
       add(new JButton("/"));
       add(new JButton("4"));
       add(new JButton("5"));
       add(new JButton("6"));
       add(new JButton("*"));
       add(new JButton("1"));
       add(new JButton("2"));
       add(new JButton("3"));
       add(new JButton("-"));
       add(new JButton("0"));
       add(new JButton("."));
       add(new JButton("="));
       add(new JButton("+"));
       
       }
       
       
       }   

    En esta clase, te puedes ahorras un monton de lineas jajaja xDD mira, para que todo sea mas ordenado, lo que puedes hacer es crear un Array de String que contenga todos los simbolos, y pues con un simple for, inicializarlos e irlos agregando XDD

    String[] simbolos = {"Cls","Bck","Close","7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
    JButton[] botones = new JButton[19];
    JLabel etiqueta = new JLabel("");
    for( int kk=0;kk<19;kk++){
         if( kk == 2 ){
           add(etiqueta);
           botones[kk] = new JButton(simbolos[kk]);
           add(botones[kk];
         }
         else{
           botones[kk] = new JButton(simbolos[kk]);
           add(botones[kk]);
          }
    }

    Suerte
    « Última modificación: Diciembre 16, 2012, 11:55:43 pm por Kenkox »
    En línea

    • Imprimir
    Páginas: [1]   Ir Arriba
    • Hack x Crack - Comunidad de Seguridad informática »
    • Programación »
    • Java y Android »
    • [Aporte] GridLayout
     

    • SMF | SMF © 2013, Simple Machines
    • XHTML
    • RSS
    • WAP2
    Va un mudo y le dice a un sordo: Hack x Crack usa cookies. Pues eso... Learn more