JAVA 계산기 실습 중간코드

실습과제 모음 2013. 2. 16. 10:41 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

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 oldValue;


public Cal() {


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() == pl)

{

oldValue = 

Integer.parseInt(t1.getText());

t1.setText("");

}

else if(e.getSource() == mi)

{

oldValue = 

Integer.parseInt(t1.getText());

t1.setText("");

}

else if(e.getSource() == mu)

{

oldValue = 

Integer.parseInt(t1.getText());

t1.setText("");

}

else if(e.getSource() == di)

{

oldValue = 

Integer.parseInt(t1.getText());

t1.setText("");

}

else if(e.getSource() == c)

{

t1.setText("");

}


}


}


}




public class cals {


public static void main(String[] args) {


Cal Cals = new Cal();




}


}

'실습과제 모음' 카테고리의 다른 글

JAVA 가계부 프로그램  (0) 2013.03.02
JAVA 직원 관리 프로그램 작성  (0) 2013.03.02
C 언어 실습문제 모음  (0) 2013.01.09
CPP 실습과제 1208  (0) 2012.12.08
123  (0) 2012.11.03