:) Hey Guys,
I'm a newbie poster here and I'm new with java. . I made a GUI of a simple calculator,the problem is I don't know how to put events on the buttons of my simple calculator. .
Can you please help me with this problem?

Here's my code for GUI of calculator. .

import java.awt.*;
public class Exer5{
	
   private Frame marky;
   
   private Button buttper;
   private Button buttc;
   private Button buttbsp;
   private Button butt1;
   private Button butt2;
   private Button butt3;
   private Button butt4;
   private Button butt5;
   private Button butt6;
   private Button butt7;
   private Button butt8;
   private Button butt9;
   private Button butt0;
   private Button buttplus;
   private Button buttminus;
   private Button buttdivide;
   private Button buttx;
   private Button buttequal;
   private TextField text1;
   
   public Exer5()
   	
   	{
       marky = new Frame ("Simple Calculator");
       text1 = new TextField ("               ");
       buttbsp = new Button (" Backspace ");
       buttc = new Button ("  C  ");
       buttper = new Button ("  . ");
       butt0 = new Button (" 0 ");
 	   butt1 = new Button (" 1 ");
 	   butt2 = new Button (" 2 ");
 	   butt3 = new Button (" 3 ");
 	   butt4 = new Button (" 4 ");
 	   butt5 = new Button (" 5 ");
 	   butt6 = new Button (" 6 ");
 	   butt7 = new Button (" 7 ");
 	   butt8 = new Button (" 8 ");
 	   butt9 = new Button (" 9 ");
 	   buttplus = new Button (" + ");
 	   buttminus = new Button (" - ");
 	   buttdivide = new Button (" / ");
 	   buttx = new Button (" * ");
 	   buttequal = new Button (" = ");
    } 
   
   public void launchFrame()
   	
   	{
       	marky.setLayout(new FlowLayout());
       	
       	marky.add(text1);
       	marky.add(buttbsp);
       	marky.add(buttc);
       	marky.add(butt7);
        marky.add(butt8);
        marky.add(butt9);
        marky.add(buttdivide);
        marky.add(butt4);
        marky.add(butt5);
        marky.add(butt6);
        marky.add(buttx);
        marky.add(butt1);
       	marky.add(butt2);
        marky.add(butt3);
        marky.add(buttminus);
       	marky.add(butt0);
        marky.add(buttper);
        marky.add(buttequal);
        marky.add(buttplus);
              
        marky.setSize(150,220);
        marky.setBackground(Color.cyan);
        marky.setVisible(true);             
            
    }
   public static void main(String args[])
   	
   	{
      Exer5 markyFrame = new Exer5();
      markyFrame.launchFrame();
    }
}

Hoping for your positive response. .:)

to 'append' actions to a JButton, you need to add an ActionListener to the button.

for instance:

JButton addButton = new JButton();
addButton.setText("add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               addSomething(evt);
            }
        });

...

private void addSomething(java.awt.event.ActionEvent evt) {
    System.out.println("add whatever you want");
}
This article has been dead for over six months. Start a new discussion instead.