Calculator program in Java using AWT: This simple source code is to create a calculator application which is used to perform an usual calci functions in java. This program can be designed with the use of Labels, Text fields, buttons and frames. This project is used by various degree students like engineering, MCA & all kind of IT professionals. Also this can be used as LAB exercise program. An example code is given below.
CALCULATOR IN JAVA AWT
This Java AWT program is used to design a GUI calculator. It will calculate the addition, subtraction, multiplication, division, and average for values. It is just like a normal calculator by using separate awt code.
[Useful: Learn English Quickly] & [Career Guidance for All]
Java AWT calculator code:
Q: Write a code for simple calculator program in Java using AWT.
//Import statements import java.awt.*; import java.awt.event.*; //Class Definition class Calc extends Frame implements ActionListener, TextListener, FocusListener { //Variabl declaraion Label la1,la2,la3; TextField te1,te2,te3; Button bn1, bn2, bn3, bn4, bn5; int y,z,re=0; // © http://students3k.com - Karthikh Venkat public Calc(String title) { super(title); setLayout(new FlowLayout()); la1=new Label("Number 1:");//Object creation la2=new Label("Number 2:"); la3=new Label("Result :"); te1=new TextField(10); te1.addFocusListener(this);//Calling the method te2=new TextField(10); te3=new TextField(10); te3.setEditable(false); bn1=new Button("Add"); bn2=new Button("Sub"); bn3=new Button("Mul"); bn4=new Button("Div"); bn5=new Button("Exit"); bn1.setEnabled(false); bn2.setEnabled(false); bn3.setEnabled(false); bn4.setEnabled(false); bn1.addActionListener(this); bn2.addActionListener(this); bn3.addActionListener(this); bn4.addActionListener(this); bn5.addActionListener(this); te1.addTextListener(this); te2.addTextListener(this); add(la1); add(te1); add(la2); add(te2); add(la3); add(te3); add(bu1); add(bu2); add(bu3); add(bu4); add(bu5); } public static void main(String args[])//Main function { Calc c=new Calc("Calculator"); c.setSize(200,200); c.show(); } // © http://students3k.com - Karthikh Venkat public void actionPerformed(ActionEvent e)//Method definition { //To Convert string to integer x=Integer.parseInt(te1.getText()); y=Integer.parseInt(te2.getText()); if(e.getSource()==bu1) //Decision making statement re=x+y; else if(e.getSource()==bu2) re=x-y; else if(e.getSource()==bu3) re=x*y; else if(e.getSource()==bu4) re=x/y; else if(e.getSource()==bu5) System.exit(0); te3.setText(String.valueOf(re)); } public void copy() { x=Integer.parseInt(te1.getText()); y=Integer.parseInt(te2.getText()); } // © http://students3k.com - Karthikh Venkat public void textValueChanged(TextEvent tu) { if(!te1.getText().equals("") & (!te2.getText().equals(""))) { bu1.setEnabled(true); bu2.setEnabled(true); bu3.setEnabled(true); bu4.setEnabled(true); } else { bu1.setEnabled(false); bu2.setEnabled(false); bu3.setEnabled(false); bu4.setEnabled(false); } } public void focusGained(FocusEvent fe)// method { // © http://students3k.com - Karthikh Venkat } public void focusLost(FocusEvent fe) { if(te1.getText().equals("")) { javax.swing.JOptionPane.showMessageDialog(this,"The field should not be left blank"); } } }