import java.awt.*;
import java.awt.event.*;

public class Exo1{
    String titre = new String("On fait joujou!");
    
    Label toto = new Label("Toto!");
    TextField tutu = new TextField (26);
    Button quit = new Button("Quitter");

    Frame f = new Frame(titre);
    
    public Exo1(){
	f.add(tutu, BorderLayout.SOUTH);
	f.add(toto, BorderLayout.WEST);
	f.add(quit, BorderLayout.EAST);
	quit.addActionListener(new ActionListener(){
	    public void actionPerformed (ActionEvent evt){
		if (evt.getSource() instanceof Button) {
		    System.exit(0);
		}
	    }
	});
	f.pack();
	f.show();
    }
    public static void main(String args[]){
	new Exo1();
    }
}


public class Exo2{
    String titre = new String("On recommence!");
    
    Button un = new Button("1");
    Button deux = new Button("2");
    Button trois = new Button("3");
    Button quatre = new Button("4");
    Button cinq = new Button("5");
    Button quit = new Button("Quitter");

    Frame f = new Frame(titre);

    void BuildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
	gbc.gridx = gx;
	gbc.gridy = gy;
	gbc.gridwidth = gw;
	gbc.gridheight = gh;
	gbc.weightx = wx;
	gbc.weighty = wy;
    }
    
    public Exo2(){
	GridBagLayout gbl = new GridBagLayout();
	GridBagConstraints gbc = new GridBagConstraints();
	f.setLayout(gbl);
	
	BuildConstraints(gbc, 0, 0, 1, 1, 1, 1);
	gbl.setConstraints(un, gbc);
	f.add(un);
	
	BuildConstraints(gbc, 1, 0, 1, 1, 1, 1);
	gbl.setConstraints(deux, gbc);
	f.add(deux);

	BuildConstraints(gbc, 0, 1, 1, 1, 1, 1);
	gbl.setConstraints(trois, gbc);
	f.add(trois);

	BuildConstraints(gbc, 1, 1, 1, 1, 1, 1);
	gbl.setConstraints(quatre, gbc);
	f.add(quatre);
	
	BuildConstraints(gbc, 2, 0, 1, 2, 1, 1);
	gbc.fill = GridBagConstraints.BOTH;
	gbc.anchor = GridBagConstraints.CENTER;
	gbl.setConstraints(cinq, gbc);
	f.add(cinq);
	
	BuildConstraints(gbc, 0, 2, 3, 1, 1, 1);
	gbc.fill = GridBagConstraints.BOTH;
	gbc.anchor = GridBagConstraints.CENTER;
	gbl.setConstraints(quit, gbc);
	f.add(quit);

	quit.addActionListener(new ActionListener(){
	    public void actionPerformed (ActionEvent evt){
		if (evt.getSource() instanceof Button) {
		    System.exit(0);
		}
	    }
	});
	f.pack();
	f.show();
    }
    public static void main(String args[]){
	new Exo2();
    }
}

public class Exo3 extends Frame implements ActionListener{
    String titre = new String("Grille de 9 boutons");
    
    Button un = new Button("1");
    Button deux = new Button("2");
    Button trois = new Button("3");
    Button quatre = new Button("4");
    Button cinq = new Button("5");
    Button six = new Button("6");
    Button sept = new Button("7");
    Button huit = new Button("8");
    Button neuf = new Button("9");

    TextField t = new TextField(20);
    Panel p = new Panel();
    
    public Exo3(){
	setTitle(titre);

	GridLayout grid = new GridLayout(3,3);
	p.setLayout(grid);

	p.add(un);
	p.add(deux);
	p.add(trois);
	p.add(quatre);
	p.add(cinq);
	p.add(six);
	p.add(sept);
	p.add(huit);
	p.add(neuf);
	un.addActionListener(this);
	deux.addActionListener(this);
	trois.addActionListener(this);
	quatre.addActionListener(this);
	cinq.addActionListener(this);
	six.addActionListener(this);
	sept.addActionListener(this);
	huit.addActionListener(this);
	neuf.addActionListener(this);
	
	add(p, BorderLayout.NORTH);
	add(t, BorderLayout.CENTER);
	pack();
	show();
    }
    
    public void actionPerformed (ActionEvent evt){
		if (evt.getSource() instanceof Button) {
		    Button c = (Button)evt.getSource();
		    String x = c.getLabel();
		    if (x.equals("3")) {
			System.exit(0);
		    }
		    else
			{ 
			t.setText("Au boulot! numero " + x);
			}
		}
    }

    public static void main(String args[]){
	new Exo3();
    }
}

public class Exo4 extends Frame implements ActionListener{
    String titre = new String("Grille de 9 boutons");
    
    Button un = new Button("1");
    Button deux = new Button("2");
    Button trois = new Button("3");
    Button quatre = new Button("4");
    Button cinq = new Button("5");
    Button six = new Button("6");
    Button sept = new Button("7");
    Button huit = new Button("8");
    Button neuf = new Button("9");

    Button quit = new Button("Quitter");
    Button mes = new Button("Message");

    TextField t = new TextField(20);
    TextArea a = new TextArea();
    
    Panel p1 = new Panel();
    Panel p2 = new Panel();
    
    public Exo4(){
	setTitle(titre);

	GridLayout grid = new GridLayout(3,3);
	p1.setLayout(grid);

	p1.add(un);
	p1.add(deux);
	p1.add(trois);
	p1.add(quatre);
	p1.add(cinq);
	p1.add(six);
	p1.add(sept);
	p1.add(huit);
	p1.add(neuf);
	un.addActionListener(this);
	deux.addActionListener(this);
	trois.addActionListener(this);
	quatre.addActionListener(this);
	cinq.addActionListener(this);
	six.addActionListener(this);
	sept.addActionListener(this);
	huit.addActionListener(this);
	neuf.addActionListener(this);
	
	add(p1, BorderLayout.NORTH);
	add(a, BorderLayout.CENTER);

	p2.add(t);
	p2.add(mes);
	p2.add(quit);
	mes.addActionListener(this);
	quit.addActionListener(this);
	t.addKeyListener(new KeyEventHandler());

	add(p2, BorderLayout.SOUTH);

	pack();
	show();
    }
    
    public void actionPerformed (ActionEvent evt){
		if (evt.getSource() instanceof Button) {
		    Button c = (Button)evt.getSource();
		    String x = c.getLabel();
		    if (x.equals("Quitter")) {
			System.exit(0);
		    }
		    else
			{ if (x.equals("Message")) { 
			    a.append("Petit message \n");}
			else 
			a.append("Au boulot! numero " + x + "\n");
			}
		}
    }

    class KeyEventHandler extends KeyAdapter {
	public void keyPressed (KeyEvent evt){
	    if(evt.getKeyCode() == KeyEvent.VK_ENTER)
		{a.append(t.getText()+"\n");
		t.setText("");
		}
	}
    }

    public static void main(String args[]){
	new Exo4();
    }
}

