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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
import javax.swing.*;
import java.awt.*;
public class TestLayout extends JFrame {
Font font = new Font("Monospaced",Font.PLAIN,12);//changement de font si besoin
public TestLayout() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.CENTER,20,20)); //pour inserer un panel au centre avec 20 pixels de margin H et V
JPanel panel = new JPanel(new BorderLayout(10,20)); //panel principal à inserer 10 pour espacer entre WEST et CENTER, 20 pour espacer entre CENTER et SOUTH
panel.setPreferredSize(new Dimension(340,150));// taille preferee du panel principal
add(panel);
/****creation des éléments****/
JLabel labelPrenom = new JLabel("Prenom");
labelPrenom.setFont(font);
JLabel labelNom = new JLabel("Nom");
labelNom.setFont(font);
JLabel labelSexe = new JLabel("Sexe");
labelSexe.setFont(font);
JLabel labelDate = new JLabel("Date");
labelDate.setFont(font);
JPanel panelTextPrenom = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));//on mets textPrenom dans un JPanel avec LEFT pour coller à gauche et preferredSize fonctionne
JTextField textPrenom = new JTextField();
textPrenom.setPreferredSize(new Dimension(250,20));//taille de textPrenom
textPrenom.setFont(font);
panelTextPrenom.add(textPrenom);
JPanel panelTextNom = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));//pareil qu'au dessus
JTextField textNom = new JTextField();
textNom.setPreferredSize(new Dimension(200,20));
textNom.setFont(font);
panelTextNom.add(textNom);
JRadioButton radioMasculin = new JRadioButton("Masculin");
radioMasculin.setFont(font);
JRadioButton radioFeminin = new JRadioButton("Feminin");
radioFeminin.setFont(font);
JPanel panelRadio = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));//on mets les radio dans un JPanel avec LEFT pour coller à gauche
panelRadio.add(radioMasculin);
panelRadio.add(radioFeminin);
JComboBox comboJour = new JComboBox();
comboJour.setFont(font);
for(int i=1;i<32;i++)
comboJour.addItem(i);
JComboBox comboMois = new JComboBox();
comboMois.setFont(font);
comboMois.addItem("Janvier");
comboMois.addItem("Fevrier");
comboMois.addItem("Mars");
comboMois.addItem("Avril");
comboMois.addItem("Mai");
comboMois.addItem("Juin");
comboMois.addItem("Juillet");
comboMois.addItem("Aout");
comboMois.addItem("Septembre");
comboMois.addItem("Octobre");
comboMois.addItem("Novembre");
comboMois.addItem("Decembre");
JComboBox comboAnnee = new JComboBox();
comboAnnee.setFont(font);
for(int i=1970;i<2014;i++)
comboAnnee.addItem(i);
JPanel panelCombo = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));//on mets les combo dans un JPanel avec LEFT pour coller à gauche
panelCombo.add(comboJour);
panelCombo.add(comboMois);
panelCombo.add(comboAnnee);
/****fin de la création des éléments****/
JPanel panelWest = new JPanel(new GridLayout(4,1));//panelWest à l'ouest du borderlayout. c'est une grille d'une colonne avec 4 lignes pour les labels
panelWest.add(labelPrenom);
panelWest.add(labelNom);
panelWest.add(labelSexe);
panelWest.add(labelDate);
panel.add(panelWest,BorderLayout.WEST);//ajout du panelWest au panel principal
JPanel panelCentre = new JPanel(new GridLayout(4,1));//panelCentre au centre du borderlayout. c'est une grille d'une colonne avec 4 lignes
panelCentre.add(panelTextPrenom);
panelCentre.add(panelTextNom);
panelCentre.add(panelRadio);
panelCentre.add(panelCombo);
panel.add(panelCentre,BorderLayout.CENTER);//ajout du panelCentre au panel principal
JPanel panelSouth = new JPanel(new FlowLayout(FlowLayout.CENTER,60,0));//panel sud pour les 2 boutons, 60 pixels entre chaque
JButton boutonEnregistrer = new JButton("Enregistrer");
JButton boutonAnnuler = new JButton("Annuler");
panelSouth.add(boutonEnregistrer);
panelSouth.add(boutonAnnuler);
panel.add(panelSouth,BorderLayout.SOUTH);//ajout du panelSouth au panel principal
pack();//java propose la meilleure taille en fonction des elements
setVisible(true);
}
public static void main(String[] args) {
TestLayout t = new TestLayout();
}
} |
Partager