Advertisement
mdzafir_alvi

Calculator for adding 2 numbers using Gui

Oct 9th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. package makecalculator;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JButton;
  5.  
  6. import java.awt.TextField;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. public class CALCULATOR extends JFrame
  11. {
  12.     CALCULATOR()
  13.     {
  14.         /* Constructing Frame */
  15.         JFrame frame = new JFrame();
  16.        
  17.         frame.setTitle("MY CALCULATOR");
  18.         frame.setBounds(0, 0, 400, 400);
  19.         frame.setVisible(true);
  20.         frame.setLayout(null);
  21.         frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  22.        
  23.         /* Construction Text Fields */
  24.         final TextField tf1 = new TextField();
  25.         tf1.setBounds(50, 300, 250, 20);
  26.         frame.add(tf1);
  27.         final TextField tf2 = new TextField();
  28.         tf2.setBounds(50, 200, 250, 20);
  29.         frame.add(tf2);
  30.         final TextField tf3 = new TextField();
  31.         tf3.setBounds(50, 100, 250, 20);
  32.         frame.add(tf3);
  33.         /* Construction Text Fields - Adding TExt fields */
  34.        
  35.        
  36.        
  37.        
  38.        
  39.         /*Construction button*/
  40.         JButton b1 = new JButton();
  41.         b1.setLocation(50,50);
  42.         b1.setSize(50,50);
  43.         b1.setText("+");
  44.         /*Construction button - Adding Actions*/
  45.         b1.addActionListener
  46.         (
  47.              new ActionListener()
  48.              {
  49.                  public void actionPerformed(ActionEvent e)
  50.                  {
  51.                     String s1 = tf1.getText();
  52.                     String s2 = tf2.getText();
  53.                     int num1=Integer.parseInt(s1);
  54.                     int num2=Integer.parseInt(s2);
  55.                     int sum = num1+num2;
  56.                     tf3.setText(Integer.toString(sum));
  57.                  }
  58.                  
  59.              }
  60.         );
  61.         /*Construction button - adding button b1*/
  62.         frame.add(b1);
  63.     }
  64. }
  65. public class MakeCalculator
  66. {
  67.     public static void main(String[] args)
  68.     {
  69.         CALCULATOR c = new CALCULATOR();
  70.     }  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement