tema03
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tema03 [2026/11/12 13:41] – created mansky | tema03 [2026/11/12 14:01] (current) – [1. Preparar las imágenes] mansky | ||
|---|---|---|---|
| Line 51: | Line 51: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ===== GRIDLAYOUT ===== | ||
| + | |||
| + | GridLayout organiza los componentes en una cuadrícula de filas y columnas. Todas las celdas tienen el mismo tamaño, y los componentes ocupan toda su celda. | ||
| + | |||
| + | Se añaden de izquierda a derecha y de arriba abajo. | ||
| + | |||
| + | <code java> | ||
| + | setLayout(new GridLayout(2, | ||
| + | </ | ||
| + | |||
| + | ^ Parámetro ^ Significado ^ | ||
| + | | 2 | Número de filas | | ||
| + | | 3 | Número de columnas | | ||
| + | | Primer 10 | Separación horizontal | | ||
| + | | Segundo 10 | Separación vertical | | ||
| + | |||
| + | ==== Ejemplo GRIDLAYOUT ==== | ||
| + | |||
| + | <code java> | ||
| + | import java.awt.GridLayout; | ||
| + | import javax.swing.JButton; | ||
| + | import javax.swing.JFrame; | ||
| + | public class EjemploGridLayout extends JFrame { | ||
| + | public EjemploGridLayout() { | ||
| + | setTitle(" | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | // Dos filas y tres columnas. | ||
| + | setLayout(new GridLayout(2, | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | setSize(450, | ||
| + | setLocationRelativeTo(null); | ||
| + | } | ||
| + | public static void main(String[] args) { | ||
| + | EjemploGridLayout ventana = new EjemploGridLayout(); | ||
| + | ventana.setVisible(true); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Para un menú vertical, puedes indicar una columna y tantas filas como sean necesarias: | ||
| + | |||
| + | <code java> | ||
| + | JPanel menu = new JPanel(new GridLayout(0, | ||
| + | menu.add(new JButton(" | ||
| + | menu.add(new JButton(" | ||
| + | menu.add(new JButton(" | ||
| + | </ | ||
| + | |||
| + | El 0 en las filas significa que su número se calcula automáticamente. No pueden ser cero las filas y las columnas a la vez. | ||
| + | |||
| + | Matiz: si fijas un número de filas mayor que cero, GridLayout mantiene esas filas y calcula las columnas según los componentes añadidos. Por ejemplo, con new GridLayout(2, | ||
| + | |||
| + | ===== BORDERLAYOUT ===== | ||
| + | |||
| + | BorderLayout distribuye los componentes en cinco zonas: | ||
| + | |||
| + | ^ Zona ^ Posición ^ Comportamiento ^ | ||
| + | | NORTH | Arriba | Ocupa todo el ancho disponible | | ||
| + | | SOUTH | Abajo | Ocupa todo el ancho disponible | | ||
| + | | WEST | Izquierda | Ocupa el alto entre las zonas superior e inferior | | ||
| + | | EAST | Derecha | Ocupa el alto entre las zonas superior e inferior | | ||
| + | | CENTER | Centro | Ocupa todo el espacio restante | | ||
| + | |||
| + | Las zonas no tienen el mismo tamaño. El centro crece o disminuye al cambiar el tamaño de la ventana. | ||
| + | |||
| + | <code java> | ||
| + | import java.awt.BorderLayout; | ||
| + | import javax.swing.JButton; | ||
| + | import javax.swing.JFrame; | ||
| + | public class EjemploBorderLayout extends JFrame { | ||
| + | public EjemploBorderLayout() { | ||
| + | setTitle(" | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | // Separación horizontal y vertical de 10 píxeles. | ||
| + | setLayout(new BorderLayout(10, | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | add(new JButton(" | ||
| + | setSize(500, | ||
| + | setLocationRelativeTo(null); | ||
| + | } | ||
| + | public static void main(String[] args) { | ||
| + | EjemploBorderLayout ventana = new EjemploBorderLayout(); | ||
| + | ventana.setVisible(true); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | No es obligatorio utilizar todas las zonas. | ||
| + | |||
| + | Si añades un componente sin indicar zona, se coloca en CENTER. | ||
| + | |||
| + | Cada zona admite un componente gestionado. Para colocar varios botones en una misma zona, introdúcelos en un JPanel y añade ese panel a la zona. | ||
| + | |||
| + | El panel de contenido de un JFrame utiliza BorderLayout por defecto. | ||
| + | |||
| + | Por ejemplo, una barra de botones arriba y una tabla en el centro: | ||
| + | |||
| + | <code java> | ||
| + | JPanel botones = new JPanel(new FlowLayout()); | ||
| + | botones.add(new JButton(" | ||
| + | botones.add(new JButton(" | ||
| + | add(botones, | ||
| + | add(new JScrollPane(tabla), | ||
| + | </ | ||
| + | |||
tema03.1794490901.txt.gz · Last modified: by mansky
