Java GUI Program to Add Two Numbers Using AWT

Remember that program we did to add two numbers? We are going to simply uptrade! We will create a Java GUI program to add two numbers using AWT and it’s gonna be fun.

Adding two numbers doesn’t have too much of a logic. But when you are doing so while using AWT things become a little challenging. Snice a text field in Java takes in String as input we need to first parse it into the form of Integer.

We are going to create two text fields and a label where the output will be seen. A button will be used to trigger the event.

So let’s get to it.

Java GUI Program to Add Two Numbers Using AWT

Here is the addition program:

import java.awt.*;
import java.net.*;
import java.awt.event.*;

public class LearnAWT extends Frame { 
            TextField tf1; 
            TextField tf2; 
            Label l1; 
            Button b;
                    LearnAWT() { 
                          setTitle("Adder"); 
                          tf1 = new TextField(); 
                          tf1.setBounds(100, 50, 85, 20);
                          tf2 = new TextField(); 
                          tf2.setBounds(100, 100, 85, 20); 
                          b = new Button("Add"); 
                          b.setBounds(110,220,60,40);
                          l1 = new Label(""); 
                          l1.setBounds(100, 120, 85, 20); 
                          add(b); 
                          add(tf1); 
                          add(tf2); 
                          add(l1); 
                          setSize(300,300); 
                          setVisible(true); 

        b.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e) { 
                    int a = Integer.parseInt(tf1.getText());
                    int b = Integer.parseInt(tf2.getText()); 
                    int c = a + b; 
                    l1.setText("Their sum is = " + String.valueOf(c)); 
                                      }
                }); 
            } 
public static void main(String []args) { 
                   new LearnAWT();
          } 
}

As you can see our main code went into the addActionListener method. We created three variables and parsed the String values into integers. Then for setText we had convert them back to String, and so String’s valueOf() came to the rescue.

Notice, since you are using AWT classes and event handler you need to import the respective classes first or your code wouldn’t work.

Now let’s run the above program.

You will get the following result when you run it:

Java GUI program to add two numbers using AWT

Isn’t that a charm?

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

9 Responses

  1. vikas says:

    “b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    int a = Integer.parseInt(tf1.getText());
    int b = Integer.parseInt(tf2.getText());
    int c = a + b;
    l1.setText(“Their sum is = ” + String.valueOf(c)); “”

    if i want to add more than one button such as “add” ,”sub” ,”mul” etc..
    so what is step to add more button

    • dumbitdude says:

      Initialize a new button like:

      Button b2 = new Button(“LABEL OF THE BUTTON”);

      and then set its bounds by using:

      b2.setBounds(150,250,80,70);

      Then finally use

      add(b2);

      to include it.

      Eventually, you have to create a new Action Listener Anonymous class

      b2.addActionListener(new ActionListener(){

      });

      and then specify what should happen when you click the button inside a method.

      – Scottshak

  2. Santhosh says:

    How to change the frame location and Setdefaultclose operation

    • Scottshak says:

      Hi Santhosh,

      If you want to bring it to the centre you can try:

      setLocationRelativeTo(null);

      set default close operation can be performed by:

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Hope that helps.

      Cheers

  3. hero says:

    helloooooo

  1. July 11, 2017

    […] Also, you could use Java GUI or AWT to make an interesting looking program. I have created an addition program using GUI here. […]

  2. July 12, 2017

    […] How to Add Two Numbers […]

  3. July 13, 2017

    […] a Checkbox in Java GUI is quite a simple process. Here, unlike the buttons we saw in a Java GUI program to add two numbers there is no addActionListener. We have addItemListener instead. Checkbox in Java GUI comes really […]

Leave a Reply