Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class GridBagLayoutTest {
- public static void main(String[] args) {
- MyGridBagLayoutTest test = new MyGridBagLayoutTest();
- test.build();
- }
- }
- class MyGridBagLayoutTest extends JFrame{
- JPanel pnlCenter = new JPanel();
- JPanel pnlCenterTopLeft = new JPanel();
- JPanel pnlCenterTopRight = new JPanel();
- JPanel pnlCenterBottomLeft = new JPanel();
- JPanel pnlCenterBottomRight = new JPanel();
- JPanel pnl1 = new JPanel();
- JPanel pnl2 = new JPanel();
- JPanel pnl3 = new JPanel();
- JPanel pnl4 = new JPanel();
- JPanel pnl5 = new JPanel();
- JPanel pnl6 = new JPanel();
- JPanel pnl7 = new JPanel();
- JPanel pnl8 = new JPanel();
- void build(){
- this.setLayout(new BorderLayout());
- pnl1.setBackground(Color.GREEN);
- pnl2.setBackground(Color.BLUE);
- pnl3.setBackground(Color.BLACK);
- pnl4.setBackground(Color.YELLOW);
- pnl5.setBackground(Color.RED);
- pnl6.setBackground(Color.ORANGE);
- pnl7.setBackground(Color.LIGHT_GRAY);
- pnl8.setBackground(Color.PINK);
- pnlCenterTopLeft.setLayout(new GridBagLayout());
- 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 ));
- 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 ));
- 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 ));
- pnlCenterTopRight.setLayout(new GridBagLayout());
- 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 ));
- pnlCenterBottomLeft.setLayout(new GridBagLayout());
- 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));
- 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));
- 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));
- pnlCenterBottomRight.setLayout(new GridBagLayout());
- 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 ));
- pnlCenter.setLayout(new GridBagLayout());
- 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));
- 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));
- 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));
- 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));
- this.setSize(400, 400);
- this.getContentPane().add(pnlCenter);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement