import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Caculator extends JFrame implements ActionListener {
static public String[] keys = {"1", "2", "3", "+",
"4", "5", "6", "-",
"7", "8", "9", "*",
".", "0", "log", "/",
"=", "+/-", "Del"};
static public JButton[] jb = new JButton[keys.length];
static public JTextField jt = new JTextField();
static public JPanel jp1 = new JPanel(new GridLayout(1, 1, 2, 10));
static public JPanel jp2 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp3 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp4 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp5 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp6 = new JPanel(new GridLayout(1, 2, 2, 10));
static public JPanel jp7 = new JPanel(new GridLayout(1, 1, 2, 10));
static public JPanel jp8 = new JPanel(new GridLayout(1, 2, 2, 10));
public Caculator() {
this.setLayout(new GridLayout(6, 1, 2, 10));
this.setVisible(true);
this.setBounds(300, 300, 300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i=0; i
jb[i] = new JButton(keys[i]);
}
jp1.add(jt);
for(int i=0; i<4; i++) {
jp2.add(jb[i]);
}
for(int i=4; i<8; i++) {
jp3.add(jb[i]);
}
for(int i=8; i<12; i++) {
jp4.add(jb[i]);
}
for(int i=12; i<16; i++) {
jp5.add(jb[i]);
}
jp6.add(jp7);jp6.add(jp8);
jp7.add(jb[16]); jp8.add(jb[17]);jp8.add(jb[18]);
this.add(jp1); this.add(jp2); this.add(jp3);
this.add(jp4); this.add(jp5); this.add(jp6);
for(int i=0; i
jb[i].addActionListener(this);
}
}
public static void main(String[] args) {
Caculator c = new Caculator();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(jb[0])||
e.getSource().equals(jb[1])||
e.getSource().equals(jb[2])||
e.getSource().equals(jb[3])||
e.getSource().equals(jb[4])||
e.getSource().equals(jb[5])||
e.getSource().equals(jb[6])||
e.getSource().equals(jb[7])||
e.getSource().equals(jb[8])||
e.getSource().equals(jb[9])||
e.getSource().equals(jb[11])||
e.getSource().equals(jb[15])||
e.getSource().equals(jb[13])||
e.getSource().equals(jb[10])||
e.getSource().equals(jb[12])||
e.getSource().equals(jb[14])) {
myNumButtonPerformed(e);
}
if(e.getSource().equals(jb[18])) {
myDelPerformed();
}
if(e.getSource().equals(jb[16])) {
jt.setText(String.valueOf(myValuePerformed()));
}
if(e.getSource().equals(jb[17])) {
jt.setText("-");
}
}
public void myNumButtonPerformed(ActionEvent e) {
StringBuffer s = null;
double d;
s = new StringBuffer(jt.getText());
if(s.equals(null)) {
jt.setText((String)e.getActionCommand());
} else {
s = s.append(e.getActionCommand());
jt.setText(s.substring(0));;
}
}
public double myValuePerformed() {
String s = jt.getText();
char sop = 0;
double value = 0;
double d1 = 0;
double d2 = 0;
for(int i=1; i
if((s.charAt(i) == '+')||
(s.charAt(i) == '-')||
(s.charAt(i) == '*')||
(s.charAt(i) == '/')) {
sop = s.charAt(i);
}
}
if(sop == '+') {
String[] str = s.split("\\+");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1+d2;
}
if(sop == '-') {
String[] str = s.substring(1).split("-");
StringBuffer sb = new StringBuffer(s.substring(0, 1));
sb = sb.append(str[0]);
d1 = Double.parseDouble(sb.substring(0, sb.length()));
d2 = Double.parseDouble(str[1]);
value = d1-d2;
}
if(sop == '*') {
String[] str = s.split("\\*");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1*d2;
}
if(sop == '/') {
String[] str = s.split("/");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1/d2;
}
if(s.substring(0, 3).equalsIgnoreCase("log")) {
String str = s.substring(3);
double d = Double.parseDouble(str);
value = Math.log(d);
}
return value;
}
public void myDelPerformed() {
String s = null;
s = jt.getText();
try {
jt.setText(s.substring(0, s.length()-1));
} catch(StringIndexOutOfBoundsException e) {
jt.setText("0");
}
}
}