Advertisement
Guest User

GridBagLayoutTest

a guest
Nov 17th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10.  
  11. public class GridBagLayoutTest {
  12.     public static void main(String[] args) {
  13.         MyGridBagLayoutTest test = new MyGridBagLayoutTest();
  14.         test.build();
  15.     }
  16. }
  17.  
  18. class MyGridBagLayoutTest extends JFrame{
  19.     JPanel pnlCenter = new JPanel();
  20.     JPanel pnlCenterTopLeft = new JPanel();
  21.     JPanel pnlCenterTopRight = new JPanel();
  22.     JPanel pnlCenterBottomLeft = new JPanel();
  23.     JPanel pnlCenterBottomRight = new JPanel();
  24.     JPanel pnl1 = new JPanel();
  25.     JPanel pnl2 = new JPanel();
  26.     JPanel pnl3 = new JPanel();
  27.     JPanel pnl4 = new JPanel();
  28.     JPanel pnl5 = new JPanel();
  29.     JPanel pnl6 = new JPanel();
  30.     JPanel pnl7 = new JPanel();
  31.     JPanel pnl8 = new JPanel();
  32.  
  33.     void build(){
  34.         this.setLayout(new BorderLayout());
  35.  
  36.         pnl1.setBackground(Color.GREEN);
  37.         pnl2.setBackground(Color.BLUE);
  38.         pnl3.setBackground(Color.BLACK);
  39.         pnl4.setBackground(Color.YELLOW);
  40.         pnl5.setBackground(Color.RED);
  41.         pnl6.setBackground(Color.ORANGE);
  42.         pnl7.setBackground(Color.LIGHT_GRAY);
  43.         pnl8.setBackground(Color.PINK);
  44.  
  45.         pnlCenterTopLeft.setLayout(new GridBagLayout());
  46.         pnlCenterTopLeft.add(pnl1, new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,0,0),0,0 ));
  47.         pnlCenterTopLeft.add(pnl2, new GridBagConstraints(1,0,1,1,1.0,1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(5,5,0,0),0,0 ));
  48.         pnlCenterTopLeft.add(pnl3, new GridBagConstraints(0,1,GridBagConstraints.REMAINDER,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5,5,0,0),0,0 ));
  49.  
  50.         pnlCenterTopRight.setLayout(new GridBagLayout());
  51.         pnlCenterTopRight.add(pnl4, new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5,5,0,0),0,0 ));
  52.  
  53.         pnlCenterBottomLeft.setLayout(new GridBagLayout());
  54.         pnlCenterBottomLeft.add(pnl5, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5,5,0,0), 0, 0));
  55.         pnlCenterBottomLeft.add(pnl6, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5,5,0,0), 0, 0));
  56.         pnlCenterBottomLeft.add(pnl7, new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5,5,0,0), 0, 0));
  57.  
  58.         pnlCenterBottomRight.setLayout(new GridBagLayout());
  59.         pnlCenterBottomRight.add(pnl8, new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5,5,0,0),0,0 ));
  60.  
  61.         pnlCenter.setLayout(new GridBagLayout());
  62.         pnlCenter.add(pnlCenterTopLeft, new GridBagConstraints(0,0,1,1,0.5,0.7, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 0, 0),0,0));
  63.         pnlCenter.add(pnlCenterTopRight, new GridBagConstraints(1,0,1,1,0.5,0.7, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 0, 0),0,0));
  64.         pnlCenter.add(pnlCenterBottomLeft, new GridBagConstraints(0,1,1,1,0.5,0.3, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 0, 0),0,0));
  65.         pnlCenter.add(pnlCenterBottomRight, new GridBagConstraints(1,1,1,1,0.5,0.3, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 0, 0),0,0));
  66.  
  67.         this.setSize(400, 400);
  68.         this.getContentPane().add(pnlCenter);
  69.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.         this.setVisible(true);
  71.  
  72.     }
  73. }
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement