Interfaces Graficas Profesor: Rodrigo Cruzat
Contenedores La ventana misma, que contiene botones, texto, etc… y donde se realizará la interacción JFrame JDialog
Componentes JLabel ! JLabel label = new Label("Esto es una etiqueta"); JButton – JButton boton = new Button("Esto es un boton"); TextField – JTextField texto = new JTextField("Texto por defecto");
Componentes JComboBox – JComboBox combo = new JComboBox(); – combo.addItem("Opcion 4"); – combo.removeItem("Opciones") JRadioButton – JRadioButton[] radioButtons = new JRadioButton[3]; – ButtonGroup group = new ButtonGroup(); – radioButtons[0] = new JRadioButton("Opcion 1"); – radioButtons[1] = new JRadioButton("Opcion 2"); – radioButtons[2] = new JRadioButton("Opcion 3"); – for (int i = 0; i < 3; i++) { – group.add(radioButtons[i]); – }
Administradores de Layout (Layout Managers) Encargados de especificar el orden en que se muestran los componentes Los principales son: – BorderLayout: Pone cada elemento en una coordenada cardinal (North, South, East, West). – FlowLayout: Inserta los elementos de izquierda a derecha – GridLayout: Se define una grilla, en la cual se insertan los elementos por linea.
Layout Managers
Layout managers
Layout Managers
Distribuciones más complejas?
Distribuciones Mas complejas
Cómo combinar distribuciones? Panel: objeto para agrupar componentes – Panel p = new Panel(); Le damos un Layout a un Panel – p.setLayout(new GridLayout(1,4)); Agregamos objetos al Panel – p.add(…); Le damos el Layout al Frame – setLayout(new BorderLayout()); Agregamos el Panel al Frame – getConentPane().add(“South”,p);
Una Ventana import java.awt.*; import javax.swing.*; class NOMBRE extends JFrame { ACA DECLARAR COMPONENTES public NOMBRE(){ ACA CREAR COMPONENTES SET LAYOUT AGREGAR COMPONENTES pack(); setVisible(true); } public static void main(String[] args){ new NOMBRE(); }
Ejemplos