- Code:
private void bCalcActionPerformed(java.awt.event.ActionEvent evt) {
float a=0,b=0,x=0;
boolean conda=false,condb=false;
String rep="";
int index = combo.getSelectedIndex();
DecimalFormat fmt = new DecimalFormat("#.0000");
//Lecture de a et b
try{
a=Float.parseFloat(tVar1.getText());
conda=true;
}
catch(NumberFormatException err){
JOptionPane.showMessageDialog(
null,
tVar1.getText()+" n'est pas un nombre",
"Erreur",
JOptionPane.WARNING_MESSAGE);
}
try{
b=Float.parseFloat(tVar2.getText());
condb=true;
}
catch(NumberFormatException err){
JOptionPane.showMessageDialog(
null,
tVar2.getText()+" n'est pas un nombre",
"Erreur",
JOptionPane.WARNING_MESSAGE);
}
if(conda==false || condb==false)
{
tReponse.setText("NaN");
}
//Operation
if(index==0 && conda==true && condb==true)
{
x=a+b;
tReponse.setText(fmt.format(x));
setTitle("Addition");
}
if(index==1 && conda==true && condb==true)
{
x=a-b;
tReponse.setText(fmt.format(x));
setTitle("Soustraction");
}
if(index==2 && conda==true && condb==true)
{
x=a*b;
tReponse.setText(fmt.format(x));
setTitle("Multiplication");
}
if(index==3 && conda==true && condb==true)
{
if (b==0)
{
tReponse.setText("NaN");
}
else
{
x=a/b;
tReponse.setText(fmt.format(x));
}
setTitle("Division");
}
}
private void bEffaceActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
tReponse.setText("");
tVar1.setText("");
tVar2.setText("");
combo.setSelectedIndex(0);
setTitle("Calculatrice");
}
Voila mes 2 evenements