importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;public class DemoGridBag extends JFrame{private Container cont...
Stringnombres[] = { "Hierro", "Acero", "Cobre" };JComboBoxcuadroCombinado = new JComboBox( nombres );JTextFieldcampoTexto ...
// fill es BOTH para boton3restricciones.weightx = 0;restricciones.weighty = 0;agregarComponente( boton3, 1, 2, 1, 1 );// ...
restricciones.gridwidth = anchura;restricciones.gridheight = altura;// establecer restricciones y agregar componenteesquem...
private static final Insets insets = new Insets(0, 0, 0, 0);public static void main(final String args[]) {finalJFrame fram...
}private static void addComponent(Container container, Component component, intgridx, intgridy,intgridwidth, intgridheight...
frame.pack();frame.setSize(new Dimension(300, 100));frame.setVisible(true);}}
Upcoming SlideShare
Loading in …5
×

Taller 6

187 views

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
187
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Taller 6

  1. 1. importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;public class DemoGridBag extends JFrame{private Container contenedor;privateGridBagLayoutesquema;privateGridBagConstraintsrestricciones;// configurar GUIpublicDemoGridBag(){super( "GridBagLayout" );contenedor = getContentPane();esquema = new GridBagLayout();contenedor.setLayout( esquema );// instanciar restricciones de GridBagLayoutrestricciones = new GridBagConstraints();// crear componentes de GUIJTextArea areaTexto1 = new JTextArea( "AreaTexto1", 5, 10 );JTextArea areaTexto2 = new JTextArea( "AreaTexto2", 2, 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 predeterminadorestricciones.fill = GridBagConstraints.BOTH;agregarComponente( areaTexto1, 0, 0, 1, 3 );// weightx y weighty para boton1 son 0: el valor predeterminadorestricciones.fill = GridBagConstraints.HORIZONTAL;agregarComponente( boton1, 0, 1, 2, 1 );// weightx y weighty para cuadroCombinado son 0: el valor predeterminado// fill es HORIZONTALagregarComponente( cuadroCombinado, 2, 1, 2, 1 );// boton2restricciones.weightx = 1000; // puede hacerse más anchorestricciones.weighty = 1; // puede hacerse más largorestricciones.fill = GridBagConstraints.BOTH;agregarComponente( boton2, 1, 1, 1, 1 );
  3. 3. // fill es BOTH para boton3restricciones.weightx = 0;restricciones.weighty = 0;agregarComponente( boton3, 1, 2, 1, 1 );// weightx y weighty para campoTexto son 0, fill es BOTHagregarComponente( campoTexto, 3, 0, 2, 1 );// weightx y weighty para areaTexto2 son 0, fill es BOTHagregarComponente( areaTexto2, 3, 2, 1, 1 );setSize( 300, 150 );setVisible( true );} // fin del constructor de DemoGridBag// método para establecer restriccionesprivatevoidagregarComponente( Component componente,intfila, intcolumna, intanchura, intaltura ){// establecergridx y gridyrestricciones.gridx = columna;restricciones.gridy = fila;// establecergridwidth y gridheight
  4. 4. restricciones.gridwidth = anchura;restricciones.gridheight = altura;// establecer restricciones y agregar componenteesquema.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. 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 Buttonsbutton = 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 Buttonsbutton = 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 Buttonsbutton = 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. 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"),newEmptyBorder(10,20,20,20));label.setBorder(borderSelect);frame.getContentPane().add(label);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7. 7. frame.pack();frame.setSize(new Dimension(300, 100));frame.setVisible(true);}}

×