Calculator Application Using Java AWT
Here is the java code for a simple calculator application that will look like this.
Code:
import java.awt.*;
import java.awt.event.*;
public class CalGUI implements ActionListener, KeyListener {
String[] s = {"", "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "=", "+", "C"};
Frame f;
Button[] b = new Button[16];
TextField text;
int x = 35, y = 100;
float res = 0.0f;
String msg = "";
char op;
public CalGUI() {
f = new Frame("Calculator");
f.setSize(400, 450);
f.setResizable(false);
f.setLayout(null);
f.setBackground(Color.gray);
text = new TextField(45);
text.setFocusable(false);
text.setBounds(25, 40, 350, 40);
text.setEditable(false);
text.setFont(new Font("Consolas",1,20));
f.add(text);
for (int i = 1; i < s.length; i++) {
b[i-1] = new Button(s[i]);
b[i-1].setFont(new Font("Consolas",1,20));
b[i-1].setBackground(Color.LIGHT_GRAY);
b[i-1].setBounds(x, y, 80, 80);
x=x+80;
if(i % 4 == 0 && i != 0) {y = y + 80; x = 35;}
f.add(b[i-1]);
}
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
for(int i = 0; i < b.length; i++) {
b[i].addActionListener(this);
}
f.addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
char ch = ke.getKeyChar();
if (ch >= '0' && ch <= '9') {
msg += ch;
text.setText(msg);
}
else if(ch == '=') {
switch(op) {
case '+':
res = res + Integer.parseInt(text.getText());
break;
case '-':
res = res - Integer.parseInt(text.getText());
break;
case '*':
res = res * Integer.parseInt(text.getText());
break;
case '/':
res = res / (float)Integer.parseInt(text.getText());
break;
}
text.setText("Ans. " + res);
msg="";
}
else if(ch == 'C') {
text.setText("");
res = 0;
msg ="";
}
else {
res = Integer.parseInt(text.getText());
text.setText("");
msg = "";
switch(ch) {
case '+': op = '+'; break;
case '*': op = '*'; break;
case '/': op = '/'; break;
case '-': op = '-'; break;
}
}
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
char ch = str.charAt(0);
if (ch >= '0' && ch <= '9') {
msg += str;
text.setText(msg);
}
else if(ch == '=') {
switch(op) {
case '+':
res = res + Integer.parseInt(text.getText());
break;
case '-':
res = res - Integer.parseInt(text.getText());
break;
case '*':
res = res * Integer.parseInt(text.getText());
break;
case '/':
res = res / (float)Integer.parseInt(text.getText());
break;
}
text.setText("Ans. " + res);
msg="";
}
else if(ch == 'C') {
text.setText("");
res = 0;
msg ="";
}
else {
res = Integer.parseInt(text.getText());
text.setText("");
msg = "";
switch(ch) {
case '+': op = '+'; break;
case '*': op = '*'; break;
case '/': op = '/'; break;
case '-': op = '-'; break;
}
}
}
public static void main(String[] arg) {
new CalGUI();
}
@Override
public void keyTyped(KeyEvent arg0) {
char ch = arg0.getKeyChar();
if (ch >= '0' && ch <= '9') {
msg += ch;
text.setText(msg);
}
else if(ch == '=') {
switch(op) {
case '+':
res = res + Integer.parseInt(text.getText());
break;
case '-':
res = res - Integer.parseInt(text.getText());
break;
case '*':
res = res * Integer.parseInt(text.getText());
break;
case '/':
res = res / (float)Integer.parseInt(text.getText());
break;
}
text.setText("Ans. " + res);
msg="";
}
else if(ch == 'C') {
text.setText("");
res = 0;
msg ="";
}
else {
res = Integer.parseInt(text.getText());
text.setText("");
msg = "";
switch(ch) {
case '+': op = '+'; break;
case '*': op = '*'; break;
case '/': op = '/'; break;
case '-': op = '-'; break;
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {}
}