import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class Cal extends Frame
implements ActionListener
{
String s1[] = {
"1", "2", "3", "+", "4", "5", "6", "-", "7", "8",
"9", "*", ".", "0", "=", "/"
};
JButton b[];
static JTextArea tf;
//static JTextArea tf1;
JPanel jp1;
JPanel jp2;
JFrame f;
private double result;
private double opr1;
private double opr2;
static String lastop;
Cal()
{
b = new JButton[20];
opr1 = 0.0D;
opr2 = 0.0D;
result = 0.0D;
lastop = "";
jp1 = new JPanel();
jp2 = new JPanel();
tf = new JTextArea("0");
tf.setBackground(Color.black);
tf.setForeground(Color.green);
jp1.setLayout(new GridLayout(4, 4));
jp2.setLayout(new GridLayout(1, 2));
for(int i = 1; i <= 16; i++)
{
b[i] = new JButton(s1[i - 1]);
jp1.add(b[i]);
b[i].addActionListener(this);
}
jp2.add(tf);
//jp2.add(tf1);
f = new JFrame("claculator");
f.setLayout(new BorderLayout());
f.add(jp1, "Center");
f.add(jp2, "North");
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
int j = dimension.width;
int k = dimension.height;
f.setSize(200, 200);
f.setLocation(j / 4, k / 4);
}
public static void write(String s)
{
if(tf.getText().equals("0"))
{
tf.setText(s);
} else
if(lastop == "=")
{
tf.setText(s);
lastop = null;
} else
{
tf.setText((new StringBuilder()).append(tf.getText()).append(s).toString());
}
}
public void actionPerformed(ActionEvent actionevent)
{
if(actionevent.getActionCommand() == "1")
{
write("1");
} else
if(actionevent.getActionCommand() == "2")
{
write("2");
} else
if(actionevent.getActionCommand() == "3")
{
write("3");
} else
if(actionevent.getActionCommand() == "4")
{
write("4");
} else
if(actionevent.getActionCommand() == "5")
{
write("5");
} else
if(actionevent.getActionCommand() == "6")
{
write("6");
} else
if(actionevent.getActionCommand() == "7")
{
write("7");
} else
if(actionevent.getActionCommand() == "8")
{
write("8");
} else
if(actionevent.getActionCommand() == "9")
{
write("9");
} else
if(actionevent.getActionCommand() == "0")
{
write("0");
} else
if(actionevent.getActionCommand() == ".")
{
if(tf.getText().indexOf(".") == -1)
{
tf.setText((new StringBuilder()).append(tf.getText()).append(".").toString());
}
} else
if(actionevent.getActionCommand() == "+" || actionevent.getActionCommand() == "-" || actionevent.getActionCommand() == "*" || actionevent.getActionCommand() == "/")
{
opr1 = Double.parseDouble(tf.getText());
lastop = actionevent.getActionCommand();
tf.setText("");
// tf1.setText(actionevent.getActionCommand());
} else
if(actionevent.getActionCommand() == "=")
{
//tf1.setText(actionevent.getActionCommand());
opr2 = Double.parseDouble(tf.getText());
if(lastop.equals("+"))
{
result = opr1 + opr2;
} else
if(lastop.equals("-"))
{
result = opr1 - opr2;
} else
if(lastop.equals("*"))
{
result = opr1 * opr2;
} else
if(lastop.equals("/"))
{
result = opr1 / opr2;
}
tf.setText((new StringBuilder()).append(" ").append(result).toString());
lastop = "=";
}
}
public static void main(String args[])
{
new Cal();
}
}
Download
0 comments:
Post a Comment