Upload
phamliem
View
217
Download
0
Embed Size (px)
Citation preview
UNIVERSIDAD POLITECNICA SALESIANAINGENIERIA ELECTRÓNICA
PROGRAMACIÓN IINOMBRE: León Castillo Tania Talía
CURSO: Tercero - G3TEMA: LAYOUTS
EJEMPLOS FLOW LAYOUT
import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;
public class Flow extends JFrame {/// CREAMOS UN PANEL JPanel panel = new JPanel();////***CREAMOS LOS BOTONES JButton boton1 = new JButton("Panel de Boton 1 ");JButton boton2 = new JButton("Panel de Boton 2 ");JButton boton3 = new JButton("Panel de Boton 3 ");JButton boton4 = new JButton("Panel de Boton 4 ");JButton boton5 = new JButton("Panel de Boton 5 ");JButton boton6 = new JButton("Panel de Boton 6 ");
public Flow() {panel.add(boton1); //Añado cada boton al panel
creadopanel.add(boton2);panel.add(boton3);panel.add(boton4);panel.add(boton5);panel.add(boton6);panel.setLayout(new FlowLayout()); //Añado el Layout para
organizar los componentessetContentPane(panel); /// añado el Panel al Jframe que
contiene los botones creadossetSize(600,150); ////TAMAÑO DE LA VENTANA PRINCIPAL setTitle(" FlowLayout"); ///TITULO DE LA VENTANA PRINCIPAL setVisible(true); ///HACEMOS VISIBLE LA VENTANA
}public static void main(String[] args) {
new Flow();}
}
BORDER LAYOUT import java.awt.BorderLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;
public class Border extends JFrame {
JPanel panel = new JPanel();
public Border() {
//////////////////*****BORDER LAYOUT *****panel.setLayout(new BorderLayout());
////////////******CREO LOS BOTONES JButton norte = new JButton("BOTON DE NORTE");JButton sur = new JButton("BOTON DE PANEL SUR");JButton este = new JButton("BOTON DE PANEL ESTE");JButton oeste = new JButton("BOTON DE PANEL OESTE");JButton centro = new JButton("BOTON PANEL CENTRO");
//////////////****INCICO LA POSICION DE CADA BOTON CON BORDERLAYOUT
panel.add(norte, BorderLayout.NORTH); //Nortepanel.add(sur, BorderLayout.SOUTH); //Surpanel.add(este, BorderLayout.EAST); //Estepanel.add(oeste, BorderLayout.WEST); //Oestepanel.add(centro, BorderLayout.CENTER); //Centro
///////*******AÑADO BORDER LAYOUT******//////
add(panel); // añado el Panel al Jframe que contiene los componentes añadidos
setSize(550,250); //TAMAÑO setTitle("BorderLayout"); /// NOMBRE DE LA VENTANA PRINCIPAL setVisible(true); //// HAGO VISIBLE A MI VENTANA
}public static void main(String[] args) {
new Border();
}}
CARD LAYOUT import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;
public class Card extends JFrame {
JButton siguiente = new JButton("siguiente"); /// BOTON EL CUAL AL ACCIONARLO PASARA A LAS DEMAS ETIQUETAS
JLabel etiqueta1 = new JLabel("Componente 1"); //Creo Etiquetas JLabel etiqueta2 = new JLabel("Componente 2");JLabel etiqueta3 = new JLabel("Componente 3");JLabel etiqueta4 = new JLabel("Componente 4");JPanel panelComponentes= new JPanel(); //Creo un
PanelCardLayout cl=new CardLayout(); //Con
este Objeto llamo a la clase CardLayoutpublic Card() {
setLayout(null); // Anulo el ordenamiento para yo organizar los componentes
siguiente.setBounds(50, 50, 100, 50); // (posicion x, posicion y, ancho que ocupara, largo que ocupara)
add(siguiente); //// Añado Boton al Frame
panelComponentes.setLayout(cl); // Añado CardLayout para organizar al Panel
panelComponentes.add(etiqueta1);// Añado las etiquetas al panel en el orden que quiero que se muestre
panelComponentes.add(etiqueta2);panelComponentes.add(etiqueta3);panelComponentes.add(etiqueta4);
siguiente.addActionListener(new ActionListener() { //Creo un evento al Boton siguiente
public void actionPerformed(ActionEvent e) {cl.next(panelComponentes); //
CardLayOut mostrara un componente a la vez del panel}
});panelComponentes.setBounds(200, 50 , 100, 100);add(panelComponentes); //(posicion x, posicion y, ancho que
ocupara, largo que ocupara)setSize(400,300); //// tamaño que deseo para mostrar la
ventana creada setTitle("CardLayout"); /// doy un nombre a mi ventanasetVisible(true); /// hago visible a mi ventana
}public static void main(String[] args) {
new Card();
}}
BOX LAYOUTimport java.awt.FlowLayout;
import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;
public class Box extends JFrame{
/// CREAMOS UN PANEL JPanel panel = new JPanel();////***CREAMOS LOS BOTONES JButton boton1 = new JButton("Panel de Boton 1 ");JButton boton2 = new JButton("Panel de Boton 2 ");JButton boton3 = new JButton("Panel de Boton 3 ");JButton boton4 = new JButton("Panel de Boton 4 ");JButton boton5 = new JButton("Panel de Boton 5 ");JButton boton6 = new JButton("Panel de Boton 6 ");
public Box() {panel.add(boton1); //Añado cada boton al panel
creadopanel.add(boton2);panel.add(boton3);panel.add(boton4);panel.add(boton5);panel.add(boton6);panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
//Añado el Layout para organizar los componentes
//Y_AXIS Se organizaran en el eje Y//Y_AXIS Se organizaran en el eje X
setContentPane(panel); /// añado el Panel al Jframe que contiene los botones creados
setSize(300,250); ////TAMAÑO DE LA VENTANA PRINCIPAL setTitle(" BoxLayout"); ///TITULO DE LA VENTANA PRINCIPAL setVisible(true); ///HACEMOS VISIBLE LA VENTANA
}public static void main(String[] args) {
new Box();}
}
NO LAYOUT MANAGERimport javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;
public class NoLayoutManager extends JFrame {JLabel etiqueta= new JLabel("Ejemplo: setLayout(null)");JButton boton1 = new JButton("Panel de Boton 1 ");JButton boton2 = new JButton("Panel de Boton 2 ");JButton boton3 = new JButton("Panel de Boton 3 ");public NoLayoutManager() {
setLayout(null);
etiqueta.setBounds(150, 100, 150, 25); //(posicion x, posicion y, ancho que ocupara en la ventana, largo que ocupara en la ventana)
boton1.setBounds(50, 150, 150, 50);boton2.setBounds(200, 150, 150, 150);boton3.setBounds(150, 300, 150, 100);add(boton1);add(etiqueta);add(boton2);add(boton3);setSize(500,500); ////TAMAÑO DE LA VENTANA PRINCIPAL setTitle(" setBounds "); ///TITULO DE LA VENTANA PRINCIPAL setVisible(true); ///HACEMOS VISIBLE LA VENTANA
}
public static void main(String[] args) {new NoLayoutManager();
}
}
Grid layout
import java.awt.*;
import javax.swing.*;
public class Grid extends JFrame {/// creo mi clase y extiendo a mi JFrame
JPanel panel = new JPanel(); ///// creo un panel el cual me ayudara a que contenga todos mis botones
///// creo botones invocando a JButton
JButton boton1 = new JButton("Boton 1 ");JButton boton2 = new JButton("Boton 2 ");JButton boton3 = new JButton("Boton 3 ");JButton boton4 = new JButton("Boton 4 ");JButton boton5 = new JButton("Boton 5 ");JButton boton6 = new JButton("Boton 6 ");
public Grid() {// Añado Cada Boton al Panelpanel.add(boton1);panel.add(boton2);panel.add(boton3);panel.add(boton4);panel.add(boton5);panel.add(boton6);
setContentPane(panel); // Añado panel al JFramesetLayout( new GridLayout()); //// ocupo el layout que quiero
ocupar setSize(600,300); /// doy tamaño a mi ventana setTitle("GridLayout"); //// doy un nombre o titulo a mi
ventana setVisible(true); //// y hago que mi ventana sea visible
}
public static void main(String[] args) {new Grid();
}}
Gridbag layout
import java.awt.*;import javax.swing.*;
public class GridBag extends JFrame {static JButton button;public static void main(String[] args) {
GridBag frame = new GridBag();Container container = frame.getContentPane();
GridBagLayout gridbag = new GridBagLayout();GridBagConstraints c = new GridBagConstraints();container.setLayout(gridbag); ////// agrego mi layout
/////c.fill = GridBagConstraints.VERTICAL; ////// especifico la
manera como quiero ver mis botones ya sea horizontal o vertical button = new JButton("boton 1");c.weighty = 0.5; ///// pongo la coordenada la cual deseo ver
mis botones
////////***ESPECIFICO LA COORDENADAS HORIZONTAL O VERTICAL DEL COMPONENTE
c.gridx = 0;c.gridy = 0;
////////////////////////////////gridbag.setConstraints(button, c);container.add(button);button = new JButton("boton 2");c.gridx = 1;c.gridy = 0;gridbag.setConstraints(button, c);container.add(button);button = new JButton("boton 3");c.gridx = 0;c.gridy = 1;gridbag.setConstraints(button, c);container.add(button);button = new JButton("boton 4");c.gridx = 1;c.gridy = 1;gridbag.setConstraints(button, c);
///////////////////////container.add(button);/// añado el button al Jframe que
contiene los botones creadosframe.setSize(400,300);////TAMAÑO DE LA VENTANA PRINCIPAL frame.setTitle("GridBagLayout"); //TITULO DE LA VENTANA
PRINCIPAframe.setVisible(true);//HACEMOS VISIBLE LA VENTANA
}}
PDF LAYOUTS LINKS
LAYOUTS 1 PDF http://www.fdi.ucm.es/profesor/jpavon/poo/tema6resumido.pdf
LAYOUTS 2 PDF file:///F:/TODO%20PROGRAMACION/LINKS%20LAYOUTS/GUIs_AWT_Swing.pdf
LAYOUTS 3 PDFhttp://www-scf.usc.edu/~csci201/lectures/Lecture8.pdf
LAYOUTS 4 PDFhttp://es.slideshare.net/njca01/layouts-java
LAYOUTS 5 PDFhttp://www2.hs-fulda.de/caelabor/inhalte/java/j3d/j3d_seminar/3/Java%20Layoutmanager.pdf
LAYOUTS 6 PDF
http://www.sal.ksu.edu/faculty/tmertz/Java/110GUI/05layoutmanagers.pdf
LAYOUTS 7 http://www2.uah.es/jcaceres/cursos/java/archivos/temas/cursoJava_6.pdf
VIDEOS LINKS
VIDEO 1 https://www.youtube.com/watch?v=mx5eZwlcg78
VIDEO 2 https://www.youtube.com/watch?v=yeac1p6vHws
VIDEO 3 https://www.youtube.com/watch?v=CEzux_141a4&list=PLib8Q64STW-vqRHJyBd6FkKpoWNKJTRI4
VIDEO 4 https://www.youtube.com/watch?v=fzDKzkCJTlE
VIDEO 5 https://www.youtube.com/watch?v=GUO0xe2g5XI