import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class cl {
int mn, fir, sec;
double last;
Frame f = new Frame("계산기");
String num[];
ActionListener al;
TextField tf=new TextField();
Panel p;
public cl() {
p = new Panel();
Button[] numButtons = null;
p.setLayout(new GridLayout(4, 4));
String numStr[] = { "7", "8", "9", "*", "4", "5", "6", "/", "1",
"2", "3", "+", "C", "0", "=", "-" };
numButtons = new Button[numStr.length];
for (int i = 0; i < numStr.length; i++) {
numButtons[i] = new Button(numStr[i]);
numButtons[i].setForeground(Color.blue);
p.add(numButtons[i]);
}
al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand() == "0") {
tf.setText("0");
} else if (e.getActionCommand() == "1") {
tf.setText("1");
} else if (e.getActionCommand() == "2") {
tf.setText("2");
} else if (e.getActionCommand() == "3") {
tf.setText("3");
} else if (e.getActionCommand() == "4") {
tf.setText("4");
} else if (e.getActionCommand() == "5") {
tf.setText("5");
} else if (e.getActionCommand() == "6") {
tf.setText("6");
} else if (e.getActionCommand() == "7") {
tf.setText("7");
} else if (e.getActionCommand() == "8") {
tf.setText("8");
} else if (e.getActionCommand() == "9") {
tf.setText("9");
} else if (e.getActionCommand() == "+") {
last = Integer.parseInt(tf.getText());
mn = 1;
} else if (e.getActionCommand() == "-") {
last = Integer.parseInt(tf.getText());
mn = 2;
} else if (e.getActionCommand() == "*") {
last = Integer.parseInt(tf.getText());
mn = 3;
} else if (e.getActionCommand() == "/") {
last = Integer.parseInt(tf.getText());
mn = 4;
} else if (e.getActionCommand() == "C") {
fir = 0;
sec = 0;
last = 0;
tf.setText("0");
} else if (e.getActionCommand() == "=") {
last = Integer.parseInt(tf.getText());
if (mn == 1) {
last = fir += sec;
tf.setText(String.valueOf(last));
} else if (mn == 2) {
last = fir -= sec;
tf.setText(String.valueOf(last));
} else if (mn == 3) {
last = fir *= sec;
tf.setText(String.valueOf(last));
} else if (mn == 4) {
if (sec == 0) {
last = 0;
tf.setText(String.valueOf(last));
} else if (sec != 0) {
last = fir /= sec;
tf.setText(String.valueOf(last));
}
}
}
}
};
for (int i = 0; i < 16; i++) {
numButtons[i].addActionListener(al);
}
f.add("North", tf);
f.add("Center", p);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
f.setSize(200, 150);
f.setVisible(true);
}
}
public class cal {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
cl aa = new cl();
}
}
여기까진 내가 짯는데....
연산결과가 미친듯이 0만튀어나와요...왜그런지 좀 알려줘요 제발 ㅠㅠ
미쳤나보지.