SlideShare ist ein Scribd-Unternehmen logo
1 von 7
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
public class DemoGridBag extends JFrame
{
private Container contenedor;
privateGridBagLayoutesquema;
privateGridBagConstraintsrestricciones;
// configurar GUI
publicDemoGridBag()
{
super( "GridBagLayout" );
contenedor = getContentPane();
esquema = new GridBagLayout();
contenedor.setLayout( esquema );
// instanciar restricciones de GridBagLayout
restricciones = new GridBagConstraints();
// crear componentes de GUI
JTextArea areaTexto1 = new JTextArea( "AreaTexto1", 5, 10 );
JTextArea areaTexto2 = new JTextArea( "AreaTexto2", 2, 2 );
Stringnombres[] = { "Hierro", "Acero", "Cobre" };
JComboBoxcuadroCombinado = new JComboBox( nombres );
JTextFieldcampoTexto = new JTextField( "CampoTexto" );
JButton boton1 = new JButton( "survivor horror" );
JButton boton2 = new JButton( "accion" );
JButton boton3 = new JButton( "rol" );
// weightx y weighty para areaTexto1 son 0: el valor predeterminado
// anchor para todos los componentes es CENTER: el valor predeterminado
restricciones.fill = GridBagConstraints.BOTH;
agregarComponente( areaTexto1, 0, 0, 1, 3 );
// weightx y weighty para boton1 son 0: el valor predeterminado
restricciones.fill = GridBagConstraints.HORIZONTAL;
agregarComponente( boton1, 0, 1, 2, 1 );
// weightx y weighty para cuadroCombinado son 0: el valor predeterminado
// fill es HORIZONTAL
agregarComponente( cuadroCombinado, 2, 1, 2, 1 );
// boton2
restricciones.weightx = 1000; // puede hacerse más ancho
restricciones.weighty = 1; // puede hacerse más largo
restricciones.fill = GridBagConstraints.BOTH;
agregarComponente( boton2, 1, 1, 1, 1 );
// fill es BOTH para boton3
restricciones.weightx = 0;
restricciones.weighty = 0;
agregarComponente( boton3, 1, 2, 1, 1 );
// weightx y weighty para campoTexto son 0, fill es BOTH
agregarComponente( campoTexto, 3, 0, 2, 1 );
// weightx y weighty para areaTexto2 son 0, fill es BOTH
agregarComponente( areaTexto2, 3, 2, 1, 1 );
setSize( 300, 150 );
setVisible( true );
} // fin del constructor de DemoGridBag
// método para establecer restricciones
privatevoidagregarComponente( Component componente,
intfila, intcolumna, intanchura, intaltura )
{
// establecergridx y gridy
restricciones.gridx = columna;
restricciones.gridy = fila;
// establecergridwidth y gridheight
restricciones.gridwidth = anchura;
restricciones.gridheight = altura;
// establecer restricciones y agregar componente
esquema.setConstraints( componente, restricciones );
contenedor.add(componente );
}
public static void main( String args[] )
{
DemoGridBagaplicacion = new DemoGridBag();
aplicacion.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
}
importjava.awt.Component;
importjava.awt.Container;
importjava.awt.GridBagConstraints;
importjava.awt.GridBagLayout;
importjava.awt.Insets;
importjavax.swing.JButton;
importjavax.swing.JFrame;
public class GridBagButtons {
private static final Insets insets = new Insets(0, 0, 0, 0);
public static void main(final String args[]) {
finalJFrame frame = new JFrame("GridBagLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
JButton button;
// Row One - Three Buttons
button = new JButton("One");
addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("Two");
addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("Three");
addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
// Row Two - Two Buttons
button = new JButton("Four");
addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("Five");
addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
// Row Three - Two Buttons
button = new JButton("Six");
addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("Seven");
addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
frame.setSize(500, 200);
frame.setVisible(true);
}
private static void addComponent(Container container, Component component, intgridx, intgridy,
intgridwidth, intgridheight, int anchor, int fill) {
GridBagConstraintsgbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
}
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class HolaMundoSwingBorder {
public static void main(String[] args) {
JFrame frame = new JFrame("HolaMundoSwing");
JLabellabel = new JLabel("Hola Mundo");
CompoundBorder borderSelect = new CompoundBorder(new TitledBorder("Mensaje"),new
EmptyBorder(10,20,20,20));
label.setBorder(borderSelect);
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(new Dimension(300, 100));
frame.setVisible(true);
}
}

Weitere ähnliche Inhalte

Was ist angesagt?

The Ring programming language version 1.5.1 book - Part 171 of 180
The Ring programming language version 1.5.1 book - Part 171 of 180 The Ring programming language version 1.5.1 book - Part 171 of 180
The Ring programming language version 1.5.1 book - Part 171 of 180 Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 173 of 189
The Ring programming language version 1.6 book - Part 173 of 189The Ring programming language version 1.6 book - Part 173 of 189
The Ring programming language version 1.6 book - Part 173 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 115 of 210
The Ring programming language version 1.9 book - Part 115 of 210The Ring programming language version 1.9 book - Part 115 of 210
The Ring programming language version 1.9 book - Part 115 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 109 of 212
The Ring programming language version 1.10 book - Part 109 of 212The Ring programming language version 1.10 book - Part 109 of 212
The Ring programming language version 1.10 book - Part 109 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 179 of 189
The Ring programming language version 1.6 book - Part 179 of 189The Ring programming language version 1.6 book - Part 179 of 189
The Ring programming language version 1.6 book - Part 179 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 113 of 212
The Ring programming language version 1.10 book - Part 113 of 212The Ring programming language version 1.10 book - Part 113 of 212
The Ring programming language version 1.10 book - Part 113 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 174 of 189
The Ring programming language version 1.6 book - Part 174 of 189The Ring programming language version 1.6 book - Part 174 of 189
The Ring programming language version 1.6 book - Part 174 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 77 of 88
The Ring programming language version 1.3 book - Part 77 of 88The Ring programming language version 1.3 book - Part 77 of 88
The Ring programming language version 1.3 book - Part 77 of 88Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 75 of 88
The Ring programming language version 1.3 book - Part 75 of 88The Ring programming language version 1.3 book - Part 75 of 88
The Ring programming language version 1.3 book - Part 75 of 88Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 27 of 30
The Ring programming language version 1.4 book - Part 27 of 30The Ring programming language version 1.4 book - Part 27 of 30
The Ring programming language version 1.4 book - Part 27 of 30Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 168 of 181
The Ring programming language version 1.5.2 book - Part 168 of 181The Ring programming language version 1.5.2 book - Part 168 of 181
The Ring programming language version 1.5.2 book - Part 168 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 167 of 181
The Ring programming language version 1.5.2 book - Part 167 of 181The Ring programming language version 1.5.2 book - Part 167 of 181
The Ring programming language version 1.5.2 book - Part 167 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 168 of 189
The Ring programming language version 1.6 book - Part 168 of 189The Ring programming language version 1.6 book - Part 168 of 189
The Ring programming language version 1.6 book - Part 168 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 71 of 84
The Ring programming language version 1.2 book - Part 71 of 84The Ring programming language version 1.2 book - Part 71 of 84
The Ring programming language version 1.2 book - Part 71 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 29 of 31
The Ring programming language version 1.5 book - Part 29 of 31The Ring programming language version 1.5 book - Part 29 of 31
The Ring programming language version 1.5 book - Part 29 of 31Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 170 of 189
The Ring programming language version 1.6 book - Part 170 of 189The Ring programming language version 1.6 book - Part 170 of 189
The Ring programming language version 1.6 book - Part 170 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 104 of 202
The Ring programming language version 1.8 book - Part 104 of 202The Ring programming language version 1.8 book - Part 104 of 202
The Ring programming language version 1.8 book - Part 104 of 202Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 111 of 196
The Ring programming language version 1.7 book - Part 111 of 196The Ring programming language version 1.7 book - Part 111 of 196
The Ring programming language version 1.7 book - Part 111 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 72 of 84
The Ring programming language version 1.2 book - Part 72 of 84The Ring programming language version 1.2 book - Part 72 of 84
The Ring programming language version 1.2 book - Part 72 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 180 of 189
The Ring programming language version 1.6 book - Part 180 of 189The Ring programming language version 1.6 book - Part 180 of 189
The Ring programming language version 1.6 book - Part 180 of 189Mahmoud Samir Fayed
 

Was ist angesagt? (20)

The Ring programming language version 1.5.1 book - Part 171 of 180
The Ring programming language version 1.5.1 book - Part 171 of 180 The Ring programming language version 1.5.1 book - Part 171 of 180
The Ring programming language version 1.5.1 book - Part 171 of 180
 
The Ring programming language version 1.6 book - Part 173 of 189
The Ring programming language version 1.6 book - Part 173 of 189The Ring programming language version 1.6 book - Part 173 of 189
The Ring programming language version 1.6 book - Part 173 of 189
 
The Ring programming language version 1.9 book - Part 115 of 210
The Ring programming language version 1.9 book - Part 115 of 210The Ring programming language version 1.9 book - Part 115 of 210
The Ring programming language version 1.9 book - Part 115 of 210
 
The Ring programming language version 1.10 book - Part 109 of 212
The Ring programming language version 1.10 book - Part 109 of 212The Ring programming language version 1.10 book - Part 109 of 212
The Ring programming language version 1.10 book - Part 109 of 212
 
The Ring programming language version 1.6 book - Part 179 of 189
The Ring programming language version 1.6 book - Part 179 of 189The Ring programming language version 1.6 book - Part 179 of 189
The Ring programming language version 1.6 book - Part 179 of 189
 
The Ring programming language version 1.10 book - Part 113 of 212
The Ring programming language version 1.10 book - Part 113 of 212The Ring programming language version 1.10 book - Part 113 of 212
The Ring programming language version 1.10 book - Part 113 of 212
 
The Ring programming language version 1.6 book - Part 174 of 189
The Ring programming language version 1.6 book - Part 174 of 189The Ring programming language version 1.6 book - Part 174 of 189
The Ring programming language version 1.6 book - Part 174 of 189
 
The Ring programming language version 1.3 book - Part 77 of 88
The Ring programming language version 1.3 book - Part 77 of 88The Ring programming language version 1.3 book - Part 77 of 88
The Ring programming language version 1.3 book - Part 77 of 88
 
The Ring programming language version 1.3 book - Part 75 of 88
The Ring programming language version 1.3 book - Part 75 of 88The Ring programming language version 1.3 book - Part 75 of 88
The Ring programming language version 1.3 book - Part 75 of 88
 
The Ring programming language version 1.4 book - Part 27 of 30
The Ring programming language version 1.4 book - Part 27 of 30The Ring programming language version 1.4 book - Part 27 of 30
The Ring programming language version 1.4 book - Part 27 of 30
 
The Ring programming language version 1.5.2 book - Part 168 of 181
The Ring programming language version 1.5.2 book - Part 168 of 181The Ring programming language version 1.5.2 book - Part 168 of 181
The Ring programming language version 1.5.2 book - Part 168 of 181
 
The Ring programming language version 1.5.2 book - Part 167 of 181
The Ring programming language version 1.5.2 book - Part 167 of 181The Ring programming language version 1.5.2 book - Part 167 of 181
The Ring programming language version 1.5.2 book - Part 167 of 181
 
The Ring programming language version 1.6 book - Part 168 of 189
The Ring programming language version 1.6 book - Part 168 of 189The Ring programming language version 1.6 book - Part 168 of 189
The Ring programming language version 1.6 book - Part 168 of 189
 
The Ring programming language version 1.2 book - Part 71 of 84
The Ring programming language version 1.2 book - Part 71 of 84The Ring programming language version 1.2 book - Part 71 of 84
The Ring programming language version 1.2 book - Part 71 of 84
 
The Ring programming language version 1.5 book - Part 29 of 31
The Ring programming language version 1.5 book - Part 29 of 31The Ring programming language version 1.5 book - Part 29 of 31
The Ring programming language version 1.5 book - Part 29 of 31
 
The Ring programming language version 1.6 book - Part 170 of 189
The Ring programming language version 1.6 book - Part 170 of 189The Ring programming language version 1.6 book - Part 170 of 189
The Ring programming language version 1.6 book - Part 170 of 189
 
The Ring programming language version 1.8 book - Part 104 of 202
The Ring programming language version 1.8 book - Part 104 of 202The Ring programming language version 1.8 book - Part 104 of 202
The Ring programming language version 1.8 book - Part 104 of 202
 
The Ring programming language version 1.7 book - Part 111 of 196
The Ring programming language version 1.7 book - Part 111 of 196The Ring programming language version 1.7 book - Part 111 of 196
The Ring programming language version 1.7 book - Part 111 of 196
 
The Ring programming language version 1.2 book - Part 72 of 84
The Ring programming language version 1.2 book - Part 72 of 84The Ring programming language version 1.2 book - Part 72 of 84
The Ring programming language version 1.2 book - Part 72 of 84
 
The Ring programming language version 1.6 book - Part 180 of 189
The Ring programming language version 1.6 book - Part 180 of 189The Ring programming language version 1.6 book - Part 180 of 189
The Ring programming language version 1.6 book - Part 180 of 189
 

Mehr von Řỉgö VẻGầ (20)

Configuración servidores DCHP, DNS y HTTP - Cisco Packet Tracer
Configuración servidores DCHP, DNS y HTTP - Cisco Packet TracerConfiguración servidores DCHP, DNS y HTTP - Cisco Packet Tracer
Configuración servidores DCHP, DNS y HTTP - Cisco Packet Tracer
 
Pratica1
Pratica1Pratica1
Pratica1
 
Laboratorio 4 1 introping
Laboratorio 4 1 intropingLaboratorio 4 1 introping
Laboratorio 4 1 introping
 
Laboratorio 3 mascara de subred
Laboratorio 3 mascara de subredLaboratorio 3 mascara de subred
Laboratorio 3 mascara de subred
 
Encuesta
EncuestaEncuesta
Encuesta
 
Taller 5
Taller 5Taller 5
Taller 5
 
Taller 4
Taller 4Taller 4
Taller 4
 
Taller parcial
Taller parcialTaller parcial
Taller parcial
 
Taller interfaz 3
Taller interfaz 3Taller interfaz 3
Taller interfaz 3
 
Taller interfaz 2
Taller interfaz 2Taller interfaz 2
Taller interfaz 2
 
Interfaz Grafica En Java
Interfaz Grafica En JavaInterfaz Grafica En Java
Interfaz Grafica En Java
 
Taller de string(java)
Taller de string(java)Taller de string(java)
Taller de string(java)
 
Hechos en clase
Hechos en claseHechos en clase
Hechos en clase
 
Taller1
Taller1Taller1
Taller1
 
Taller Matrices En Java
Taller Matrices En JavaTaller Matrices En Java
Taller Matrices En Java
 
Historia de la computacion, tipos de lenguaje de programacion e historia
Historia de la computacion, tipos de lenguaje de programacion e historiaHistoria de la computacion, tipos de lenguaje de programacion e historia
Historia de la computacion, tipos de lenguaje de programacion e historia
 
Ventajas y desventajas de las redes sociales
Ventajas y desventajas de las redes socialesVentajas y desventajas de las redes sociales
Ventajas y desventajas de las redes sociales
 
Ventajas y desventajas de las redes sociales
Ventajas y desventajas de las redes socialesVentajas y desventajas de las redes sociales
Ventajas y desventajas de las redes sociales
 
Leyes
LeyesLeyes
Leyes
 
Como fabricar queso
Como fabricar quesoComo fabricar queso
Como fabricar queso
 

Taller 6

  • 1. importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; public class DemoGridBag extends JFrame { private Container contenedor; privateGridBagLayoutesquema; privateGridBagConstraintsrestricciones; // configurar GUI publicDemoGridBag() { super( "GridBagLayout" ); contenedor = getContentPane(); esquema = new GridBagLayout(); contenedor.setLayout( esquema ); // instanciar restricciones de GridBagLayout restricciones = new GridBagConstraints(); // crear componentes de GUI JTextArea areaTexto1 = new JTextArea( "AreaTexto1", 5, 10 ); JTextArea areaTexto2 = new JTextArea( "AreaTexto2", 2, 2 );
  • 2. Stringnombres[] = { "Hierro", "Acero", "Cobre" }; JComboBoxcuadroCombinado = new JComboBox( nombres ); JTextFieldcampoTexto = new JTextField( "CampoTexto" ); JButton boton1 = new JButton( "survivor horror" ); JButton boton2 = new JButton( "accion" ); JButton boton3 = new JButton( "rol" ); // weightx y weighty para areaTexto1 son 0: el valor predeterminado // anchor para todos los componentes es CENTER: el valor predeterminado restricciones.fill = GridBagConstraints.BOTH; agregarComponente( areaTexto1, 0, 0, 1, 3 ); // weightx y weighty para boton1 son 0: el valor predeterminado restricciones.fill = GridBagConstraints.HORIZONTAL; agregarComponente( boton1, 0, 1, 2, 1 ); // weightx y weighty para cuadroCombinado son 0: el valor predeterminado // fill es HORIZONTAL agregarComponente( cuadroCombinado, 2, 1, 2, 1 ); // boton2 restricciones.weightx = 1000; // puede hacerse más ancho restricciones.weighty = 1; // puede hacerse más largo restricciones.fill = GridBagConstraints.BOTH; agregarComponente( boton2, 1, 1, 1, 1 );
  • 3. // fill es BOTH para boton3 restricciones.weightx = 0; restricciones.weighty = 0; agregarComponente( boton3, 1, 2, 1, 1 ); // weightx y weighty para campoTexto son 0, fill es BOTH agregarComponente( campoTexto, 3, 0, 2, 1 ); // weightx y weighty para areaTexto2 son 0, fill es BOTH agregarComponente( areaTexto2, 3, 2, 1, 1 ); setSize( 300, 150 ); setVisible( true ); } // fin del constructor de DemoGridBag // método para establecer restricciones privatevoidagregarComponente( Component componente, intfila, intcolumna, intanchura, intaltura ) { // establecergridx y gridy restricciones.gridx = columna; restricciones.gridy = fila; // establecergridwidth y gridheight
  • 4. restricciones.gridwidth = anchura; restricciones.gridheight = altura; // establecer restricciones y agregar componente esquema.setConstraints( componente, restricciones ); contenedor.add(componente ); } public static void main( String args[] ) { DemoGridBagaplicacion = new DemoGridBag(); aplicacion.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); } } importjava.awt.Component; importjava.awt.Container; importjava.awt.GridBagConstraints; importjava.awt.GridBagLayout; importjava.awt.Insets; importjavax.swing.JButton; importjavax.swing.JFrame; public class GridBagButtons {
  • 5. private static final Insets insets = new Insets(0, 0, 0, 0); public static void main(final String args[]) { finalJFrame frame = new JFrame("GridBagLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridBagLayout()); JButton button; // Row One - Three Buttons button = new JButton("One"); addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); button = new JButton("Two"); addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); button = new JButton("Three"); addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); // Row Two - Two Buttons button = new JButton("Four"); addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); button = new JButton("Five"); addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH); // Row Three - Two Buttons button = new JButton("Six"); addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); button = new JButton("Seven"); addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); frame.setSize(500, 200); frame.setVisible(true);
  • 6. } private static void addComponent(Container container, Component component, intgridx, intgridy, intgridwidth, intgridheight, int anchor, int fill) { GridBagConstraintsgbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0); container.add(component, gbc); } } import javax.swing.*; import javax.swing.border.*; import java.awt.*; public class HolaMundoSwingBorder { public static void main(String[] args) { JFrame frame = new JFrame("HolaMundoSwing"); JLabellabel = new JLabel("Hola Mundo"); CompoundBorder borderSelect = new CompoundBorder(new TitledBorder("Mensaje"),new EmptyBorder(10,20,20,20)); label.setBorder(borderSelect); frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);