//이벤트 리스너
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
JTextField te=new JTextField(15);
public Main() {
setSize(300,300);
setVisible(true);
Container c=getContentPane();
c.setLayout(new BorderLayout());
//North Panel
JPanel p1=new JPanel();
p1.setBackground(Color.LIGHT_GRAY);
c.add(p1,BorderLayout.NORTH);
JLabel la=new JLabel("수식입력");
p1.add(la);
p1.add(te);
//Center Panel
JPanel p2=new JPanel();
p2.setBackground(Color.WHITE);
p2.setLayout(new GridLayout(4,4));
c.add(p2,BorderLayout.CENTER);
for(int i=0;i<=9;i++) {
String si= Integer.toString(i);
JButton b1=new JButton(si);
b1.addActionListener(new MyActionListener()); ////액션리스너 객체 붙여주기
p2.add(b1);
}
p2.add(new JButton("CE"));
p2.add(new JButton("계산"));
p2.add(new JButton("+"));
p2.add(new JButton("-"));
p2.add(new JButton("x"));
p2.add(new JButton("/"));
//South Panel
JPanel p3=new JPanel();
p3.setBackground(Color.YELLOW);
c.add(p3,BorderLayout.SOUTH);
JLabel la2=new JLabel("계산결과");
p3.add(la2);
JTextField te2= new JTextField(15);
p3.add(te2);
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
// e : 이벤트에 대한 정보가 들어있는 객체 ( 어디서? 언제? .. )
JButton b = (JButton)e.getSource(); //b : 이벤트가 일어난 버튼
//숫자 버튼을 누르면, 수식입력 옆의 텍스트필드에 그 숫자가 출력되도록 ( hint. te.setText("??"))
String a = te.getText();
String c = b.getText();
te.setText(a+c);
if(te.getText().length()==10) te.setText(" ");
//눌린 버튼의 숫자가 홀수이면 글자색을 red로, 짝수이면 blue로 변경
//System.out.println(b.getText());
// int num = Integer.valueOf(b.getText());
// if(num%2==0) {
// b.setForeground(Color.blue);
// }
// else {
// b.setForeground(Color.red);
// }
// if(b.getBackground().equals(Color.blue)) b.setBackground(Color.red);
// else b.setBackground(Color.blue);
}
}
public static void main(String[] args) {
Main frame=new Main();
}
}
*/