Viruz Blog
¿Quieres reaccionar a este mensaje? Regístrate en el foro con unos pocos clics o inicia sesión para continuar.

Administradores de diseño

Ir abajo

Administradores de diseño Empty Administradores de diseño

Mensaje  Admin (Viruz) Miér Dic 19, 2012 2:16 am

Existen personas que se vienen adentrando al mundo de la programación y en cuestiones de la interfaz gráfica tanto como para AWT y Swing existen administradores de diseño, pero... ¿Qué son los administradores de diseño?.
Estos determinan la forma en la cual se van a presentar los componentes gráficos dentro de la ventana o del panel, así también como tener la habilidad de poder ajustarse a la redimensión que el usuario pueda llegar a darle a dicha ventana.
Ahora dentro de un ejemplo veremos los "principales" administradores de diseño con los que se cuenta aunque no quiere decir que sean los únicos, existen otros como GridBagLayout donde se muestra un ejemplo:

Lo que se necesita conocer
  • Uso de GridBagLayout.
Imágenes
[Tienes que estar registrado y conectado para ver esa imagen]
¿Como funciona el programa?

  1. Se va a dividir la ventana principal con un GridLayout de manera que nos quede 2x2.
  2. Lo que consta en la parte de arriba del lado izquierda se contará con un administrador de diseño FlowLayout, y del lado derecho tendremos un BorderLayout.
  3. Para la parte de abajo del lado derecho se tendrá otro GridLayout de 5x5 de manera que simulemos una numeración para ello se cargan todos los botones mediante un ciclo for().

    Código:
          for(int i=1;i<26;i++){
                p4.add(new Button(""+i));
            }
  4. El tercer panel (izquierda inferior) se divide en 2 partes la parte de arriba carga con BoxLayout el cual tiene un JComboBox que se encargará de la función de cambiar el CardLayout de la parte de abajo, utilizando el método itemStateChanged(ItemEvent e).
Código

Clase Main
/**
*
* @author Viruz
*/
public class Main {
public static void main(String[] args) {
Ventana v = new Ventana();
v.setVisible(true);
}
}

Clase Ventana

import java.awt.CardLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Viruz
*/
public class Ventana extends javax.swing.JFrame implements ItemListener {

/**
* Creates new form Ventana
*/
public Ventana() {
initComponents();

for (int i=1; i<26; i++) {
jPanel4.add(new JButton(""+i));
}

jComboBox1.addItemListener(this);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jPanel5 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jPanel6 = new javax.swing.JPanel();
jButton7 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jPanel4 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Viruz Blog: Administradores de Diseño");
getContentPane().setLayout(new java.awt.GridLayout(2, 2));

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("FlowLayout"));

jLabel1.setText("FlowLayout");
jPanel1.add(jLabel1);

jTextField1.setColumns(5);
jPanel1.add(jTextField1);

jButton1.setText("Examinar");
jPanel1.add(jButton1);

getContentPane().add(jPanel1);

jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("BorderLayout"));
jPanel2.setLayout(new java.awt.BorderLayout());

jButton2.setText("Norte");
jPanel2.add(jButton2, java.awt.BorderLayout.PAGE_START);

jButton3.setText("Oeste");
jPanel2.add(jButton3, java.awt.BorderLayout.LINE_START);

jButton4.setText("Sur");
jPanel2.add(jButton4, java.awt.BorderLayout.PAGE_END);

jButton5.setText("Este");
jPanel2.add(jButton5, java.awt.BorderLayout.LINE_END);

jButton6.setText("Centro");
jPanel2.add(jButton6, java.awt.BorderLayout.CENTER);

getContentPane().add(jPanel2);

jPanel3.setLayout(new java.awt.GridLayout(2, 0));

jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("BoxLayout"));
jPanel5.setLayout(new javax.swing.BoxLayout(jPanel5, javax.swing.BoxLayout.LINE_AXIS));

jLabel3.setText("BoxLayout: ");
jPanel5.add(jLabel3);

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Boton", "Etiqueta" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jPanel5.add(jComboBox1);

jPanel3.add(jPanel5);

jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("CardLayout"));
jPanel6.setLayout(new java.awt.CardLayout());

jButton7.setText("Boton de Panel 1");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jPanel6.add(jButton7, "Boton");

jLabel2.setText("Etiqueta de Panel 2");
jPanel6.add(jLabel2, "Etiqueta");

jPanel3.add(jPanel6);

getContentPane().add(jPanel3);

jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("GridLayout"));
jPanel4.setLayout(new java.awt.GridLayout(5, 5));
getContentPane().add(jPanel4);

pack();
}// </editor-fold>

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

// /**
// * @param args the command line arguments
// */
// public static void main(String args[]) {
// /*
// * Set the Nimbus look and feel
// */
// //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
// /*
// * If Nimbus (introduced in Java SE 6) is not available, stay with the
// * default look and feel. For details see
// * [Tienes que estar registrado y conectado para ver este vínculo]
// */
// try {
// for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
// if ("Nimbus".equals(info.getName())) {
// javax.swing.UIManager.setLookAndFeel(info.getClassName());
// break;
// }
// }
// } catch (ClassNotFoundException ex) {
// java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (InstantiationException ex) {
// java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (IllegalAccessException ex) {
// java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (javax.swing.UnsupportedLookAndFeelException ex) {
// java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// }
// //</editor-fold>
//
// /*
// * Create and display the form
// */
// java.awt.EventQueue.invokeLater(new Runnable() {
//
// @Override
// public void run() {
// new Ventana().setVisible(true);
// }
// });
// }
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JTextField jTextField1;
// End of variables declaration

/**
* @return the jComboBox1
*/
public javax.swing.JComboBox getjComboBox1() {
return jComboBox1;
}

/**
* @param jComboBox1 the jComboBox1 to set
*/
public void setjComboBox1(javax.swing.JComboBox jComboBox1) {
this.jComboBox1 = jComboBox1;
}

@Override
public void itemStateChanged(ItemEvent e) {
CardLayout cl = (CardLayout)(jPanel6.getLayout());
cl.show(jPanel6, (String)e.getItem());
}
}


Descarga el Código
Administradores de diseño
Autor: Viruz study
Enlace: Administradores de diseño en Java
Admin (Viruz)
Admin (Viruz)
Admin

Mensajes : 148
Fecha de inscripción : 23/09/2012
Edad : 35
Localización : Desktop

https://viruz.foroactivo.mx

Volver arriba Ir abajo

Volver arriba


 
Permisos de este foro:
No puedes responder a temas en este foro.