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
|
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JLabel;
public class Fenetre extends JFrame {
private JPanel container = new JPanel();
private JPanel containerAffichage = new JPanel();
private JPanel containerChiffres = new JPanel();
private JPanel containerOperateurs = new JPanel();
private GridBagLayout GL4x3 = new GridBagLayout();
private GridBagLayout GL5x1 = new GridBagLayout();
//private GridLayout gl3x3 = new GridLayout();
private JLabel mylabel = new JLabel("0");
public Fenetre(){
// création de la fenetre
this.setTitle("Calculette");
this.setSize(240, 260);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComposant();
this.setContentPane(container);
this.setVisible(true);
}
private void initComposant(){
// définition des layout managers
// definition des paramètres des GridBagConstraints
GridBagConstraints gbc[][] = new GridBagConstraints[5][3];
for (int j=0; j<5; j++){
for (int i=0; i<3; i++){
gbc[j][i] = new GridBagConstraints();
gbc[j][i].gridx = i;
gbc[j][i].gridy = j;
gbc[j][i].anchor = GridBagConstraints.CENTER; // alignement des composants
gbc[j][i].fill = GridBagConstraints.BOTH; // taille que prennent les composants
gbc[j][i].insets = new Insets(5, 5, 5, 5);
}
}
container.setLayout(new BorderLayout());
containerChiffres.setLayout(GL4x3);
containerOperateurs.setLayout(GL5x1);
container.add(containerAffichage, BorderLayout.NORTH);
container.add(containerOperateurs, BorderLayout.EAST);
container.add(containerChiffres, BorderLayout.CENTER);
// mise en forme des containers
containerChiffres.setBackground(Color.BLUE);
// création des boutons
containerChiffres.add(new JButton("1"), gbc[0][0]);
containerChiffres.add(new JButton("2"), gbc[0][1]);
containerChiffres.add(new JButton("3"), gbc[0][2]);
containerChiffres.add(new JButton("4"), gbc[1][0]);
containerChiffres.add(new JButton("5"), gbc[1][1]);
containerChiffres.add(new JButton("6"), gbc[1][2]);
containerChiffres.add(new JButton("7"), gbc[2][0]);
containerChiffres.add(new JButton("8"), gbc[2][1]);
containerChiffres.add(new JButton("9"), gbc[2][2]);
containerChiffres.add(new JButton("."), gbc[3][0]);
containerChiffres.add(new JButton("0"), gbc[3][1]);
containerChiffres.add(new JButton("="), gbc[3][2]);
containerOperateurs.add(new JButton("+"), gbc[0][0]);
containerOperateurs.add(new JButton("-"), gbc[1][0]);
containerOperateurs.add(new JButton("*"), gbc[2][0]);
containerOperateurs.add(new JButton("/"), gbc[3][0]);
containerOperateurs.add(new JButton("C"), gbc[4][0]);
// création des Labels
containerAffichage.add(mylabel);
}
} |
Partager