1

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){   }


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tony Andreev
  • 421
  • 1
  • 6
  • 9
  • 1
    And the error messages are? – Alexis C. Jan 19 '14 at 09:34
  • 3
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jan 19 '14 at 09:34

3 Answers3

2

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.

KarelG
  • 5,176
  • 4
  • 33
  • 49
  • I am in the first semester in the university.And the teacher hasn't taught us swing or jframe yet.So I have to do this this way.And yeah everywhere I looked nobody is using applets or awt.Anyway thank you for the fast responce. – Tony Andreev Jan 19 '14 at 10:10
  • if my reply has helped you, please mark it as an answer for people whom is also using AWT and having button event problems. Now, i hope that the teacher will teach you to use Swing later. My first GUI experience was Swing (It depends from docent to docent). If you aren't getting courses with Swing, i recommend to learn it yourself by using tutorials. It might help you a lot (and make it easier) in future applications. Good luck with learning programming. – KarelG Jan 19 '14 at 12:56
  • Yes.I have done it.Everything is working 100% now.So your reply really helped me.I just don't know how to mark the question as answered :D. – Tony Andreev Jan 19 '14 at 23:03
  • *"And the teacher hasn't taught us swing or jframe yet."* Well I'll stress, I do want **you** to refer **the teacher** to that article. They should not be teaching applets at all, and are wasting the student's time by teaching them AWT. If they are working from 'prepared texts', it is overdue they update those texts. – Andrew Thompson Jan 19 '14 at 23:41
0

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()); 
}
Reeno
  • 5,720
  • 11
  • 37
  • 50
Neil
  • 1
  • 1
0

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.