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
|
package calculatrice;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Calculatrice extends JFrame{
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("=");
boolean vir= false;
boolean signe = false;
public Calculatrice(){
panneauBoutons.setLayout(new GridLayout(3,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_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(ecran);
ActionListener actionBouton = new ChiffresListener();
bt_un.addActionListener(actionBouton);
bt_deux.addActionListener(actionBouton);
bt_trois.addActionListener(actionBouton);
bt_quatre.addActionListener(actionBouton);
bt_cinq.addActionListener(actionBouton);
bt_six.addActionListener(actionBouton);
bt_sept.addActionListener(actionBouton);
bt_huit.addActionListener(actionBouton);
bt_neuf.addActionListener(actionBouton);
bt_zero.addActionListener(actionBouton);
bt_plus.addActionListener(actionBouton);
bt_moins.addActionListener(actionBouton);
bt_divise.addActionListener(actionBouton);
bt_multiple.addActionListener(actionBouton);
bt_virgule.addActionListener(actionBouton);
bt_modulo.addActionListener(actionBouton);
bt_egale.addActionListener(actionBouton);
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);
}
class ChiffresListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
String textBouton = ev.getActionCommand();
calcul(textBouton);
}
}
private void calcul(String arg){
float a;
float b;
float c;
String ope1;
String ope2;
String chaine = ecran.getText();
String oper;
int fin;
// si on appuie sur les chiffres
if(!arg.equals("=") && !arg.equals(",") && !arg.equals("+") && !arg.equals("-") && !arg.equals("x") && !arg.equals("\u00f7")&& !arg.equals("mod")){
ecran.setText(chaine+arg);
}
//si on appuie sur une autre touche
else{
// teste que la chaine n'est pas vide
if(chaine.equals("")){
ecran.setText("");
}
//si la chaine n'est pas vide
else {
// si on appuie sur la virgule pour la premiere fois et que la chaine n'est pas vide
if (arg.equals(",") && !vir){
vir = true;
ecran.setText(chaine+arg);
}
else ecran.getText();
// si on appuie sur un signe pour la premiere fois et que la chaine n'est pas vide
if ((arg.equals("-") && !signe)|| (arg.equals("+") && !signe)|| (arg.equals("\u00f7") && !signe)|| (arg.equals("x") && !signe)||(arg.equals("mod") && !signe)){
signe = true;
vir = false;
ecran.setText(chaine+arg);
}
else ecran.getText();
//si on a un signe et qu'on appuie sur =
if (signe && arg.equals("=")){
fin = chaine.indexOf("x");
oper = chaine.substring(fin,fin+1);
//si le signe est -
if(oper.equals("-")){
ope1 = chaine.substring(0,fin);
a = Float.parseFloat(ope1);
ope2 = chaine.substring(fin+1,chaine.length());
b = Float.parseFloat(ope2);
c = a - b;
signe = false;
ecran.setText(Float.toString(c));
}
//si le signe est +
if(oper.equals("+")){
ope1 = chaine.substring(0,fin);
a = Float.parseFloat(ope1);
ope2 = chaine.substring(fin+1,chaine.length());
b = Float.parseFloat(ope2);
c = a + b;
signe = false;
ecran.setText(Float.toString(c));
}
//si le signe est x
if(oper.equals("x")){
ope1 = chaine.substring(0,fin);
a = Float.parseFloat(ope1);
ope2 = chaine.substring(fin+1,chaine.length());
b = Float.parseFloat(ope2);
c = a * b;
signe = false;
ecran.setText(Float.toString(c));
}
//si le signe est /
if(oper.equals("\u00f7")){
ope1 = chaine.substring(0,fin);
a = Float.parseFloat(ope1);
ope2 = chaine.substring(fin+1,chaine.length());
b = Float.parseFloat(ope2);
c = a / b;
signe = false;
// test de la division par zéro
if (b == 0){
ecran.setText("division par zéro impossible");
}
else{
ecran.setText(Float.toString(c));
}
}
//si le signe est %
if(oper.equals("mod")){
ope1 = chaine.substring(0,fin);
a = Float.parseFloat(ope1);
ope2 = chaine.substring(fin+1,chaine.length());
b = Float.parseFloat(ope2);
c = a % b;
signe = false;
ecran.setText(Float.toString(c));
}
}//fin du =
}//fin de chaine vide
}//fin des touches
}// fin de la méthode
public static void main (String [] args){
new Calculatrice();
}
} |
Partager