einfacher Währungsrechner - incompatible types

chwo

Mitglied
Hallo,
ich habe hier einen sehr einfachen Währungsrechner erstellt, bei dem man einen double-wert eingibt und der dann mit einem Klick auf den entsprechenden Button entweder in Euro oder in Dollar umgewandelt wird. Beim kompilieren bekomme ich aber immer diese Fehlermeldung :(
Leider kenne ich mich mir dem ActionListener nicht aus, sodass ich nicht weiter weiss.
Bitte helft mir

hier der Error:

Java:
waehrungsrechner.java:70:20: error: incompatible types
        e = fromTF (eTF);
                   ^
  required: ActionEvent
  found:    double
waehrungsrechner.java:71:16: error: method euroDollar in class waehrungsrechner cannot be applied to given types;
        x1Str= euroDollar(e);
               ^
  required: double
  found: ActionEvent
  reason: actual argument ActionEvent cannot be converted to double by method invocation conversion
waehrungsrechner.java:78:19: error: incompatible types
        e = fromTF(eTF);
                  ^
  required: ActionEvent
  found:    double
waehrungsrechner.java:79:16: error: method dollarEuro in class waehrungsrechner cannot be applied to given types;
        x1Str= dollarEuro(e);
               ^
  required: double
  found: ActionEvent
  reason: actual argument ActionEvent cannot be converted to double by method invocation conversion
4 errors

hier der Quellcode:
Java:
// Währungsrechner Euro-Dollar
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


//Erklärung
//e= Eingabe
//k= (Wechsel)Kurs
//a= Ausgabe
public class waehrungsrechner extends Applet {
  Panel         p1, p2;  
  Label         eL, kL, aL;
  TextField     eTF, kTF, aTF;
  Button        EuDoB, DoEuB, clrB;
  double        e, a;
  String        x1Str;
  
  public void lay (){
    setSize(500,100);
    p1    = new Panel();
    eL    = new Label ("Eingabe:");
    eTF   = new TextField (10);
    kL     = new Label  ("Kurs:");      
    kTF    = new TextField (10);
    aL    = new Label ("Ausgabe:");
    aTF   = new TextField (10);
    p1.add (eL); p1.add (eTF); p1.add (kL); p1.add (kTF); p1.add (aL); p1.add (aTF);
    
    p2    = new Panel ();
    EuDoB = new Button ("Euro in Dollar");
    DoEuB = new Button ("Dollar in Euro");
    clrB  = new Button ("loeschen");
    p2.add (EuDoB); p2.add(DoEuB); p2.add (clrB);
    
    setLayout (new BorderLayout());
    add ("North", p1);
    add ("Center", p2);
    
  }
  
  public void init () {
    lay();
    rechne ();
  }  
  
  public double fromTF (TextField tf) {
    return Double.valueOf (tf.getText()).doubleValue();
  }
  
  public String euroDollar(double x){
    String ergStr="";  
    double e=(x*2.0);
    ergStr = " "+e;
    return ergStr;
  }
  
  public String dollarEuro(double y){
    String ergStr="";  
    double e=(y*0.7386);
    ergStr = ""+e;
    return ergStr;  
  }
  
  public void rechne () {
    EuDoB.addActionListener (new ActionListener() {
      public void actionPerformed (ActionEvent e) {
        e = fromTF (eTF);
        x1Str= euroDollar(e);
        aTF.setText (x1Str);
        kTF.setText ("1,3533");        
      }
    });
    DoEuB.addActionListener (new ActionListener() {
      public void actionPerformed (ActionEvent e) {
        e = fromTF(eTF);
        x1Str= dollarEuro(e);
        aTF.setText (x1Str);
        kTF.setText ("0,7386");    
        
        //        p = fromTF (pTF);
        //        q = fromTF (qTF);
        //        x1Str= axpb (p,q);
        //        x1TF.setText (x1Str);
        //        x2Str= axpb2 (p,q);
        //        x2TF.setText (x2Str);        
      }
    });
    clrB.addActionListener (new ActionListener() {
      public void actionPerformed (ActionEvent e) {
        eTF.setText (" "); kTF.setText(" "); aTF.setText(" "); 
      }
    });
  }
}
 

stg

Top Contributor
Du hast eine Klassenvariable
Code:
double e
. In deinen Methoden kommt nun teilweise eine lokale Variable
Code:
ActionEvent e
hinzu. Beide heißen gleich, was hier zu dem Konflikt führt. Du versuchst in deinem Code beispielsweise einer double-Variablen einen ActionEvent zuzuweise und umgekehrt, was natürlich Blödsinn ist. Wähle also entweder eindeutige Namen, oder achte genau darauf, welche der beiden Variablen gerade sichtbar ist.
 
Zuletzt bearbeitet:

chwo

Mitglied
Wow, super vielen Dank für die schnelle Antwort!!!
Du hattest recht. Ich habe das ActionEvent einfach anstatt e f genannt uns schon geht es.
Super nochmals vielen Dank :toll::applaus:
 

Neue Themen


Oben