About Me

header ads

JAVA GUI CALCULATOR | WETECHKNOWLOGY

GRAPHICAL USER INTERFACE OF CALCULATOR IN JAVA

HELLO GUYS , WE HAVE GOT A PROJECT ON GRAPHICAL USER INTERFACE OF A CALCULATOR.

THE CODE IS PROVIDED BELOW , IN THIS CODE THE THE JAVA FRAMES ARE BEING USED .
IN THIS CALCULATOR WE HAVE THE FOLLOWING OPTIONS :


HERE WE HAVE FOLLLOWING OPTIONS:
-ADD (ADDITION)
-SUB(SUBTRACTION)
-MUL(MULTIPLICATION)
-DIV(DIVISION)
-MOD(MODULUS)
-MAX(MAXIMUM)
-MIN(MINIMUM)
-CONCAT(CONCATINATION)
-DATE(GIVES CURRENT DATE)
-CLEAR(CLEAR INPUTS)
-EXIT(EXITS THE APPLICATION)

--------------------------------------------------------------------------------------------------------------------------
 import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class MyGui extends JFrame

implements ActionListener

{

JTextField tf,tf1,tf2,tf3;

JTextArea area,area1;

JScrollPane pane,pane1;

JButton b, b2,b3,b4,b5,b6,b7,b8,b9,b10,b11;

JLabel l,l1,l2,l3,l4;

public void setGUI()

{

super.setBounds

(200, 200, 900, 600);

super.setTitle("CALCULATOR");

super.setResizable(false);

//

tf = new JTextField();

tf.setBounds(700,100,100,50);

super.add(tf);

tf1= new JTextField();

tf1.setBounds(700,180,100,50);

super.add(tf1);

area = new JTextArea();

pane = new JScrollPane(area); 

pane.setBounds(700,260,100,50);

super.add(pane);

area1 = new JTextArea();

pane = new JScrollPane(area1); 

pane.setBounds(700,340,100,50);

super.add(pane);

b = new JButton("ADD");

b.setBounds(100, 115, 100, 30);

super.add(b);

// register button for event handling

b.addActionListener(this);

b2 = new JButton("SUB");

b2.setBounds(230, 115, 100, 30);

super.add(b2);

b2.addActionListener(this);

b3 = new JButton("MUL");

b3.setBounds(360, 115, 100, 30);

super.add(b3);

b3.addActionListener(this);

b4 = new JButton("DIV");

b4.setBounds(100, 215, 100, 30);

super.add(b4);

b4.addActionListener(this);

b5 = new JButton("MOD");

b5.setBounds(230,215, 100, 30);

super.add(b5);

b5.addActionListener(this);

b6 = new JButton("MAX");

b6.setBounds(360,215, 100, 30);

super.add(b6);

b6.addActionListener(this);

b7 = new JButton("MIN");

b7.setBounds(100,315 , 100, 30);

super.add(b7);

b7.addActionListener(this);

b8 = new JButton("CONCAT");

b8.setBounds(230,315, 100, 30);

super.add(b8);

b8.addActionListener(this);

b9 = new JButton("DATE");

b9.setBounds(360,315, 100, 30);

super.add(b9);

b9.addActionListener(this);

b10 = new JButton("CLEAR");

b10.setBounds(140,400, 140, 40);

super.add(b10);

b10.addActionListener(this);

b11 = new JButton("EXIT");

b11.setBounds(300,400, 140, 40);

super.add(b11);

b11.addActionListener(this);

l = new JLabel("DATA 1:");

l.setBounds(600, 115, 50, 25);

super.add(l);

l1 = new JLabel("DATA 2:");

l1.setBounds(600,190,50, 25);

super.add(l1);

l2 = new JLabel("OPERATION:");

l2.setBounds(600, 265, 50, 25);

super.add(l2);

l3 = new JLabel("RESULT:");

l3.setBounds(600, 340, 50, 25);

super.add(l3); 

l4 = new JLabel("CALCULATOR");

l4.setBounds(240,30, 100,50);

super.add(l4);

super.setLayout(null);

super.setVisible(true);

super.setDefaultCloseOperation

(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) 

{

MyGui m = new MyGui();

m.setGUI();

}

// lets override the AP method of 

// action-listener interface

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == b)

{

// fetch data of TF and AREA

String s1 = tf.getText(),s2 = tf1.getText();

double x=Double.parseDouble(s1);

double y=Double.parseDouble(s2);

String str=Double.toString(x+y);

//store in area

area1.setText(str);

area.setText("ADD");

}

else if(ae.getSource()==b2)

{

String s1 = tf.getText(),s2 = tf1.getText();

double x=Double.parseDouble(s1);

double y=Double.parseDouble(s2);

String str=Double.toString(x-y);

//store in area

area1.setText(str);

area.setText("SUB");

}

else if(ae.getSource()==b3)

{

String s1 = tf.getText(),s2 = tf1.getText();

double x=Double.parseDouble(s1);

double y=Double.parseDouble(s2);

String str=Double.toString(x*y);

//store in area

area1.setText(str);

area.setText("MUL");

}

else if(ae.getSource()==b4)

{

String s1 = tf.getText(),s2 = tf1.getText();

double x=Double.parseDouble(s1);

double y=Double.parseDouble(s2);

String str=Double.toString(x/y);

//store in area

area1.setText(str);

area.setText("DIV");

}

else if(ae.getSource()==b5)

{

String s1 = tf.getText(),s2 = tf1.getText();

double x=Double.parseDouble(s1);

double y=Double.parseDouble(s2);

String str=Double.toString(x%y);

//store in area

area1.setText(str);

area.setText("MOD");

}

else if(ae.getSource()==b6)

{

String s1 = tf.getText(),s2 = tf1.getText();

double max = 0;

double x=Double.parseDouble(s1);

double y=Double.parseDouble(s2);

if(x>y)

{

max=x;

}

else

{

max=y;

}

String str=Double.toString(max);

//store in area

area1.setText(str);

area.setText("MAX");

}

else if(ae.getSource()==b7)

{

String s1 = tf.getText(),s2 = tf1.getText();

double min = 0;

double x=Double.parseDouble(s1);

double y=Double.parseDouble(s2);

if(x>y)

{

min=y;

}

else

{

min=x;

}

String str=Double.toString(min);

//store in area

area1.setText(str);

area.setText("MIN");

}

else if(ae.getSource()==b8)

{

String s1 = tf.getText(),s2 = tf1.getText();

area1.setText(s1+s2);

area.setText("CONCAT");

}

else if(ae.getSource()==b9)

{

DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

LocalDateTime now=LocalDateTime.now();

area1.setText(dtf.format(now));

area.setText("Date");

}

else if(ae.getSource()==b10)

{

area1.setText(" ");

area.setText(" ");

tf.setText(" ");

tf1.setText(" ");

}

else if(ae.getSource()==b11)

{

            System.exit(0);

                }


}

}

Post a Comment

0 Comments