You are on page 1of 6

ISIMM UE: Programmation Java

Département informatique

Correction du TP n°3- programmation graphique

Enseignant : Dr ABBASSI Imed

Correction de l’Exercice 1 :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Addition extends JFrame implements ActionListener
{
JComboBox<String> operande1 = new JComboBox();
JComboBox<String> operande2 = new JComboBox();
JLabel plus = new JLabel(" + ");
JLabel egale= new JLabel(" = ");
JTextField res = new JTextField("0");
JButton ok = new JButton("Calculer");
public Addition(String s){
super(s);
operande1.addItem("0");
operande1.addItem("1");
operande1.addItem("2");
operande1.addItem("3");
operande1.addItem("4");
operande2.addItem("0");
operande2.addItem("1");
operande2.addItem("2");
operande2.addItem("3");
operande2.addItem("4");
JPanel p1= new JPanel();
p1.add(operande1);
p1.add(plus);
p1.add(operande2);
p1.add(egale);
p1.add(res);
this.add(p1,BorderLayout.NORTH);
this.add(ok, BorderLayout.CENTER);
ok.addActionListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(200,120);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e){
JButton b=(JButton)e.getSource();
if(b==ok){
int x=Integer.parseInt((String)operande1.getSelectedItem());
int y=Integer.parseInt((String)operande2.getSelectedItem());
res.setText(x+y+"");
}

1
}
}

public class Main{


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

Correction de l’Exercice 2 :
import javax.swing.* ;
import java.awt.event.* ;
class MaFenetre extends JFrame implements ActionListener, ItemListener{
private JButton boutRaz=new JButton ("RAZ") ;;
private JButton boutEtat=boutEtat = new JButton ("Etat") ;
private JPanel pan=new JPanel () ; // Par défaut FlowLayout ;
private JCheckBox cercle = new JCheckBox ("Cercle") ;
private JCheckBox rectangle = new JCheckBox ("Rectangle") ;
private JCheckBox triangle = new JCheckBox ("Triangle") ;
public MaFenetre (){
super("Cases a cocher") ;
setSize (300, 140) ;
setVisible(true) ;
boutRaz.addActionListener (this) ;
add (boutRaz, "North") ;
boutEtat.addActionListener (this) ;
add (boutEtat, "South") ;
add (pan) ;
pan.add (cercle) ;
cercle.addActionListener (this) ;
cercle.addItemListener (this) ;
pan.add (rectangle) ;
rectangle.addActionListener (this) ;
rectangle.addItemListener (this) ;
pan.add (triangle) ;
triangle.addActionListener (this) ;
triangle.addItemListener (this) ;
}
public void actionPerformed (ActionEvent e)
{
Object source = e.getSource() ;
if (source == boutRaz){
cercle.setSelected (false) ;
rectangle.setSelected (false) ;
triangle.setSelected (false) ;
}
if (source == boutEtat){
System.out.print ("Cases selectionnees : ") ;
if (cercle.isSelected()) System.out.print (" cercle ") ;
if (rectangle.isSelected()) System.out.print (" rectangle ") ;
if (triangle.isSelected()) System.out.print (" triangle ") ;
}
if (source == cercle) System.out.println ("Action case cercle") ;

2
if (source == rectangle) System.out.println ("Action case rectangle");
if (source == triangle) System.out.println ("Action case triangle") ;
}
public void itemStateChanged (ItemEvent e)
{
Object source = e.getSource() ;
if (source == cercle) System.out.println ("Item case cercle") ;
if (source == rectangle) System.out.println ("Item case rectangle") ;
if (source == triangle) System.out.println ("Item case triangle") ;
}
}
public class Main
{
public static void main (String args[]){
new MaFenetre() ;

}
}

Correction de l’Exercice 3 :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Calculatrice extends JFrame implements ActionListener
{
JButton b0=new JButton("0");
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton moin=new JButton("-");
JButton plus=new JButton("+");
JButton fois=new JButton("*");
JButton div=new JButton("/");
JButton poum=new JButton("+/-");
JButton all=new JButton("Clear All");
JButton bEntre=new JButton("Entrer");
JTextField t=new JTextField("0");
boolean status=false;
String operation=" ";
int res;
Calculatrice(String s)
{
super(s);
JPanel pTouche=new JPanel();
pTouche.setLayout(new GridLayout(4,3));
pTouche.add(b7);
pTouche.add(b8);
pTouche.add(b9);

3
pTouche.add(b4);
pTouche.add(b5);
pTouche.add(b6);
pTouche.add(b1);
pTouche.add(b2);
pTouche.add(b3);
pTouche.add(b0);
pTouche.add(poum);
pTouche.add(all);

JPanel pOpera=new JPanel(new GridLayout(4,1));


pOpera.add(plus);
pOpera.add(moin);
pOpera.add(fois);
pOpera.add(div);
setLayout(new BorderLayout());
add(t,"North");
add(pTouche,"Center");
add(pOpera,"East");
add(bEntre,"South");

/* les evennement.................*/
b0.addActionListener(this);
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);
plus.addActionListener(this);
moin.addActionListener(this);
fois.addActionListener(this);
poum.addActionListener(this);
div.addActionListener(this);
bEntre.addActionListener(this);
all.addActionListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
JButton b=(JButton)e.getSource();

if(b==b0||b==b1||b==b2||b==b3||b==b4||b==b5||b==b6||b==b7||b==b8||b==
b9)
{
String s=b.getText();
if(status==false){
if(t.getText().equals("0")) t.setText(s);
else t.setText(t.getText()+s);
}
else

4
{
t.setText(s);
status=false;
}
}
else if(b==fois||b==plus||b==moin||b==div){
res=Integer.parseInt(t.getText());
status=true;
operation=b.getText();
}
else if(b==bEntre){
if(operation=="+"){
res=res+Integer.parseInt(t.getText());
t.setText(res+"");
status=true;
operation="";
}
else if(operation=="*"){
res=res*Integer.parseInt(t.getText());
t.setText(res+"");
status=true;
operation="";
}
else if(operation=="-"){
res=res-Integer.parseInt(t.getText());
t.setText(res+"");
status=true;
operation="";
}
else if(operation=="/"){
res=res/Integer.parseInt(t.getText());
t.setText(res+"");
status=true;
operation="";
}
}
else if(b==all){
t.setText("0");
res=0;
status=false;
operation=" ";
}
else if(b==poum){
int x=0-Integer.parseInt(t.getText());
t.setText(x+"");
status=false;
}
}

public class testCalculatrice{


public static void main(String[] args)
{

5
Calculatrice cal=new Calculatrice("CALCULATRICE");
cal.setSize(300,300);
cal.setVisible(true);

}
}

You might also like