//import java.awt.*;
//import java.awt.event.*;
//import javax.swing.*;
//
//public class Main extends JFrame {
// private JLabel la1 = new JLabel("0");
// private JLabel la2 = new JLabel("0");
// private JLabel la3 = new JLabel("0");
// private JLabel la4 = new JLabel("시작합니다");
// public Main() {
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Container c = getContentPane();
// c.setLayout(null);
// c.add(la1);
// la1.setSize(60,60);
// la1.setLocation(40,20);
// c.add(la2);
// la2.setSize(60,60);
// la2.setLocation(60,20);
// c.add(la3);
// la3.setSize(60,60);
// la3.setLocation(80,20);
// c.add(la4);
// la4.setSize(80,80);
// la4.setLocation(40,40);
// c.addKeyListener(new MyKeyListener());
// c.setFocusable(true);
// c.requestFocus();
// setSize(250, 250);
// setVisible(true);
// }
//
// class MyKeyListener extends KeyAdapter {
// public void keyPressed(KeyEvent e) {
// if(e.getKeyChar() == '\n'); {
// int x = (int)(Math.random()*2);
// la1.setText(Integer.toString(x));
// int y = (int)(Math.random()*2);
// la2.setText(Integer.toString(y));
// int z = (int)(Math.random()*2);
// la3.setText(Integer.toString(z));
// if(x==y && y==z) {
// la4.setText("축하합니다!!");
// }
// else {
// la4.setText("아쉽군요");
// }
// }
// }
// }
//
// public static void main(String[] args) {
// new Main();
// }
//}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class Main extends JFrame{
JTextField tf1 = new JTextField(13);
JTextField tf2 = new JTextField(11);
public Main() {
setTitle("계산기 프레임");
Container c = getContentPane();
c.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
c.add(p1, BorderLayout.NORTH);
c.add(p2, BorderLayout.CENTER);
c.add(p3, BorderLayout.SOUTH);
p1.setBackground(Color.GRAY);
p1.setLayout(new FlowLayout());
p2.setBackground(Color.WHITE);
p2.setLayout(new GridLayout(4,4,1,1));
p3.setBackground(Color.YELLOW);
p3.setLayout(new FlowLayout());
p1.add(new JLabel("수식입력"));
p1.add(tf1);
p3.add(new JLabel("계산 결과"));
p3.add(tf2);
String[] arr = {"CE","계산","+","-","x","/"};
for(int i=0;i<=9;i++) {
JButton b = new JButton(Integer.toString(i));
b.addActionListener(new MyActionListener());
p2.add(b);
}
for(int i=0;i<=5;i++){
JButton b = new JButton(arr[i]);
b.addActionListener(new MyActionListener());
p2.add(b);
if(i>=2) {
b.setBackground(Color.CYAN);
}
}
setSize(400,300);
setVisible(true);
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
String a = b.getText();
if(a.equals("CE")) { //tf1 글자없애기
tf1.setText("");
}
else if(a.equals("계산")) { //계산결과 tf2에 settext하기
//1. 한자리숫자 연산자 한자리숫자
String sik = tf1.getText();
int aa = sik.charAt(0)-48;
char x = sik.charAt(1);
int bb = sik.charAt(2)-48;
int res;
if(x=='+') {
res = aa+bb;
}
else if(x=='-') {
res = aa-bb;
}
else if(x=='/') {
res = aa/bb;
}
else {
res = aa*bb;
}
tf2.setText(Integer.toString(res));
}
else {
tf1.setText(tf1.getText()+a);
}
}
}
public static void main(String[] args) {
new Main();
}
}