Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I use action event (and/or actionevent listener) in Java? Right now I hav

ID: 3884667 • Letter: H

Question

How do I use action event (and/or actionevent listener) in Java?

Right now I have this.

public void actionPerformed( ActionEvent arg0 ) {
  
}

Where arg0 will be the button pressed. This project is a implementing a Calculator. So the buttons would be:

"7", "8", "9", "+", "4", "5", "6", "- ", "1", "2", "3",
"*", "0", "^", "=", "/", "(", ")", "C", "CE"

Which are all implemented on the GUI already, with the operators and the C, CE buttons already having their own classes and stored in a hashmap.

So, for the actionevent listener, How should I implement it? When a button is pressed, how do I store the string that was pressed? And how to call the hashmap function when a button is pressed?

Example:
1. A "1" is pressed, then "+" is pressed, then "1" is pressed, then "=" is pressed.
2. If a number is pressed, how to convert that string into an integer?
3. And then how to implement that integer with the Addition() function? (Addition.java exists, containing Class Addition{} )
4. How would you implement the "=" class?

Explanation / Answer

/* complete calculator program to help you out how to create a calculator using actionListener*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Calculator extends Applet implements ActionListener
{
String first,op;
TextField t1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b15,b14,b0;
public void init()
{
t1=new TextField(40);
b0=new Button("0"); //creation of buttons
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b10=new Button("+");
b11=new Button("-");
b12=new Button("*");
b13=new Button("/");
b14=new Button(".");
b15=new Button("="); //this will help you to find how to create "=" in program
Panel p=new Panel();
  
p.setLayout(new GridLayout(4,4)); //layout creation


p.add(b0); p.add(b1); //adding of all the buttons
p.add(b2); p.add(b3);
p.add(b4); p.add(b5);
p.add(b6); p.add(b7);
p.add(b8); p.add(b9);
p.add(b10); p.add(b11);
p.add(b12); p.add(b13);
p.add(b14); p.add(b15);

b10.addActionListener(this); // addition of java actionListener
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);

Panel p1=new Panel();
p1.setLayout(new BorderLayout());
p1.add("North",t1);
p1.add("Center",p);
add(p1);
}

public boolean action(Event e,Object o) //data fetching
{
if(o.equals("0"))
t1.setText(t1.getText()+"0");
if(o.equals("1"))
t1.setText(t1.getText()+"1");
if(o.equals("2"))
t1.setText(t1.getText()+"2");
if(o.equals("3"))
t1.setText(t1.getText()+"3");
if(o.equals("4"))
t1.setText(t1.getText()+"4");
if(o.equals("5"))
t1.setText(t1.getText()+"5");
if(o.equals("6"))
t1.setText(t1.getText()+"6");
if(o.equals("7"))
t1.setText(t1.getText()+"7");
if(o.equals("8"))
t1.setText(t1.getText()+"8");
if(o.equals("9"))
t1.setText(t1.getText()+"9");
if(o.equals("."))
t1.setText(t1.getText()+".");
return true;
}

/* mathematical calculation */
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("+"))
{
first=t1.getText();
op="+";
t1.setText("");
}
if(ae.getActionCommand().equals("-"))
{
first=t1.getText();
op="-";
t1.setText("");
}
if(ae.getActionCommand().equals("*"))
{
first=t1.getText();
op="*";
t1.setText("");
}
if(ae.getActionCommand().equals("/"))
{
first=t1.getText();
op="/";
t1.setText("");
}
if(ae.getActionCommand().equals("="))
{
float x=Float.parseFloat(first);
float y=Float.parseFloat(t1.getText());
float z=0.0f;
if(op.equals("+"))
{
z=x+y;
}
if(op.equals("-"))
{
z=x-y;
}
if(op.equals("*"))
{
z=x*y;
}
if(op.equals("/"))
{
z=x/y;
}
t1.setText(""+z);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote