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);