Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package botonesappletgridbaglayout;
  2.  
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import javax.swing.JApplet;
  6. import javax.swing.JButton;
  7.  
  8. /**
  9.  *
  10.  * @author Rafa
  11.  */
  12. public class BotonesAppletGridBagLayout extends JApplet{
  13.     GridBagLayout gridbag;
  14.     GridBagConstraints gridbagconst;
  15.    
  16.     //Declara los botones
  17.     private JButton boton1, boton2, boton3, boton4;
  18.     private JButton boton5, boton6, boton7, boton8;
  19.    
  20.    
  21.     public void init(){
  22.         gridbag = new GridBagLayout();
  23.         gridbagconst = new GridBagConstraints();    
  24.        
  25.         //Crea los 8 botones
  26.         boton1 = new JButton("Boton1");
  27.         boton2 = new JButton("Boton2");
  28.         boton3 = new JButton("Boton3");
  29.         boton4 = new JButton("Boton4");
  30.         boton5 = new JButton("Boton5");
  31.         boton6 = new JButton("Boton6");
  32.         boton7 = new JButton("Boton7");
  33.         boton8 = new JButton("Boton8");
  34.        
  35.         //establecemos la composicion
  36.         setLayout(gridbag);
  37.        
  38.         //boton1
  39.         gridbagconst.fill = GridBagConstraints.BELOW_BASELINE_LEADING;
  40.         gridbag.setConstraints(boton1, gridbagconst);
  41.         add(boton1);
  42.        
  43.         //boton2
  44.         gridbag.setConstraints(boton2, gridbagconst);
  45.         add(boton2);
  46.        
  47.         //boton3
  48.         gridbag.setConstraints(boton3, gridbagconst);
  49.         add(boton3);
  50.        
  51.         //boton4
  52.         gridbagconst.gridwidth = GridBagConstraints.RELATIVE;
  53.         gridbag.setConstraints(boton4, gridbagconst);
  54.         add(boton4);
  55.        
  56.         //boton5
  57.         gridbagconst.gridwidth = GridBagConstraints.RELATIVE;
  58.         gridbag.setConstraints(boton5, gridbagconst);
  59.         add(boton5);
  60.        
  61.         //boton6
  62.         gridbagconst.gridwidth = GridBagConstraints.REMAINDER;
  63.         gridbag.setConstraints(boton6, gridbagconst);
  64.         add(boton6);
  65.  
  66.         //Boton7
  67.         gridbagconst.fill = GridBagConstraints.BELOW_BASELINE_TRAILING;
  68.         gridbag.setConstraints(boton7, gridbagconst);
  69.         add(boton7);
  70.        
  71.         //Boton8
  72.         gridbagconst.gridwidth = GridBagConstraints.FIRST_LINE_START;
  73.         gridbag.setConstraints(boton8, gridbagconst);
  74.         add(boton8);
  75.                        
  76.     }
  77. }