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;
class Cal extends JFrame {
private JTextField t1;
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
JButton pl, mi, mu, di, cal, c;
String x, y;
int operator;
// + - * /
// 0 1 2 3
public Cal() {
operator = 4;
setSize(285, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
JPanel p = new JPanel();
p.setLayout(null);
t1 = new JTextField(10);
p.add(t1);
b1 = new JButton("1");
p.add(b1);
b1.addActionListener(new Number());
b2 = new JButton("2");
p.add(b2);
b2.addActionListener(new Number());
b3 = new JButton("3");
p.add(b3);
b3.addActionListener(new Number());
b4 = new JButton("4");
p.add(b4);
b4.addActionListener(new Number());
b5 = new JButton("5");
p.add(b5);
b5.addActionListener(new Number());
b6 = new JButton("6");
p.add(b6);
b6.addActionListener(new Number());
b7 = new JButton("7");
p.add(b7);
b7.addActionListener(new Number());
b8 = new JButton("8");
p.add(b8);
b8.addActionListener(new Number());
b9 = new JButton("9");
p.add(b9);
b9.addActionListener(new Number());
b0 = new JButton("0");
p.add(b0);
b0.addActionListener(new Number());
pl = new JButton("+");
p.add(pl);
pl.addActionListener(new Number());
mi = new JButton("-");
p.add(mi);
mi.addActionListener(new Number());
mu = new JButton("*");
p.add(mu);
mu.addActionListener(new Number());
di = new JButton("/");
p.add(di);
di.addActionListener(new Number());
cal = new JButton("=");
p.add(cal);
cal.addActionListener(new Number());
c = new JButton("C");
p.add(c);
c.addActionListener(new Number());
t1.setBounds(10, 10, 245, 80);
b1.setBounds(10, 105, 45, 45);
b2.setBounds(60, 105, 45, 45);
b3.setBounds(110, 105, 45, 45);
b4.setBounds(10, 155, 45, 45);
b5.setBounds(60, 155, 45, 45);
b6.setBounds(110, 155, 45, 45);
b7.setBounds(10, 205, 45, 45);
b8.setBounds(60, 205, 45, 45);
b9.setBounds(110, 205, 45, 45);
b0.setBounds(60, 255, 45, 45);
pl.setBounds(160, 105, 45, 45);
mi.setBounds(160, 155, 45, 45);
mu.setBounds(160, 205, 45, 45);
di.setBounds(160, 255, 45, 45);
c.setBounds(110, 255, 45, 45);
cal.setBounds(210, 105, 45, 195);
add(p);
setVisible(true);
}
private class Number implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
t1.setText(t1.getText() + "1");
} else if (e.getSource() == b2) {
t1.setText(t1.getText() + "2");
} else if (e.getSource() == b3) {
t1.setText(t1.getText() + "3");
} else if (e.getSource() == b4) {
t1.setText(t1.getText() + "4");
} else if (e.getSource() == b5) {
t1.setText(t1.getText() + "5");
} else if (e.getSource() == b6) {
t1.setText(t1.getText() + "6");
} else if (e.getSource() == b7) {
t1.setText(t1.getText() + "7");
} else if (e.getSource() == b8) {
t1.setText(t1.getText() + "8");
} else if (e.getSource() == b9) {
t1.setText(t1.getText() + "9");
} else if (e.getSource() == b0) {
t1.setText(t1.getText() + "0");
} else if (e.getSource() == c) {
t1.setText("");
}
// + - * / x
// 0 1 2 3 4
else if (e.getSource() == pl) {
if (operator == 4) {
x = t1.getText();
t1.setText("");
operator = 0;
} else {
process();
x = "" + valX;
t1.setText("");
operator = 0;
}
} else if (e.getSource() == mi) {
if (operator == 4) {
x = t1.getText();
t1.setText("");
operator = 1;
} else {
process();
x = "" + valX;
t1.setText("");
operator = 1;
}
} else if (e.getSource() == mu) {
if (operator == 4) {
x = t1.getText();
t1.setText("");
operator = 2;
} else {
process();
x = "" + valX;
t1.setText("");
operator = 2;
}
} else if (e.getSource() == di) {
if (operator == 4) {
x = t1.getText();
t1.setText("");
operator = 3;
} else {
process();
x = "" + valX;
t1.setText("");
operator = 3;
}
} else if (e.getSource() == cal) {
process();
t1.setText("" + valX);
}
}
int valX;
int valY;
public void process() {
y = t1.getText();
valX = Integer.parseInt(x);
valY = Integer.parseInt(y);
switch (operator) {
case 0:
valX = valX + valY;
break;
case 1:
valX = valX - valY;
break;
case 2:
valX = valX * valY;
break;
case 3:
valX = valX / valY;
break;
}
operator = 4;
}
}
}
public class cals {
public static void main(String[] args) {
Cal Cals = new Cal();
}
}
'Programming > JAVA,JSP' 카테고리의 다른 글
JAVA 오목 게임 #1 (0) | 2012.11.23 |
---|---|
JAVA 프레임 안에 프레임 (1) | 2012.11.18 |
JAVA TCP 통신 예제 (0) | 2012.11.17 |
Java 계산기 레이아웃 (0) | 2012.11.17 |
setLookAndFeel 예제 (0) | 2012.11.04 |