1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
package calculatrice;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Calculatrice extends JFrame{
//déclaration éléments graphiques
private JFrame fenetre = new JFrame("page JSP");
private JPanel panneauBoutons = new JPanel();
private JPanel panneauChiffres = new JPanel();
private JPanel panneauEcran = new JPanel();
private JPanel panneauCalcul = new JPanel();
private JTextArea ecran = new JTextArea(5,25);
private JButton bt_un = new JButton("1");
private JButton bt_deux = new JButton("2");
private JButton bt_trois = new JButton("3");
private JButton bt_quatre = new JButton("4");
private JButton bt_cinq = new JButton("5");
private JButton bt_six = new JButton("6");
private JButton bt_sept = new JButton("7");
private JButton bt_huit = new JButton("8");
private JButton bt_neuf = new JButton("9");
private JButton bt_zero = new JButton("0");
private JButton bt_plus = new JButton("+");
private JButton bt_moins = new JButton("-");
private JButton bt_multiple = new JButton("x");
private JButton bt_divise = new JButton("\u00f7");
private JButton bt_modulo = new JButton("mod");
private JButton bt_virgule = new JButton(".");
private JButton bt_egale = new JButton("=");
private JButton bt_clear = new JButton("C");
//déclaration variables de test
private boolean vir= false;
private boolean suite = false;
private boolean fin = false;
private String textSigne;
private String chaine;
private String nombre1;
private String nombre2;
private String memo;
public Calculatrice(){
//construction graphique et ajout de listener
ecran.setEditable(false);
panneauBoutons.setLayout(new GridLayout(4,2,5,5));
panneauBoutons.add(bt_plus);
panneauBoutons.add(bt_moins);
panneauBoutons.add(bt_divise);
panneauBoutons.add(bt_multiple);
//panneauBoutons.add(bt_modulo);
panneauBoutons.add(bt_clear);
panneauBoutons.add(bt_egale);
panneauChiffres.setLayout(new GridLayout(4,3,5,5));
panneauChiffres.add(bt_sept);
panneauChiffres.add(bt_huit);
panneauChiffres.add(bt_neuf);
panneauChiffres.add(bt_quatre);
panneauChiffres.add(bt_cinq);
panneauChiffres.add(bt_six);
panneauChiffres.add(bt_un);
panneauChiffres.add(bt_deux);
panneauChiffres.add(bt_trois);
panneauChiffres.add(bt_zero);
panneauChiffres.add(bt_virgule);
panneauEcran.add(new JScrollPane(ecran));
ActionListener actionChiffres = new ChiffresListener();
ActionListener actionSigne = new SigneListener();
ActionListener actionEgale = new EgaleListener();
ActionListener actionClear = new ClearListener();
bt_un.addActionListener(actionChiffres);
bt_deux.addActionListener(actionChiffres);
bt_trois.addActionListener(actionChiffres);
bt_quatre.addActionListener(actionChiffres);
bt_cinq.addActionListener(actionChiffres);
bt_six.addActionListener(actionChiffres);
bt_sept.addActionListener(actionChiffres);
bt_huit.addActionListener(actionChiffres);
bt_neuf.addActionListener(actionChiffres);
bt_zero.addActionListener(actionChiffres);
bt_virgule.addActionListener(actionChiffres);
bt_plus.addActionListener(actionSigne);
bt_moins.addActionListener(actionSigne);
bt_divise.addActionListener(actionSigne);
bt_multiple.addActionListener(actionSigne);
bt_modulo.addActionListener(actionSigne);
bt_egale.addActionListener(actionEgale);
bt_clear.addActionListener(actionClear);
panneauCalcul.setLayout(new GridLayout(1,2,10,10));
panneauCalcul.add(panneauChiffres);
panneauCalcul.add(panneauBoutons);
Container contenu = fenetre.getContentPane();
contenu.setLayout(new BorderLayout());
contenu.add(panneauEcran,BorderLayout.NORTH);
contenu.add(panneauCalcul,BorderLayout.CENTER);
fenetre.pack();
fenetre.setSize(300,250);
fenetre.setResizable(false);
fenetre.setVisible(true);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//listener de la touche C (clear)
class ClearListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
effacement();
}
}
//fonction de remise à zéro
private void effacement(){
ecran.setText("");
suite = false;
fin = false;
vir = false;
}
//listener des boutons des chiffres et virgule
class ChiffresListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
// si on a deja effectué une opération.
if(fin){
effacement();
memo = null;
}
String textChiffre = ev.getActionCommand();
suite = false;
chaine = ecran.getText();
//si on appuie sur la virgule
if(textChiffre.equals(".")){
// si c'est le premier appuie sur la virgule
if(!vir){
// si la chaine est vide
if(chaine.equals("")){
ecran.setText("0" + textChiffre);
vir = true;
}
//si la chaine se termine par un signe
if(chaine.endsWith("-")|| chaine.endsWith("+")|| chaine.endsWith("x")|| chaine.endsWith("\u00f7")){
ecran.setText(chaine +"\n" +"0" + textChiffre);
vir = true;
}
//si la chaine n'est pas vide
else{
ecran.setText(chaine +"\n" + textChiffre);
vir = true;
}
}
}
//si on appuie sur une autre touche que la virgule
else {
ecran.setText(chaine + textChiffre);
}
}
}
//listener des boutons de signe
class SigneListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
vir = false;
if(fin){
effacement();
ecran.setText(memo);
}
textSigne = ev.getActionCommand();
chaine = ecran.getText();
//JOptionPane.showMessageDialog(null, nombre1);
//JOptionPane.showMessageDialog(null, chaine);
//JOptionPane.showMessageDialog(null, textSigne);
//appuie ultérieur
if(suite){
ecran.setText(chaine.substring(0, chaine.length()-3) + "\n" + textSigne);
}
//premier appuie
else{
ecran.setText(chaine + "\n" + textSigne);
suite = true;
}
//JOptionPane.showMessageDialog(null, "memo : " +memo);
//JOptionPane.showMessageDialog(null, "chaine : " +chaine);
//JOptionPane.showMessageDialog(null, "textSigne : "+ textSigne);
}
}
//listener du bouton égale
class EgaleListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
chaine = ecran.getText();
nombre1 = chaine.substring(0,chaine.lastIndexOf(textSigne)-1);
nombre2 = chaine.substring(chaine.lastIndexOf(textSigne)+1, chaine.length());
//JOptionPane.showMessageDialog(null, "chaine : \n" + chaine);
//JOptionPane.showMessageDialog(null, "nombre1 : \n" +nombre1);
//JOptionPane.showMessageDialog(null, "textSigne : \n" +textSigne);
//JOptionPane.showMessageDialog(null, "nombre2 : \n"+nombre2);
calcul(nombre1,nombre2);
if (memo.equals("err")){
JOptionPane.showMessageDialog(null, "ERREUR !!\nDivision par zéro impossible");
effacement();
}else{
ecran.setText(chaine + "\n" + "=" + "\n" + memo);
fin = true;
}
}
private String calcul(String nb1, String nb2) {
float resultat;
float a = Float.parseFloat(nb1);
float b = Float.parseFloat(nb2);
if(textSigne.equals("+")){
resultat = a + b;
}else if(textSigne.equals("-")){
resultat = a - b;
}else if(textSigne.equals("x")){
resultat = a * b;
}else if(textSigne.equals("\u00f7")){
if(b == 0){
return memo ="err";
}
else
resultat = a / b;
}else{
resultat = a % b;
}
memo = Float.toString(resultat);
return memo;
}
}
public static void main (String [] args){
new Calculatrice();
}
} |
Partager