This is a simple basic calculator in java which does functions +,-,*... Very simple GUI based, i hope it would help for the beginners...
Code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame
implements ActionListener {
JFrame calcFrame;
Container con;
JTextField txtAnswer;
JButton btn0 , btn1 , btn2 , btn3 , btn4;
JButton btn5 , btn6 , btn7 , btn8 , btn9;
JButton btnPlus , btnMinus , btnMultiply; JButton btnEqual , btnClear , btnPoint;
JPanel panelButtons;
JLabel calcLabel;
int num = 0;
int num1 = 0;
Integer result = 0 , tempNum = 0;
String str;
int addBit = 0 , subBit = 0 , mulBit = 0 , flag = 0 ;
public void calcInterface() {
calcFrame = new JFrame();
// Creating 0 to 9 Buttons
btn0 = new JButton("0");
btn1 = new JButton("1");
btn2 = new JButton("2");
btn3 = new JButton("3");
btn4 = new JButton("4");
btn5 = new JButton("5");
btn6 = new JButton("6");
btn7 = new JButton("7");
btn8 = new JButton("8");
btn9 = new JButton("9");
// Creating All Operators Buttons
btnPlus = new JButton("+");
btnMinus = new JButton("-");
btnMultiply = new JButton("*");
btnEqual = new JButton("=");
btnClear = new JButton("C");
btnPoint = new JButton(".");
panelButtons = new JPanel( new GridLayout(4,4) );
panelButtons.add(btn1);
panelButtons.add(btn2);
panelButtons.add(btn3);
panelButtons.add(btnClear);
panelButtons.add(btn4);
panelButtons.add(btn5);
panelButtons.add(btn6);
panelButtons.add(btnMultiply);
panelButtons.add(btn7);
panelButtons.add(btn8);
panelButtons.add(btn9);
panelButtons.add(btnMinus);
panelButtons.add(btn0);
panelButtons.add(btnPoint);
panelButtons.add(btnPlus);
panelButtons.add(btnEqual);
con = getContentPane();
con.setLayout (new BorderLayout());
txtAnswer = new JTextField();
calcLabel = new JLabel("My Calculator In Java");
con.add(txtAnswer , BorderLayout.NORTH);
con.add(calcLabel , BorderLayout.SOUTH);
con.add(panelButtons , BorderLayout.CENTER);
setSize(300,300);
setVisible(true);
actionOnButtons();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// Default Constructor
public Calculator() {
calcInterface();
}
// ActionListeners of All Buttons
public void actionOnButtons() {
btn0.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btnPlus.addActionListener(this);
btnPoint.addActionListener(this);
btnClear.addActionListener(this);
btnEqual.addActionListener(this);
btnMinus.addActionListener(this);
btnMultiply.addActionListener(this);
}
// reseting All operators(btnPlus, btnMinus , btnMultiply etc ) Bits to 0
public void resetBits() {
addBit = 0 ;
subBit = 0 ;
mulBit = 0 ;
}
//Clear All Variable values To 0 and txtAnswer =""
public void clearResult() {
num = 0;
num1 = 0;
result = 0;
txtAnswer.setText("");
resetBits();
}
public void actionPerformed (ActionEvent ae) { // Actions On All Buttons
if ( ae.getSource() == btn0) {
txtAnswer.setText(txtAnswer.getText() + "0");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn1) {
txtAnswer.setText(txtAnswer.getText() + "1");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn2) {
txtAnswer.setText(txtAnswer.getText() + "2");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn3) {
txtAnswer.setText(txtAnswer.getText() + "3");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn4) {
txtAnswer.setText(txtAnswer.getText() + "4");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn5) {
txtAnswer.setText(txtAnswer.getText() + "5");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn6) {
txtAnswer.setText(txtAnswer.getText() + "6");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn7) {
txtAnswer.setText(txtAnswer.getText() + "7");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn8) {
txtAnswer.setText(txtAnswer.getText() + "8");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btn9) {
txtAnswer.setText(txtAnswer.getText() + "9");
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btnPoint) {
txtAnswer.setText(txtAnswer.getText() + ".");
str = txtAnswer.getText();
str = txtAnswer.getText();
num = Integer.parseInt(str);
} else if ( ae.getSource() == btnClear) {
clearResult();
} else if ( ae.getSource() == btnPlus) {
addBit = 1;
str = txtAnswer.getText();
num1 = Integer.parseInt(str);
txtAnswer.setText("");
} else if ( ae.getSource() == btnMinus) {
subBit = 1;
str = txtAnswer.getText();
num1 = Integer.parseInt(str);
txtAnswer.setText("");
} else if ( ae.getSource() == btnMultiply) {
mulBit = 1;
str = txtAnswer.getText();
num1 = Integer.parseInt(str);
txtAnswer.setText("");
} else if ( ae.getSource() == btnEqual) {
if (addBit == 1)
result = num1 + num;
if (subBit == 1)
result = num1 - num;
if (mulBit == 1)
result = num1 * num;
txtAnswer.setText(result.toString());
resetBits();
}
}
public static void main (String[] args) {
new Calculator();
}
}
0 comments:
Post a Comment