Comment activez les boutons d'une calculatrice en java swing?

Résolu
olfahdidi Messages postés 1 Date d'inscription jeudi 27 novembre 2014 Statut Membre Dernière intervention 27 novembre 2014 - Modifié par Whismeril le 8/12/2014 à 11:45
Whismeril Messages postés 19020 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 15 avril 2024 - 8 déc. 2014 à 11:46
j'ai fait cette calculatrice mais elle ne fonctionne pas et je ne sais pas pourquoi.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class cal2 extends JFrame implements ActionListener,ItemListener
{   
 JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b,bexp,bc,bcl,bMult,bDiv,bSom,bSoust;
 JButton bRes,bOff,bSqrt,bLog,bFact,bSin,bCos;
 JButton bTan,bPuis;
 
 ButtonGroup bg1,bg2;
 JPanel pTxt,p2,p3;
 JTextField txt;
 double xp=1,xs=0,aux=0;
 boolean op=false,mult=false,div=false,som=false,soust=false,init=true,virg=false;
    cal2()
 {
 super("Calculatrice olfa");
 b0=new JButton("0");b1=new JButton("1");b2=new JButton("2");
 b3=new JButton("3");b4=new JButton("4");b5=new JButton("5");
 b6=new JButton("6");b7=new JButton("7");b8=new JButton("8");
 b9=new JButton("9");b=new JButton(".");bexp=new JButton("Pi");
 bMult=new JButton("*");
 bDiv=new JButton("/");bSom=new JButton("+");bSoust=new JButton("-");
 bRes=new JButton("=");txt =new JTextField("0");
 bSqrt=new JButton("Rac");bLog=new JButton("Ln/Log");
 bFact=new JButton("n!");
 bSin=new JButton("Sin");bCos=new JButton("Cos");bTan=new JButton("Tan");
 bPuis=new JButton("Y^X");bc=new JButton("C");
 bOff=new JButton("OFF");bcl=new JButton("Del");
 bg1=new ButtonGroup();bg2=new ButtonGroup();
 b0.setPreferredSize(new Dimension(80,30));b1.setPreferredSize(new Dimension(80,30));
 b2.setPreferredSize(new Dimension(80,30));b3.setPreferredSize(new Dimension(80,30));
 b4.setPreferredSize(new Dimension(80,30));b5.setPreferredSize(new Dimension(80,30));
 b6.setPreferredSize(new Dimension(80,30));b7.setPreferredSize(new Dimension(80,30));
 b8.setPreferredSize(new Dimension(80,30));b9.setPreferredSize(new Dimension(80,30));
 b.setPreferredSize(new Dimension(80,30));bexp.setPreferredSize(new Dimension(80,30));
 bDiv.setPreferredSize(new Dimension(80,30));bSom.setPreferredSize(new Dimension(80,30));
    bSoust.setPreferredSize(new Dimension(80,30));bRes.setPreferredSize(new Dimension(165,30));
 bMult.setPreferredSize(new Dimension(80,30));
 bSqrt.setPreferredSize(new Dimension(80,30));bLog.setPreferredSize(new Dimension(80,30));
 bFact.setPreferredSize(new Dimension(80,30));
 bCos.setPreferredSize(new Dimension(80,30));bPuis.setPreferredSize(new Dimension(80,30));
 bSin.setPreferredSize(new Dimension(80,30));bc.setPreferredSize(new Dimension(80,30));
 bOff.setPreferredSize(new Dimension(80,30));bcl.setPreferredSize(new Dimension(80,30));
 bTan.setPreferredSize(new Dimension(80,30));txt.setPreferredSize(new Dimension(440,35));
 txt.setHorizontalAlignment(SwingConstants.RIGHT);
 txt.setBackground(Color.black);txt.setForeground(Color.GREEN);
 txt.setFont(new Font("DIALOG",Font.CENTER_BASELINE+Font.BOLD,12));
 JPanel pNorm=new JPanel();
 pNorm.add(b7);pNorm.add(b8);pNorm.add(b9);pNorm.add(bexp);pNorm.add(bSqrt);
 pNorm.add(b4);pNorm.add(b5);pNorm.add(b6);pNorm.add(bMult);pNorm.add(bDiv);
 pNorm.add(b1);pNorm.add(b2);pNorm.add(b3);pNorm.add(bSom);pNorm.add(bSoust);
 pNorm.add(b0);pNorm.add(b);pNorm.add(bRes);
 p2=new JPanel(new GridLayout(1,1));p2.add(pNorm);
 JPanel pStd=new JPanel();pStd.add(bcl);
 pStd.add(bc);pStd.add(bOff);
 JPanel pSci=new JPanel();
 pSci.add(bPuis);
 pSci.add(bFact);pSci.add(bLog);pSci.add(bSin);pSci.add(bCos);pSci.add(bTan);
 p3 =new JPanel(new GridLayout(1,1)); p3.add(pSci);
 pTxt=new JPanel();pTxt.add(txt);pTxt.add(pStd);
    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);b.addActionListener(this);bexp.addActionListener(this);
    bMult.addActionListener(this);
    bDiv.addActionListener(this);bSom.addActionListener(this);bSoust.addActionListener(this);
    bRes.addActionListener(this);bc.addActionListener(this);
    bcl.addActionListener(this);bOff.addActionListener(this);bSqrt.addActionListener(this);
    bLog.addActionListener(this);
    bFact.addActionListener(this);bSin.addActionListener(this);
    bCos.addActionListener(this);bTan.addActionListener(this);bPuis.addActionListener(this);
    this.setSize(new Dimension(452,331));
    this.getContentPane().setLayout(new GridLayout(2,1));
    this.getContentPane().add(pTxt);this.getContentPane().add(p2);
    this.setLocation(300,100);
    this.setCursor(12);this.setResizable(true);this.show();
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
   
    void activateP2P3(boolean v)
    {
    b0.setEnabled(v);b1.setEnabled(v);b2.setEnabled(v);b3.setEnabled(v);
    b4.setEnabled(v);b5.setEnabled(v);b6.setEnabled(v);b7.setEnabled(v);
    b8.setEnabled(v);b9.setEnabled(v);b.setEnabled(v);bexp.setEnabled(v);
    bMult.setEnabled(v);bDiv.setEnabled(v);bSom.setEnabled(v);bSoust.setEnabled(v);
    bRes.setEnabled(v);
    bSqrt.setEnabled(v);bLog.setEnabled(v);
    bFact.setEnabled(v);bSin.setEnabled(v);bCos.setEnabled(v);
 bTan.setEnabled(v);bPuis.setEnabled(v);
    }
    void activateOp(boolean v)
    {
    b.setEnabled(v);bexp.setEnabled(v);bMult.setEnabled(v);bDiv.setEnabled(v);
    bSom.setEnabled(v);bSoust.setEnabled(v);bRes.setEnabled(v);
    bSqrt.setEnabled(v);bLog.setEnabled(v);
    bFact.setEnabled(v);
    bSin.setEnabled(v);bCos.setEnabled(v);bTan.setEnabled(v);bPuis.setEnabled(v);
    
    }
    
   
    void Resultat()
    {
     double x2=Double.parseDouble(txt.getText());
     if(mult) {txt.setText(""+(xp*x2));xp=Double.parseDouble(txt.getText());}
  else if(div) 
  {
   if(x2!=0)
   {
   txt.setText(""+(xp/x2));
   xp=Double.parseDouble(txt.getText());
   }
   else
   {
    activateP2P3(false);
    txt.setBackground(Color.LIGHT_GRAY);txt.setForeground(Color.RED);
    txt.setText("  ERROR !!  DIVISION   PAR   ZERO   IMPOSSIBLE  ");
   }  
  }
  else if(som) {txt.setText(""+(xs+x2));xs=Double.parseDouble(txt.getText());}
  else if(soust){ txt.setText(""+(xs-x2));xs=Double.parseDouble(txt.getText());}
  
   else
   {
    txt.setBackground(Color.LIGHT_GRAY);txt.setForeground(Color.RED);
    txt.setText("  ERROR!!  RULE  :  \"  n  doit  etre >= r  et  n >= 0  et  r >= 0  \" ");
       activateP2P3(false);}
  }
  
    double fact(double x)
    {
     double res=1;
     if(x>=0)
     {
     for(int i=2;i<=x;i++)
  {
   res=res*i;
  }
  return res;
  }
  else return 0;
    }
 public void actionPerformed(ActionEvent e)
 {
  Object src = e.getSource();
  JButton baux=(JButton)src;
  if((src==b0)||src==b1||src==b2||src==b3||src==b4||src==b5||src==b6||src==b7||src==b8||src==b9)
  { 
  if(!op) txt.setText(txt.getText()+baux.getLabel());
  else if(op)
  {
   txt.setText(baux.getLabel());
   op=false;
  }
  }
  else if(src==b)
  {
   if(!virg)
   {
    txt.setText(txt.getText()+".");
    virg=true;
   }
  }
 
  else if(src==bexp)
  {
   txt.setText(""+Math.PI);
   virg=true;
  }
  
  else if(src==bc)
  {txt.setText("0");xp=1;xs=0;init=true;aux=0;
  mult=false;div=false;som=false;soust=false;
  activateP2P3(true);virg=false;
  txt.setBackground(Color.black);txt.setForeground(Color.GREEN);}
  else if(src==bcl)
   txt.setText(txt.getText().substring(0,txt.getText().length()-1));
  else if(src==bMult)
  {
  try
  {
  if(init || op)
  {
   xp=(Double.parseDouble(txt.getText()));
   init=false;virg=false;
   txt.setText(""+xp);
  }
  else
  {
   Resultat();
   xp=(Double.parseDouble(txt.getText()));
  }
   op=true;mult=true;div=false;som=false;soust=false;
   
  }catch(NumberFormatException execp)
     {
      txt.setBackground(Color.LIGHT_GRAY);txt.setForeground(Color.RED);
      txt.setText("ERROR :  CONVERTION  AVEC  VERGULE  FLOTTANTE  NON  SUPPORTEE ");
       activateP2P3(false);}
  }
  else if(src==bDiv)
  {
  try
  {
  if(init || op)
  {
   xp=(Double.parseDouble(txt.getText()));
   init=false;virg=false;
   txt.setText(""+xp);
  }
  else
  {
    Resultat();
   xp=(Double.parseDouble(txt.getText()));
  }
  op=true;mult=false;div=true;som=false;soust=false;
  }catch(NumberFormatException execp)
     {
      txt.setBackground(Color.LIGHT_GRAY);txt.setForeground(Color.RED);
      txt.setText("ERROR :  CONVERTION  AVEC  VERGULE  FLOTTANTE  NON  SUPPORTEE ");
      activateP2P3(false);}
  }
  
  else if(src==bSom)
  {
  try
  {
  if(init || op)
  {
   xs=(Double.parseDouble(txt.getText()));
   init=false;virg=false;
   txt.setText(""+xs);
  }
  else
  {
    Resultat();
    xs=(Double.parseDouble(txt.getText()));
  }
  op=true;mult=false;div=false;som=true;soust=false;
  }catch(NumberFormatException execp)
     {
      txt.setBackground(Color.LIGHT_GRAY);txt.setForeground(Color.RED);
      txt.setText("ERROR :  CONVERTION  AVEC  VERGULE  FLOTTANTE  NON  SUPPORTEE ");
      activateP2P3(false);}
  }
  
  else if(src==bSoust)
  {
  try
  {
  if(init || op)
  {
   xs=(Double.parseDouble(txt.getText()));
   init=false;virg=false;
   txt.setText(""+xs);
  }
  else
  {
   Resultat();
   xs=(Double.parseDouble(txt.getText()));
  }
     op=true;mult=false;div=false;som=false;soust=true;
  }catch(NumberFormatException execp)
     {
      txt.setBackground(Color.LIGHT_GRAY);txt.setForeground(Color.RED);
      txt.setText("ERROR :  CONVERTION  AVEC  VERGULE  FLOTTANTE  NON  SUPPORTEE ");
      activateP2P3(false);}
     }
  else if(e.getSource()==bRes)
  {
   Resultat();
   init=true;virg=false;
  }
   
  else if(src==bPuis)
  {
   aux=Double.parseDouble(txt.getText());
   init=false;
   op=true;mult=false;div=false;som=false;soust=false;
  }
  
  else if(src==bLog)
  {
   double  x=Double.parseDouble(txt.getText());
   if( x>0)
   {
   double  res=Math.log(x);
   txt.setText(""+res);
   }
   else if( x>0)
   {
   double  res=Math.log10(x);
   txt.setText(""+res); 
   }
   else if(x<=0)
   {
    txt.setBackground(Color.LIGHT_GRAY);txt.setForeground(Color.RED);
    txt.setText(" ERROR!!  RULE : \"  X  DOIT  ETRE  >  A  ZERO  \" ");
       activateP2P3(false);}
  }
 
  
 
     else if(src==bOff) System.exit(0);
 }

 
 public static void main (String[] args) 
 {
  cal2 ca =new  cal2();
  ca.setVisible(true);
 }
}


EDIT: Ajout de la coloration syntaxique.

1 réponse

cormandyr Messages postés 600 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 8 juillet 2016 11
8 déc. 2014 à 11:23
Salut,

Alors 1. ton post est horrible... avec autant de lignes de code, pourquoi ne pas utilisé les balises de code histoire de rendre le tout plus lisible???

2. il semblerait que ton code ne compile pas car tu n'as pas implémenté la méthode ItemStateChanged de l'interface ItemListener....

moi perso, ca avait l'air de fonctionner...

Corm.

--
0
Whismeril Messages postés 19020 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 15 avril 2024 656
8 déc. 2014 à 11:46
Alors 1. ton post est horrible... avec autant de lignes de code, pourquoi ne pas utilisé les balises de code histoire de rendre le tout plus lisible???
c'est fait
0
Rejoignez-nous