1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import java.awt.*;
import javax.swing.*;
public class Dessin extends JPanel{
private GridBagLayout layout = new GridBagLayout();
private GridBagConstraints gbc= new GridBagConstraints();
public Dessin(){
this.setLayout(layout);
//Configuration du GridBagLayout
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_START;
//Composants
JPanel hg = new JPanel();
JPanel hd = new JPanel();
JPanel b = new JPanel();
//Couleurs des composants
hg.setBackground(Color.red);
hd.setBackground(Color.yellow);
b.setBackground(Color.blue);
this.ajouterComposant(hg, 0,0,1,1,0.7f,1);
this.ajouterComposant(hd, 0,1,1,1,0.3f,1);
this.ajouterComposant( b, 1,0,2,2,1,1);
}
/** Ajoute un composent */
private void ajouterComposant(JComponent composant, int ligne, int colonne,int gridheight,int gridwidth,float wX, float wY){
gbc.gridy = ligne;
gbc.gridx = colonne;
gbc.gridheight = gridheight;
gbc.gridwidth = gridwidth;
gbc.weightx = wX;
gbc.weighty = wY;
layout.setConstraints(composant, gbc);
this.add(composant);
}
public static void main(String[] args){
JFrame f = new JFrame("BagLayout");
f.setSize(300,300);
f.setContentPane(new Dessin());
f.setVisible(true);
}
} |
Partager