简体   繁体   中英

java calculator decimal point

I have fully working calculator using java.Can tell me how to add decimal point.I already have the button and the variables are in type double.I just can't make the button work. I tried to do it myself,but I ended up with error messages every time. Here is the code:

package oop;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Kalkulator2 extends Applet {
String arg1= "", arg2="";
double ergebnis;
Button zahl[] =new Button[10];
Button funktion[] = new Button[4];
Button ausfuehren;
Button decimalpoint;
char dec='.';
Panel zahlPanel,funktionPanel,ergebnisPanel;
TextField ergebnisFeld = new TextField(5);
int operationArgument;
char operation;
public void init () {
    operationArgument= 1; operation =' ';
    setLayout(new BorderLayout());
    zahlPanel = new Panel();
    zahlPanel.setLayout(new GridLayout (4,3));
    for (int i=9; i>=0; i--) {
        zahl[i] = new Button(String.valueOf(i));
        zahl[i].addActionListener(new ButtonZahlen());
        zahlPanel.add(zahl[i]);
    }
    decimalpoint = new Button(String.valueOf(dec));   //decimal point

    //decimalpoint.addActionListener(new Button ());
    ausfuehren = new Button("=");
    ausfuehren.addActionListener(new ButtonAusfuehren()); //zu dem Listener
    zahlPanel.add(decimalpoint);
    zahlPanel.add(ausfuehren);

    add("Center",zahlPanel);
    funktionPanel = new Panel();
    funktionPanel.setLayout(new GridLayout(4,1));
    funktion[0] = new Button("+");
    funktion[0].addActionListener(new ButtonOperation());
    funktionPanel.add(funktion[0]);
    funktion[1] = new Button("-");
    funktion[1].addActionListener(new ButtonOperation());
    funktionPanel.add(funktion[1]);
    funktion[2] = new Button("*");
    funktion[2].addActionListener (new ButtonOperation());
    funktionPanel.add(funktion[2]);
    funktion[3] = new Button("/");
    funktion[3].addActionListener (new ButtonOperation());
    funktionPanel.add(funktion[3]);



    add("East",funktionPanel);
    ergebnisPanel = new Panel();

    ergebnisPanel.add(ergebnisFeld);
    add("North",ergebnisPanel);

}
class ButtonZahlen implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        switch (operationArgument)   {
        case 1 :  {
            arg1+=e.getActionCommand();
            ergebnisFeld.setText(arg1);
            break;
        }
        case 2 :   {
            arg2 +=e.getActionCommand();
            ergebnisFeld.setText(arg2);
            break;
        }
        default: { }

        }
        }
    }
class ButtonAusfuehren implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if(operation =='+')
                ergebnis = new Double(arg1) + new Double(arg2);
        else if (operation == '-') 
            ergebnis = new Double(arg1) - new Double(arg2);



        else if(operation =='*') 
                ergebnis = new Double(arg1) * new Double(arg2);

        else if(operation =='/')
                ergebnis = new Double(arg1) / new Double(arg2);

        ergebnisFeld.setText(String.valueOf(ergebnis));


        }


    }


class ButtonOperation implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("+")) {
            operation = '+'; operationArgument = 2;
            }
        else if(e.getActionCommand().equals("-")) {
                operation = '-'; operationArgument = 2;
                }
        else if(e.getActionCommand().equals("*")) {
                    operation = '*' ; operationArgument =2;
                    }
        else if(e.getActionCommand().equals("/")) {
                        operation = '/' ; operationArgument =2;
        }
                    }

            }
        }


public void paint(Graphics g){   }


}

When the button got clicked, it is trying to create a new button object which doesn't implement an actionListener. Thus it will throw an error saying " what must i do with a new button while i need an object with 'actionPerformed' method " Here is a possible solution;

// create button object
decimalpoint = new Button(".");

// not good : decimalpoint.addActionListener(new Button ());
// event on click
decimalpoint.addActionListener(new YourClassName());

and YourClassName is an instance to handle the button event

class YourClassName implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // add decimal point
    }
}

I also agree with Andrew Thompson that AWT is not a preferred way to handle your tasks. If your teacher has suggested you to use AWT, then please use Swing. Swing is far better then AWT and should be educated to people who is writing GUI-based java for the first time.

To answer the question, to add a DECIMAL POINT to java code (my example is for GUI NetBeans IDE 8.0.2) I have stumbled across this code. I must admit I have not come across this code having looked for an answer on the net.

 private void PointActionPerformed(java.awt.event.ActionEvent evt) {                                      

      txtDisplay.setText(txtDisplay.getText()+Point.getText()); 
}

you can do that simply

specify the button -

Button Decimal;

caste the button you specified in your xml file to (Button)Decimal -

Decimal = findViewById(R.id.the id you gave to the button);

Now set on click listener

Decimal.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

             edit.setText(edit.getText().toString() + ".");
}

where edit is the field you want the text to be filled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM