voisin-polian : introduction à java 1 introduction à java - awt - frédéric voisin – nicole...

23
Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Upload: ines-louis

Post on 03-Apr-2015

112 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 1

Introduction à Java

- AWT -

Frédéric VOISIN – Nicole POLIAN

FIIFO - « Remise à Niveau »

Page 2: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 2

Introduction à AWTAWT : Abstract Windowing Toolkit

Ensemble de classes pour créer des interfaces graphiques de types GUI : graphical User Interface

l’AWT créé en premier ne permet pas de faire des interfaces sophistiquées :

a été étendu par JFC (Java Foundation Class) dans le package Swing

AWT : fondement théorique de Swing

gestion des événements semblable

plus rapide

plus simple

Supporté par tous les navigateurs

Page 3: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 3

Introduction à AWT (suite)

3 groupes de classes : graphiques : couleurs, polices, images composants : boutons , label, menus, listes, boites de dialogues, ... Layout-manager : classes pour le positionnement des composants dans les

objets conteneurs

et un package pour le traitement des événements (Event Handling)

pour définir les traitements déclenchés par les événements (clics de souris, saisies clavier, …)

Page 4: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 4

AWT : les éléments de base

Les conteneurs : Frame Panel Window Dialog

Les composants : Button Canvas Label List TextField TextArea FileDialog CheckBox

Les composants de menusMenu

MenuBar

MenuItem

Les aides de mise en placeColor

Font

Cursor

Layout-Manager

Page 5: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 5

Hierarchie des « Widgets »

Object

CheckBoxGroup Component

TextComponent Button Label CheckBox List Choice Container

add(Component)remove(Component)

setLayout(LayoutManager)

TextField ScrollPane Panel Window

Frame Dialog

FileDialog

Page 6: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 6

Une première fenêtre

import java.awt.*;

public class PremFen extends Frame {

Label lab;

public PremFen (){

lab = new Label ( "Bonjour!", Label.CENTER);

lab.setFont(new Font("Helvetica" , Font.BOLD , 18) );

add(lab);

}

public static void main(String[] arg){

PremFen pf= new PremFen();

pf.setSize(200, 100);

pf.setVisible(true);

}

}

Page 7: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 7

Les Layout

threeonetwo

four five

North

South

West EastCenter

one two

four five

three

six

FlowLayout GridLayout

BorderLayout

Page 8: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 8

Exemple 2

public class Fen2 extends Frame { Label lab; Button b; TextField t; Panel p1; public Fen2 (){ setLayout(new GridLayout(0,1)); lab = new Label ("Bonjour!" , Label.CENTER); lab.setBackground(Color.pink); Panel p1 = new Panel( ); p1.setLayout(new FlowLayout(FlowLayout.CENTER,20 ,15)); b = new Button("Saisir"); t = new TextField(15); add(lab); p1.add(b); p1.add(t);add(p1); setFont(new Font("Helvetica" , Font.BOLD , 18) ); } public static void main(String[] arg){ Fen2 f= new Fen2(); f.setTitle ("Exemple 2 "); f.pack(); f.setVisible(true); } }

Page 9: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 9

Exemple3

MenuBar

Label

Checkbox

Button

TextField

Choice

List

Page 10: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 10

Exemple d ’ utilisation de Canvas

public class FenDessin extends Frame { Dessin d; public FenDessin (){ d= new Dessin(); d.setSize(300, 200); add(d); } public static void main(String[] arg){ FenDessin fd= new FenDessin(); fd.pack(); fd.setTitle("exemple de canvas"); fd.setVisible(true); } }

Page 11: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 11

