Applet calculatrice (premiere applet)

Soyez le premier à donner votre avis sur cette source.

Vue 12 325 fois - Téléchargée 856 fois

Description

ceci est ma première applet (comme quand j'ai commencé delphi, je me suis décidé pour une calculatrice, lol), alors soyez indulgents et ne me lapidez pas trop ;-)
je pense que le code est assez clair malgré l'absence de commentaires, toutefois j'aimerais savoir si je n'ai pas fait trop d'erreurs et aussi comment assigner un raccourci aux boutons (par exemple, si j'appuie sur la touche "1" du pavé numérique, que l'applet considère que j'ai cliqué sur son bouton "1", etc...)
en fonction des remarques/critiques et aides, je reposterais cette applet (avec moults commentaires), car vu que c'est avec elle (enfin, j'ai beaucoup lu quelques sites et mis en application dans ce projet) que j'ai appris ce que je sais en java, en particulier sur le placement des composants, je pense que ça pourra resservir à d'autres ;-)
merci d'avance de votre aide !!

Source / Exemple :


import java.awt.*;
import java.awt.event.*;
public final class Calc extends java.applet.Applet implements ActionListener
{ public final Color clBackGround = new Color(213, 204, 187);
  public final Color clForeGround = new Color(0, 0, 0);
  public final Color clOperations = new Color(128, 255, 0);
  public final Color clCancel = new Color(255, 128, 0);
  public final Color clDone = new Color(255, 200, 0);
  public final Color clNbs = new Color(213, 204, 187);
  public static void main(String[] args)
  { System.out.println("Ceci est une applet Java, et non une application Java.");
    System.out.println("Elle doit donc être exécutée par l'intermédiaire de votre navigateur internet.");
  }
  private int Op = -1;
  private String Nb1 = "0";
  private String Nb2 = "0";
  private Label CalcResult = new Label("0", Label.RIGHT);
  private Button ButtonDivide = new Button("/");
  private Button ButtonMultiply = new Button("*");
  private Button ButtonAdd = new Button("+");
  private Button ButtonMinus = new Button("-");
  private Button Button7 = new Button("7");
  private Button Button8 = new Button("8");
  private Button Button9 = new Button("9");
  private Button ButtonCancel = new Button("C");
  private Button Button4 = new Button("4");
  private Button Button5 = new Button("5");
  private Button Button6 = new Button("6");
  private Button Button1 = new Button("1");
  private Button Button2 = new Button("2");
  private Button Button3 = new Button("3");
  private Button ButtonEquals = new Button("=");
  private Button Button0 = new Button("0");
  private Button ButtonPoint = new Button(".");
  public void init()
  { setBackground(clForeGround);
    setForeground(clForeGround);
    setLayout(new GridBagLayout());
    CalcResult.setBackground(clBackGround);
    CalcResult.setForeground(clForeGround);
    setFont(new Font("Courier", Font.PLAIN, 17));
    GridBagConstraints calcConstraints = new GridBagConstraints();
    Insets calcInsets = new Insets(4, 4, 4, 4);
    calcConstraints.gridx = 0;
    calcConstraints.gridy = 0;
    calcConstraints.gridwidth = 4;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.HORIZONTAL;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    add(CalcResult, calcConstraints);
    calcConstraints.gridx = 0;
    calcConstraints.gridy = 1;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    ButtonDivide.setBackground(clOperations);
    ButtonDivide.setForeground(clForeGround);
    ButtonDivide.setActionCommand("DIV");
    ButtonDivide.addActionListener(this);
    add(ButtonDivide, calcConstraints);
    calcConstraints.gridx = 1;
    calcConstraints.gridy = 1;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    ButtonMultiply.setBackground(clOperations);
    ButtonMultiply.setForeground(clForeGround);
    ButtonMultiply.setActionCommand("MUL");
    ButtonMultiply.addActionListener(this);
    add(ButtonMultiply, calcConstraints);
    calcConstraints.gridx = 2;
    calcConstraints.gridy = 1;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    ButtonAdd.setBackground(clOperations);
    ButtonAdd.setForeground(clForeGround);
    ButtonAdd.setActionCommand("ADD");
    ButtonAdd.addActionListener(this);
    add(ButtonAdd, calcConstraints);
    calcConstraints.gridx = 3;
    calcConstraints.gridy = 1;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    ButtonMinus.setBackground(clOperations);
    ButtonMinus.setForeground(clForeGround);
    ButtonMinus.setActionCommand("SUB");
    ButtonMinus.addActionListener(this);
    add(ButtonMinus, calcConstraints);
    calcConstraints.gridx = 0;
    calcConstraints.gridy = 2;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button7.setBackground(clNbs);
    Button7.setForeground(clForeGround);
    Button7.setActionCommand("7");
    Button7.addActionListener(this);
    add(Button7, calcConstraints);
    calcConstraints.gridx = 1;
    calcConstraints.gridy = 2;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button8.setBackground(clNbs);
    Button8.setForeground(clForeGround);
    Button8.setActionCommand("8");
    Button8.addActionListener(this);
    add(Button8, calcConstraints);
    calcConstraints.gridx = 2;
    calcConstraints.gridy = 2;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button9.setBackground(clNbs);
    Button9.setForeground(clForeGround);
    Button9.setActionCommand("9");
    Button9.addActionListener(this);
    add(Button9, calcConstraints);
    calcConstraints.gridx = 3;
    calcConstraints.gridy = 2;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 2;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    ButtonCancel.setBackground(clCancel);
    ButtonCancel.setForeground(clForeGround);
    ButtonCancel.setActionCommand("RAZ");
    ButtonCancel.addActionListener(this);
    add(ButtonCancel, calcConstraints);
    calcConstraints.gridx = 0;
    calcConstraints.gridy = 3;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button4.setBackground(clNbs);
    Button4.setForeground(clForeGround);
    Button4.setActionCommand("4");
    Button4.addActionListener(this);
    add(Button4, calcConstraints);
    calcConstraints.gridx = 1;
    calcConstraints.gridy = 3;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button5.setBackground(clNbs);
    Button5.setForeground(clForeGround);
    Button5.setActionCommand("5");
    Button5.addActionListener(this);
    add(Button5, calcConstraints);
    calcConstraints.gridx = 2;
    calcConstraints.gridy = 3;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button6.setBackground(clNbs);
    Button6.setForeground(clForeGround);
    Button6.setActionCommand("6");
    Button6.addActionListener(this);
    add(Button6, calcConstraints);
    calcConstraints.gridx = 0;
    calcConstraints.gridy = 4;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button1.setBackground(clNbs);
    Button1.setForeground(clForeGround);
    Button1.setActionCommand("1");
    Button1.addActionListener(this);
    add(Button1, calcConstraints);
    calcConstraints.gridx = 1;
    calcConstraints.gridy = 4;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button2.setBackground(clNbs);
    Button2.setForeground(clForeGround);
    Button2.setActionCommand("2");
    Button2.addActionListener(this);
    add(Button2, calcConstraints);
    calcConstraints.gridx = 2;
    calcConstraints.gridy = 4;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button3.setBackground(clNbs);
    Button3.setForeground(clForeGround);
    Button3.setActionCommand("3");
    Button3.addActionListener(this);
    add(Button3, calcConstraints);
    calcConstraints.gridx = 3;
    calcConstraints.gridy = 4;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 2;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    ButtonEquals.setBackground(clDone);
    ButtonEquals.setForeground(clForeGround);
    ButtonEquals.setActionCommand("DONE");
    ButtonEquals.addActionListener(this);
    add(ButtonEquals, calcConstraints);
    calcConstraints.gridx = 0;
    calcConstraints.gridy = 5;
    calcConstraints.gridwidth = 2;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    Button0.setBackground(clNbs);
    Button0.setForeground(clForeGround);
    Button0.setActionCommand("0");
    Button0.addActionListener(this);
    add(Button0, calcConstraints);
    calcConstraints.gridx = 2;
    calcConstraints.gridy = 5;
    calcConstraints.gridwidth = 1;
    calcConstraints.gridheight = 1;
    calcConstraints.weightx = 100;
    calcConstraints.weighty = 100;
    calcConstraints.fill = GridBagConstraints.BOTH;
    calcConstraints.anchor = GridBagConstraints.CENTER;
    calcConstraints.insets = calcInsets;
    calcConstraints.ipadx = 0;
    calcConstraints.ipady = 0;
    ButtonPoint.setBackground(clNbs);
    ButtonPoint.setForeground(clForeGround);
    ButtonPoint.setActionCommand("POINT");
    ButtonPoint.addActionListener(this);
    add(ButtonPoint, calcConstraints);
  }
  public void start()
  { requestFocus();
  }
  private void dispResult(String s)
  { if (s.charAt(0) == '0') s = s.substring(1, s.length());
    if (s.charAt(0) == '.') s = "0" + s;
    CalcResult.setText(s);
  }
  private void doRAZ()
  { CalcResult.setText("0");
    Nb1 = "0";
    Nb2 = "0";
    Op = -1;
  }
  private void addChar(char digit)
  { if (Op > 0)
    { if (Nb2.charAt(0) == '0') Nb2 = "" + digit; else Nb2 = Nb2 + digit;
      dispResult(Nb2);
    }
    else
    { if (Op == 0) doRAZ();
      if (Nb1.charAt(0) == '0') Nb1 = "" + digit; else Nb1 = Nb1 + digit;
      dispResult(Nb1);
    }
  }
  private float getNb(String s)
  { return (new Float(s)).floatValue();
  }
  private void doOperation()
  { float Nb = 0;
    switch (Op)
    { case  1 : Nb = (getNb(Nb1) / getNb(Nb2)); break;
      case  2 : Nb = (getNb(Nb1) * getNb(Nb2)); break;
      case  3 : Nb = (getNb(Nb1) + getNb(Nb2)); break;
      case  4 : Nb = (getNb(Nb1) - getNb(Nb2)); break;
      default : return;
    }
    Op = 0;
    Nb2 = "0";
    Nb1 = Float.toString(Nb);
    dispResult(Nb1);
  }
  public void actionPerformed(ActionEvent e)
  { String theAction = e.getActionCommand();
    if (theAction.equals("POINT"))
    { if ((CalcResult.getText()).indexOf('.') < 0) addChar('.');
    }
    if (theAction.equals("0"))
    { addChar('0');
    }
    if (theAction.equals("1"))
    { addChar('1');
    }
    if (theAction.equals("2"))
    { addChar('2');
    }
    if (theAction.equals("3"))
    { addChar('3');
    }
    if (theAction.equals("4"))
    { addChar('4');
    }
    if (theAction.equals("5"))
    { addChar('5');
    }
    if (theAction.equals("6"))
    { addChar('6');
    }
    if (theAction.equals("7"))
    { addChar('7');
    }
    if (theAction.equals("8"))
    { addChar('8');
    }
    if (theAction.equals("9"))
    { addChar('9');
    }
    if (theAction.equals("DIV"))
    { if (Op > 0) doOperation();
      CalcResult.setText("0");
      Nb2 = "0";
      Op = 1;
    }
    if (theAction.equals("MUL"))
    { if (Op > 0) doOperation();
      CalcResult.setText("0");
      Nb2 = "0";
      Op = 2;
    }
    if (theAction.equals("ADD"))
    { if (Op > 0) doOperation();
      CalcResult.setText("0");
      Nb2 = "0";
      Op = 3;
    }
    if (theAction.equals("SUB"))
    { if (Op > 0) doOperation();
      CalcResult.setText("0");
      Nb2 = "0";
      Op = 4;
    }
    if (theAction.equals("RAZ"))
    { doRAZ();
    }
    if (theAction.equals("DONE"))
    { if (Op > 0) doOperation();
    }
  }
}

Conclusion :


pas de bugs connus, hormis des erreurs de précisions dues aux float...
j'attend vos remarques ;-)

Codes Sources

A voir également

Ajouter un commentaire

Commentaires

Messages postés
790
Date d'inscription
samedi 8 juin 2002
Statut
Membre
Dernière intervention
7 juin 2007
1
Merci mais c'est surtout grâce à des tutoriels complets que j'ai pu faire ça aussi vite ;-) Maintenant je suis loin d'avoir terminé, mon but étant de coder une applet de chat pour un site auquel je participe (j'ai deja codé un client delphi mais la plupart des gens hésitent à télécharger des fichiers exe avec toutes les affaires de virus récentes... d'ou mon idée de me mettre a java)... en tout cas merci à toi !
Messages postés
33
Date d'inscription
mercredi 27 février 2002
Statut
Membre
Dernière intervention
25 avril 2003

Salut,

J'ai regardé attentivement ton source, je trouve ça pas mal pour quelqu'un qui fait ses premières armes en java ! ET surtout content d'avoir pu t'aider avec ma petite intro au GUI. Si t'as des questions n'hésite pas

A+
NeoZiX

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.