Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Grid Bag Layout


Code ResourceAuthor: srinivasaraobora  

Difficulty Level:

Published: 8th Nov 2006   Read: 2913 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This is useful for who learn about Gridbag layout

 


Sample Code
  1.  
  2. import java.awt.*;
  3. import javax.swing.*;
  4. class GridDemo1 extends JFrame
  5. {
  6.         GridBagLayout gbag;
  7.         GridBagConstraints cons;
  8.         GridDemo1()
  9.         {
  10.                 Container c = getContentPane();
  11.                 gbag = new GridBagLayout();
  12.                 c.setLayout(gbag);
  13.                 cons = new GridBagConstraints();
  14.                 JButton b1 = new JButton("Button1");
  15.         JButton b2 = new JButton("Button2");
  16.         JButton b3 = new JButton("Button3");
  17.         JButton b4 = new JButton("Button4");
  18.         JButton b5 = new JButton("Button5");
  19.  
  20.                 cons.fill = GridBagConstraints.HORIZONTAL;
  21.                 cons.gridx = 0;
  22.         cons.gridy = 0;
  23.                 cons.weightx = 1.0;
  24.                 gbag.setConstraints(b1,cons);
  25.                 c.add(b1);
  26.  
  27.                 cons.gridx = 2;
  28.                 cons.gridy = 0;
  29.                 gbag.setConstraints(b2,cons);
  30.                 c.add(b2);
  31.  
  32.                 cons.gridx = 1;
  33.                 cons.gridy = 0;
  34.                 gbag.setConstraints(b3,cons);
  35.                 c.add(b3);
  36.  
  37.                 cons.gridx = 0;
  38.                 cons.gridy = 1;
  39.                 cons.ipady = 100;
  40.                 cons.weighty = 0.0;
  41.                 cons.gridwidth = 3;
  42.                 gbag.setConstraints(b4,cons);
  43.                 c.add(b4);
  44.        
  45.                 cons.gridx = 1;
  46.                 cons.gridy = 2;
  47.                 cons.ipady = 0;
  48.                 cons.weighty = .7;
  49.                 cons.anchor = GridBagConstraints.PAGE_END;
  50.                 cons.insets = new Insets(10,0,0,0);
  51.                 cons.gridwidth = 2;
  52.                 gbag.setConstraints(b5,cons);
  53.                 c.add(b5);
  54.         }
  55.         public static void main(String args[])
  56.         {
  57.                 GridDemo1 demo = new GridDemo1();
  58.           demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.           demo.setSize(600,500);
  60.                   demo.setTitle("grid");
  61.                   demo.show();
  62.         }
  63.  
  64. }
  65.  
Copyright GeekInterview.com


Next Article: Java puzzles


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora130575
2. iamdvr97635
3. venki_madesh35132
4. Raju18459
5. parmod kumar duhan15743
6. Kiran.jakkaraju14037
7. chowsys7448
8. Venkateswara Rao6469
9. ashish.cns6342
10. Vamshidhar Matam5424

Active Coders

Refined Tags