Le blog de Bastien

Le blog de Bastien

Clear/Restore/Changement de couleur

Ce programme avait pour but d'effacer, de faire réapparaitre, de changer de couleur un mot.

 

Ecouteur2.java

 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ecouteur2 implements ActionListener {
    private Imbric2 fen;
    private int choix;
    public Ecouteur2(int menu, Imbric2 f1){ // un seul ecouteur pour 2 évènements
        choix = menu;
        fen = f1;
    }
    
    public void actionPerformed (ActionEvent e){
        switch(choix){
        case 1 :
            //Appelle la fonction qui permettra d'effacer le mot Coucou
            fen.efface();
        break;
        case 2 :
            fen.restore();//Permet de réinitialiser le mot Coucou
        break;
        case 3 :
            fen.rouge();//Change la couleur du mot Coucou
        break;
        case 4 :
            fen.jaune();
        break;
        case 5 :
            fen.bleu();
        break;
        case 6 :
            fen.vert();
        break;
        case 7 :
            fen.rose();
        break;
        }
    }
}


Imbric2.java

 

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Imbric2 extends JFrame {
    private JLabel lab;
    public Imbric2( String t ){
        super (t);
        BorderLayout bl;
        bl = new BorderLayout();
        
        this.setLayout(bl);
        
        lab = new JLabel("Coucou");
        
        
        JButton bout;
        bout = new JButton("Clear");
        this.add(bout);
        
        JButton bout2;
        bout2 = new JButton("Restore");
        this.add(bout2);
        
        JButton bout3;
        bout3 = new JButton("Rouge");
        this.add(bout3);
        
        JButton bout4;
        bout4 = new JButton("Jaune");
        this.add(bout4);
        
        JButton bout5;
        bout5 = new JButton("Bleu");
        this.add(bout5);
        
        JButton bout6;
        bout6 = new JButton("Vert");
        this.add(bout6);
        
        JButton bout7;
        bout7 = new JButton("Rose");
        this.add(bout7);
        
        Ecouteur2 ec = new Ecouteur2(1, this);// même écouteur mais le choix est différent selon le bouton 1 ou 2
        bout.addActionListener(ec);
        
        Ecouteur2 ec2 = new Ecouteur2(2, this);
        bout2.addActionListener(ec2);
        
        Ecouteur2 ec3 = new Ecouteur2(3, this);
        bout3.addActionListener(ec3);
        
        Ecouteur2 ec4 = new Ecouteur2(4, this);
        bout4.addActionListener(ec4);
        
        Ecouteur2 ec5 = new Ecouteur2(5, this);
        bout5.addActionListener(ec5);
        
        Ecouteur2 ec6 = new Ecouteur2(6, this);
        bout6.addActionListener(ec6);
        
        Ecouteur2 ec7 = new Ecouteur2(7, this);
        bout7.addActionListener(ec7);
            
        
        lab.setHorizontalAlignment(JLabel.CENTER);
        
        JPanel pan;//placement du bouton clear
        pan = new JPanel();
        FlowLayout fl;
        fl = new FlowLayout();
        pan.setLayout(fl);
        pan.add(bout);
        
        JPanel pan2;//placement du bouton restore
        pan2 = new JPanel();
        FlowLayout fl2;
        fl2 = new FlowLayout();
        pan.setLayout(fl2);
        pan.add(bout2);
        
        JPanel pan3;//placement du bouton rouge
        pan3 = new JPanel();
        FlowLayout fl3;
        fl3 = new FlowLayout();
        pan.setLayout(fl3);
        pan.add(bout3);
        
        JPanel pan4;//placement du bouton jaune
        pan4 = new JPanel();
        FlowLayout fl4;
        fl4 = new FlowLayout();
        pan.setLayout(fl4);
        pan.add(bout4);
        
        JPanel pan5;//placement du bouton bleu
        pan5 = new JPanel();
        FlowLayout fl5;
        fl5 = new FlowLayout();
        pan.setLayout(fl5);
        pan.add(bout5);
        
        JPanel pan6;//placement du bouton vert
        pan6 = new JPanel();
        FlowLayout fl6;
        fl6 = new FlowLayout();
        pan.setLayout(fl6);
        pan.add(bout6);
        
        JPanel pan7;//placement du bouton rose
        pan6 = new JPanel();
        FlowLayout fl7;
        fl7 = new FlowLayout();
        pan.setLayout(fl7);
        pan.add(bout7);
        
        
        this.add( lab, BorderLayout.NORTH );
        
    
        this.add( pan, BorderLayout.SOUTH );
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);

        
    }
    public void rose(){
        lab.setForeground(Color.PINK);
    }
    public void jaune(){
        lab.setForeground(Color.YELLOW);
    }
    
    public void bleu(){
        lab.setForeground(Color.BLUE);
    }
    
    public void vert(){
        lab.setForeground(Color.GREEN);
    }
    
    public void rouge(){
        lab.setForeground(Color.RED);
    }
    public void efface(){
        /*mettre comme nouveau texte dans le label ""
        1/ comment mettre un texte dans un label */
        lab.setText("");
            
    }
    public void restore(){
        lab.setText("Coucou");
        lab.setForeground(Color.BLACK);
        
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Imbric2 fen;
        fen = new Imbric2("Clear/Restore/ChangeColor");
    
        
    }

}



22/05/2013
0 Poster un commentaire

A découvrir aussi


Ces blogs de Informatique & Internet pourraient vous intéresser

Inscrivez-vous au blog

Soyez prévenu par email des prochaines mises à jour