Exemple d ’ Utilisation de Canvas (Suite)Class Dessin extends Canvas { 

public void paint ( Graphics g) {

g.setFont(new Font (« Helvetica » , Font.BOLD, 18) ;

g.drawString("Maison", 20, 20) ;

g.setColor(Color.pink);

g.fillRect(100, 100, 120, 60 ) ;

g.setColor(Color.blue);

g.fillRect(120, 120, 30, 40 ) ;

g.drawOval ( 150, 20, 30, 30 );

}

}

Page 12: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 12

Une image public class FenImage extends Frame { Button b;Label l ;Image im;Canvas c; public FenImage (){ setLayout(new BorderLayout()); l = new Label("le monde"); l.setFont( new Font("Helvetica" , Font.BOLD ,18 )); c = new Canvas(){ public void paint(Graphics g){ im= getToolkit().getImage("globe.gif"); g.drawImage(im , 40 , 10 , Color.white , this); } }; c.setSize(200,200); add("North" ,l); add ( "Center" , c); }

public static void main(String[] arg){…..

Page 13: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 13

Les événements

Page 14: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 14

Les événements AWT

Evénements méthodes

MouseEvent mousePressed,mouseReleased,mouseClicked,mouseEntered, mouseExited

MouseMotionEvent mouseMoved, mouseDragged

KeyEvent keypressed, keyreleased

FocusEvent focusGained, focusLost

ActionEvent actionPerformed

AdjustmentEvent adjustmentValueChanged

ItemEvent itemStateChanged

WindowEvent windowActivated, windowDeactivated,windowIconified, windowDeiconified,windowOpened, windowClosed,windowClosing

Page 15: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 15

Les principaux « Listener »

Les « Listener » sont des Interfaces : une classe qui implémente un Listener doit définir toutes ses méthodes

ActionListener

AdjustementListener

ComponentListener

ContainerEvent

FocusListener

ItemListener

KeyListener

MouseListener

MouseMotionListener

TextListener

WindowListener

Page 16: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 16

Utilisation d ’un Listener

pour définir une action lors du clic sur le bouton bout :

création du Listener :

ActionListener listen = new ActionListener () {

public void actionPerformed(ActionEvent ae) {……..// code}

};

ajout du listener:

bout.addActionListener(listen);

pour l ’enlever:

bout.removeActionListener(listen)

Page 17: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 17

Exemple 4 : utilisation d ’ActionListener

import java.awt.*; import java.awt.event.*;public class Fen4 extends Frame { Button b1,b2 ;Label lab ;Frame f; public Fen4 (){ f=this; b1 = new Button("quitter"); b2 = new Button("coucou!"); lab = new Label( ); lab.setBackground ( Color.pink); setLayout( new FlowLayout()); setFont(new Font("Helvetica" , Font.BOLD , 18) ); add(b1);add(b2);add(lab); ActionListener l1 = new ActionListener(){ public void actionPerformed(ActionEvent a){ System.exit(0);} }; b1.addActionListener(l1); ActionListener l2 = new ActionListener(){ public void actionPerformed(ActionEvent a){ lab.setText ( "COUCOU" ); f.pack(); } } ; b2.addActionListener(l2); } }

Page 18: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 18

Exemple 4 (suite)

Page 19: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 19

Autre possibilité

la Frame est l ’écouteur , elle « implemente » l ’interface ActionListener : elle contient une méthode actionPerformed :

Class Fen4Bis extends Frame implements ActionListener { Button b1,b2 ;Label lab ;Frame f;

public void actionPerformed(ActionEvent a){ if ( a.getSource() == b1 ){ System.exit(0); } else {lab.setText ( "COUCOU" ); f.pack(); } }

public Fen4Bis (){ f=this; b1 = new Button("quitter"); b2 = new Button("coucou!"); ……. add(b1);add(b2);add(lab); b1.addActionListener(this); b2.addActionListener(this); }

Page 20: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 20

Utilisation de WindowListener

Pour utiliser un WindowListener il faut définir toutes les méthodes :

windowActived, windowClosed, ….. (7 méthodes)

la classe abstraite WindowAdapter implémente toutes ces méthodes avec un corps vide. On peut redéfinir une seule (ou plusieurs) des méthodes en dérivant WindowAdapter

Exemple pour provoquer la fin de l ’application en utilisant le menu de la fenêtre

WindowAdapter wa= new WindowAdapter (){

public void windowClosing (WindowEvent we) {

System.exit(0);

}

};

addWindowListener(wa);

Page 21: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 21

Utilisation de MouseListener

Pour gérer les événements souris également on peut utiliser la classe abstraite MouseAdapter qui implémente l ’interface MouseListener de manière à ne pas avoir à définir toutes les méthodes (mouseClicked, MouseEntered,…).

MouseAdapter ma = new MouseAdapter(){

public void mouseClicked(MouseEvent) {

int x= getX() ; int y=getY() // coordonnées de la souris

……

}

};

addMouseListener ( ma);

Page 22: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 22

Exemple 5 : Utilisation de ItemListenerimport java.awt.*; import java.awt.event.* ; import javax.swing.*;public class DeuxFen extends Frame { Button b;TextField t1 ;List liste;Frame f; public DeuxFen (){ f=this; t1 = new TextField(20); liste = new List(3); liste.add("café");liste.add("thé");liste.add("chocolat"); setLayout( new GridLayout(0,1)); add(t1); add(liste); ItemListener il = new ItemListener () { public void itemStateChanged (ItemEvent ie) {

int r=JOptionPane.showConfirmDialog(f,"choix:"+liste.getSelectedItem()

,"choix" , JOptionPane.YES_NO_OPTION);

if (r == JOptionPane.YES_OPTION) t1.setText("choix : "

+liste.getSelectedItem() );

else t1.setText(" "); } }; liste.addItemListener ( il); }

Page 23: Voisin-Polian : Introduction à Java 1 Introduction à Java - AWT - Frédéric VOISIN – Nicole POLIAN FIIFO - « Remise à Niveau »

Voisin-Polian : Introduction à Java 23

Exemple 5 : ItemListener et JOptionPane

Après validation :