Bon jour,
j'ai essayée de programmer une calculatrice en java mais j'ai un problème l'ors de l'exécution ,tel que si je laisse le programme comme ci dessous elle marche seulement l'opération active ,et si j'active tous les opérations uniquement l'opération de la soustraction qui marche,SVP aidez moi de resoudre ce problème,merci.
voila le programme:
package tp15;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Calculatrice extends JFrame implements ActionListener {
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
private JButton b10;
private JButton b11;
private JButton b12;
private JButton b13;
private JButton b14;
private JButton b15;
private JButton b16;
private JButton b17;
private JTextField z;
// temp est la mémoire de la calculatrice
private String temp=null;
private int o1,o2;
public Calculatrice(String nom,int l,int h)
{
this.setLayout(null);
this.setTitle(nom);
this.setBounds(0,0,h,l);
z= new JTextField(" ");
z.setBounds(30,30,184,35);
this.add(z);
b1 =new JButton("7");
b1.setBounds(30,67,48,20);
this.add(b1);
b2 =new JButton("4");
b2.setBounds(30,89,48,20);
this.add(b2);
b3 =new JButton("1");
b3.setBounds(30,111,48,20);
this.add(b3);
b13 =new JButton("0");
b13.setBounds(30,133,48,20);
this.add(b13);
b5 =new JButton("8");
b5.setBounds(75,67,48,20);
this.add(b5);
b6 =new JButton("5");
b6.setBounds(75,89,48,20);
this.add(b6);
b7 =new JButton("2");
b7.setBounds(75,111,48,20);
this.add(b7);
b14 =new JButton("+/-");
b14.setBounds(75,133,48,20);
this.add(b14);
b4 =new JButton("9");
b4.setBounds(120,67,48,20);
this.add(b4);
b8 =new JButton("6");
b8.setBounds(120,89,48,20);
this.add(b8);
b9 =new JButton("3");
b9.setBounds(120,111,48,20);
this.add(b9);
b15 =new JButton("*");
b15.setBounds(120,133,48,20);
this.add(b15);
b10 =new JButton("+");
b10.setBounds(165,67,48,20);
this.add(b10);
b11 =new JButton("-");
b11.setBounds(165,89,48,20);
this.add(b11);
b12 =new JButton("=");
b12.setBounds(165,111,48,20);
this.add(b12);
b16 =new JButton("/");
b16.setBounds(165,133,48,20);
this.add(b16);
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);
this.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource().equals(b1))
{
z.setText(z.getText()+b1.getText());
}else
if(a.getSource().equals(b2))
{
z.setText(z.getText()+b2.getText());
}else
if(a.getSource().equals(b3))
{
z.setText(z.getText()+b3.getText());
}else
if(a.getSource().equals(b4))
{
z.setText(z.getText()+b4.getText());
}else
if(a.getSource().equals(b5))
{
z.setText(z.getText()+b5.getText());
}else
if(a.getSource().equals(b6))
{
z.setText(z.getText()+b6.getText());
}else
if(a.getSource().equals(b7))
{
z.setText(z.getText()+b7.getText());
}else
if(a.getSource().equals(b8))
{
z.setText(z.getText()+b8.getText());
}else
if(a.getSource().equals(b9))
{
z.setText(z.getText()+b9.getText());
}else
if(a.getSource().equals(b10))
{
temp=z.getText();
o1=new Double (temp).intValue();
z.setText(null);
}else
if(a.getSource().equals(b15))
{
temp=z.getText();
o1=new Double (temp).intValue();
z.setText(null);
}else
if(a.getSource().equals(b16))
{
temp=z.getText();
o1=new Double (temp).intValue();
z.setText(null);
}else
if(a.getSource().equals(b11))
{
temp=z.getText();
o1=new Double (temp).intValue();
z.setText(null);
}else
if(a.getSource().equals(b13))
{
z.setText(z.getText()+b13.getText());
}else
if(a.getSource().equals(b14))
{
z.setText("-");
}else
if(a.getSource().equals(b12))
{
o2=new Double (z.getText()).intValue();
int res1=o1+o2;
int res2=o1-o2;
//int res3=o1*o2;
//int res4=o1/o2;
z.setText(String.valueOf(res1));
//z.setText(String.valueOf(res2));
}
}
}
//la classe Application
package tp15;
public class Application {
public static void main(String args[])
{
Calculatrice b=new Calculatrice("Fenetre",600,600);
}
}
Afficher la suite