×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Pierre Cosse
Added: Jul 3, 2015 2:04 PM
Modified: Jul 3, 2015 2:05 PM
Views: 1898
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package graphisme;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Dimension;
  10. import java.awt.GridBagConstraints;
  11. import java.awt.GridBagLayout;
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14.  
  15. /**
  16.  *
  17.  * @author pierre
  18.  */
  19. public class Fenetre extends JFrame
  20. {
  21.  
  22.     public Fenetre()
  23.     {
  24.         this.setTitle("GridBagLayout");
  25.         this.setSize(300, 160);
  26.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         this.setLocationRelativeTo(null);
  28.  
  29.         //On crée nos différents conteneurs de couleur différente
  30.         JPanel cell1 = new JPanel();
  31.         cell1.setBackground(Color.YELLOW);
  32.         cell1.setPreferredSize(new Dimension(60, 40));
  33.         JPanel cell2 = new JPanel();
  34.         cell2.setBackground(Color.red);
  35.         cell2.setPreferredSize(new Dimension(60, 40));
  36.         JPanel cell3 = new JPanel();
  37.         cell3.setBackground(Color.green);
  38.         cell3.setPreferredSize(new Dimension(60, 40));
  39.         JPanel cell4 = new JPanel();
  40.         cell4.setBackground(Color.black);
  41.         cell4.setPreferredSize(new Dimension(60, 40));
  42.         JPanel cell5 = new JPanel();
  43.         cell5.setBackground(Color.cyan);
  44.         cell5.setPreferredSize(new Dimension(60, 40));
  45.         JPanel cell6 = new JPanel();
  46.         cell6.setBackground(Color.BLUE);
  47.         cell6.setPreferredSize(new Dimension(60, 40));
  48.         JPanel cell7 = new JPanel();
  49.         cell7.setBackground(Color.orange);
  50.         cell7.setPreferredSize(new Dimension(60, 40));
  51.         JPanel cell8 = new JPanel();
  52.         cell8.setBackground(Color.DARK_GRAY);
  53.         cell8.setPreferredSize(new Dimension(60, 40));
  54.  
  55.         //Le conteneur principal
  56.         JPanel content = new JPanel();
  57.         content.setPreferredSize(new Dimension(300, 120));
  58.         content.setBackground(Color.WHITE);
  59.         //On définit le layout manager
  60.         content.setLayout(new GridBagLayout());
  61.  
  62.         //L'objet servant à positionner les composants
  63.         GridBagConstraints gbc = new GridBagConstraints();
  64.  
  65.         //On positionne la case de départ du composant
  66.         gbc.gridx = 0;
  67.         gbc.gridy = 0;
  68.         //La taille en hauteur et en largeur
  69.         gbc.gridheight = 1;
  70.         gbc.gridwidth = 1;
  71.         content.add(cell1, gbc);
  72.         //---------------------------------------------
  73.         gbc.gridx = 1;
  74.         content.add(cell2, gbc);
  75.         //---------------------------------------------
  76.         gbc.gridx = 2;
  77.         content.add(cell3, gbc);
  78.     //---------------------------------------------
  79.         //Cette instruction informe le layout que c'est une fin de ligne
  80.         gbc.gridwidth = GridBagConstraints.REMAINDER;
  81.         gbc.gridx = 3;
  82.         content.add(cell4, gbc);
  83.         //---------------------------------------------
  84.         gbc.gridx = 0;
  85.         gbc.gridy = 1;
  86.         gbc.gridwidth = 1;
  87.         gbc.gridheight = 2;
  88.         //Celle-ci indique que la cellule se réplique de façon verticale
  89.         gbc.fill = GridBagConstraints.VERTICAL;
  90.         content.add(cell5, gbc);
  91.         //---------------------------------------------
  92.         gbc.gridx = 1;
  93.         gbc.gridheight = 1;
  94.         //Celle-ci indique que la cellule se réplique de façon horizontale
  95.         gbc.fill = GridBagConstraints.HORIZONTAL;
  96.         gbc.gridwidth = GridBagConstraints.REMAINDER;
  97.         content.add(cell6, gbc);
  98.         //---------------------------------------------
  99.         gbc.gridx = 1;
  100.         gbc.gridy = 2;
  101.         gbc.gridwidth = 2;
  102.         content.add(cell7, gbc);
  103.         //---------------------------------------------
  104.         gbc.gridx = 3;
  105.         gbc.gridwidth = GridBagConstraints.REMAINDER;
  106.         content.add(cell8, gbc);
  107.     //---------------------------------------------
  108.         //On ajoute le conteneur
  109.         this.setContentPane(content);
  110.         this.setVisible(true);
  111.     }
  112. }
  113.