3. 2
Introduction
Une interface graphique ou GUI (Graphical User Interface) est:
✓ Une couche applicative destinée à fournir aux utilisateurs d’ordinateurs un
moyen attractif et facile pour interagir avec un programme.
✓ L’interface d’un programme qui profite des capacités graphiques d’un
ordinateur pour le rendre plus facile d’utilisation. Des interfaces graphiques
bien conçues évitent à l’utilisateur d’avoir à apprendre de complexes langages
de commandes.
✓ Un ensemble d’écrans de présentations qui utilisent des éléments graphiques
(tels boutons et icônes) pour rendre un programme plus facile d’utilisation.
4. 3
Paquetages
Le paquetage java.awt : Contient toutes les classes pour créer des interfaces
graphiques et pour dessiner des graphiques et des images .
Le paquetage java.awt.event : Interfaces et classes de gestion des différents
événements associés aux composants AWT.
Le paquetage javax.swing : est un assemblage de composants
(components en anglais) affichés dans une fenêtre ou un autre type de conteneur
(Container). L’utilisateur actionne ces composants pour piloter un programme.
La différence entre AWT et Swing
AWT (Abstract Windowing Toolkit)
➢ 1ère version, a joué un rôle certain pour le succès de Java
➢ toujours utilisée : gestionnaires d’événements et de dispositions
➢ composants lourds, liés à des composants natifs.
Swing
➢ composants beaucoup plus nombreux et complexes composants légers,
100% Java
➢ composants Swing pas tous supportés par les anciennes.
Frame, Panel,
Button,..
JFrame, JPanel,
JButton,..
5. 4
Quelque Composante et
Conteneurs de AWT
• Conteneurs fenêtres :
➢ Frame : Fenêtre avec barre de titre et bords.
• Conteneurs intermédiaires:
➢ Panel : Panneau vide utilisé pour la mise en page des
composants à l'écran.
• Composants de saisie :
➢ TextField : Champ de saisie simple.
• Composants de choix :
➢ Button : Bouton.
6. 5
Les gestionnaires de disposition
le FlowLayout choisi par défaut pour les Applet et Panel, place les
composants de gauche à droite et de haut en bas,
setLayout(new FlowLayout()) indique que le conteneur courant utilise
un gestionnaire FlowLayout, et add(nom du composant) indique que
l’on rajoute le composant cité.
le BorderLayout est choisi par défaut pour les Window, Dialog et
Frame et dispose les composants selon 5 attributs (North, South,
West, East et Center. add() a 2 paramètres : la position et le
composant à rajouter.
le GridLayout met chaque composant sur une case d’une grille dont
on donne la dimension à la création du gestionnaire. Cela se passe de
la même manière que pour un FlowLayout, mais les composants
9. 8
Button b12=new Button("5");
Button b13=new Button("6");
Button b14=new Button("7");
Button b15=new Button("8");
Button b16=new Button("9");
Button b17=new Button(".");
Button b18=new Button("x2");
Button b19=new Button("x3");
Button b20=new Button("1/x");
Button b21=new Button("sqrt");
Button b22=new Button("sin");
Button b23=new Button("cos");
Button b24=new Button("tan");
Button b25=new Button("log");
Button b26=new Button("log10");
Button b27=new Button("exp");
FlowLayout f = new FlowLayout();
// Dimensions de la bouton
Dimension dim=new Dimension(90,40);
// Le constructeur
public Calculatrice (String titre) {
super(titre);
// gestionnaire de la mise en page
setLayout(f);
// On ajoute le texte dans l’interface
add(ecran);
Font font = new Font("Arial", Font.BOLD,20);
// On définit la couleur de texte sur le bouton
b1.setForeground(Color.red);b2.setForeground(Color.red);b3.setForeground(Color.red);
b4.setForeground(Color.red);b5.setForeground(Color.red);b6.setForeground(Color.red);
10. 9
b18.setForeground(Color.green);b19.setForeground(Color.green);
b20.setForeground(Color.green);b21.setForeground(Color.green);
b22.setForeground(Color.green);b23.setForeground(Color.green);
b24.setForeground(Color.green);b25.setForeground(Color.green);
b26.setForeground(Color.green);b27.setForeground(Color.green);
// On ajoute des boutons dans l’interface
add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9);add(b10);
add(b11);add(b12);add(b13);add(b14);add(b15);add(b16);add(b17);add(b18);add(b19);
add(b20);add(b21);add(b22);add(b23);add(b24);add(b25);add(b26);add(b27);
// On inscrit les boutons
b1.addActionListener(this);b2.addActionListener(this b3.addActionListener(this);
b4.addActionListener(this);b5.addActionListener(this);b6.addActionListener(this) ;
b7.addActionListener(this); b8.addActionListener(this);b9.addActionListener(this);
b10.addActionListener(this);b11.addActionListener(this); b12.addActionListener(this);
b13.addActionListener(this);b14.addActionListener(this);b15.addActionListener(this);
b16.addActionListener(this); b17.addActionListener(this);b18.addActionListener(this);
b19.addActionListener(this);b20.addActionListener(this);b21.addActionListener(this);
b22.addActionListener(this);b23.addActionListener(this);b24.addActionListener(this);
b25.addActionListener(this);b26.addActionListener(this);b27.addActionListener(this);
/* On définit la taille des boutons.setFont(font) on définit le type d’écriture et la taille de
l’écriture*/
b1.setPreferredSize(dim);b1.setFont(font);b2.setPreferredSize(dim);b2.setFont(font);
b3.setPreferredSize(dim);b3.setFont(font); b4.setPreferredSize(dim);b4.setFont(font);
b5.setPreferredSize(dim);b5.setFont(font);b6.setPreferredSize(dim);b6.setFont(font);
b7.setPreferredSize(dim);b7.setFont(font);b8.setPreferredSize(dim);b8.setFont(font);
b9.setPreferredSize(dim);b9.setFont(font); b10.setPreferredSize(dim);b10.setFont(font);
b11.setPreferredSize(dim);b11.setFont(font);b12.setPreferredSize(dim);b12.setFont(font);
b13.setPreferredSize(dim);b13.setFont(font);b14.setPreferredSize(dim);b14.setFont(font);
b15.setPreferredSize(dim);b15.setFont(font); b16.setPreferredSize(dim);b16.setFont(font);
b17.setPreferredSize(dim);b17.setFont(font);b18.setPreferredSize(dim);b18.setFont(font);
b19.setPreferredSize(dim);b19.setFont(font);b20.setPreferredSize(dim);b20.setFont(font);
12. 11
}
if (e.getSource()==b7) {
if (z == 0) {
ecran.setText(ecran.getText() + "0");
} else {
ecran.setText("");
// Lecture de bouton saisie (9)
ecran.setText(ecran.getText() + "0");
z = 0;
}
}
if (e.getSource()==b8) {
if (z == 0) {
ecran.setText(ecran.getText() + "1");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "1");
z = 0;
}
}
if (e.getSource()==b9) {
if (z == 0) {
ecran.setText(ecran.getText() + "2");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "2");
z = 0;
}
}
if (e.getSource()==b10) {
if (z == 0) {
13. 12
ecran.setText(ecran.getText() + "3");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "3");
z = 0;
}
}
if (e.getSource()==b11) {
if (z == 0) {
ecran.setText(ecran.getText() + "4");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "4");
z = 0;
}
}
if (e.getSource()==b12) {
if (z == 0) {
ecran.setText(ecran.getText() + "5");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "5");
z = 0;
}
}
if (e.getSource()==b13) {
if (z == 0) {
ecran.setText(ecran.getText() + "6");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "6");
14. 13
z = 0;
}
}
if (e.getSource()==b14) {
if (z == 0) {
ecran.setText(ecran.getText() + "7");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "7");
z = 0;
}
}
if (e.getSource()==b15) {
if (z == 0) {
ecran.setText(ecran.getText() + "8");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "8");
z = 0;
}
}
if (e.getSource()==b16) {
if (z == 0) {
ecran.setText(ecran.getText() + "9");
} else {
ecran.setText("");
ecran.setText(ecran.getText() + "9");
z = 0;
}
}
if (e.getSource()==b17) {
15. 14
if (y == 0) {
ecran.setText(ecran.getText() + ".");
y = 1;
} else {
ecran.setText(ecran.getText());
}
}
if (e.getSource()==b18) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.pow(Double.parseDouble(ecran.getText()), 2);
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b19) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.pow(Double.parseDouble(ecran.getText()), 3);
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b20) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = 1 / Double.parseDouble(ecran.getText());
ecran.setText("");
16. 15
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b21) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.sqrt(Double.parseDouble(ecran.getText()), 3);
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b22) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.sin(Double.parseDouble(ecran.getText()), 3);
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b23) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.cos(Double.parseDouble(ecran.getText()), 3);
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b24) {
17. 16
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.tan(Double.parseDouble(ecran.getText()), 3);
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b25) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.log(Double.parseDouble(ecran.getText()), 3);
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b26) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.log10(Double.parseDouble(ecran.getText()), 3);
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b27) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
a = Math.exp(Double.parseDouble(ecran.getText()), 3);
18. 17
ecran.setText("");
ecran.setText(ecran.getText() + a);
}
}
if (e.getSource()==b6) {
if (ecran.getText().equals("")) {
ecran.setText("");
} else {
temp1 = Double.parseDouble(ecran.getText());
switch (operation) {
case '+':
result = temp + temp1;
break;
case '-':
result = temp - temp1;
break;
case '/':
result = temp / temp1;
break;
case '*':
result = temp * temp1;
break;
}
ecran.setText("");
ecran.setText(ecran.getText() + result);
z=1;
}
}
}
}
public class MiniProjet{
19. 18
public static void main(String args[ ]) {
Calculatrice c= new Calculatrice ("Calculatrice scientifique");
c.setSize(490, 350); //préciser les dimensions de la fenêtre.
c.setResizable(false); /* si false on ne peut pas modifier la fenêtre
si true on peut modifier la fenêtre */
c.setVisible(true); // Rendre le fenêtre visible
}
}