awt로 된 완전 초보 계산기입니다..
웹개발만 하다보니 기초자바가 재미있네영..^^;;
import java.awt.*;
import java.awt.event.*;
public class CalcCreateExam extends Frame
{
Frame f;
Panel pl_0,pl_1;
MenuBar mb;
Menu m1,m2,m3;
MenuItem menu_exit;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button bs,ce,bc;
Button bmc,bmr,bms,bmplus;
Button bslush,bsq,bminus,bplus;
Button bresult;
TextField result_tf;
// button_al;
int result_number = 0;
char ch,privous_ch;
String str_Result = "";
String privous_number = "";
String non_check_number = "";
String final_result_number = "";
public CalcCreateExam()
{
f= new Frame("자바계산기");
//main = new Panel(new GridLayout());
pl_0 = new Panel(new GridLayout());
mb = new MenuBar();
f.setMenuBar(mb);
m1=new Menu("편집");
m2=new Menu("보기");
m3=new Menu("도움말");
menu_exit = new MenuItem("나가기");
mb.add(m1);
mb.add(m2);
mb.add(m3);
m1.add(menu_exit);
result_tf = new TextField();
//result_tf.setAlignment();
//System.out.println();
pl_0.add(result_tf);
pl_1 = new Panel(new GridLayout(4,4));
//f = new Frame();
//result_tf = new TextField(10);
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b0 = new Button("0");
bc = new Button("C");
bplus = new Button("+");
bminus = new Button("-");
bslush = new Button("/");
bsq = new Button("*");
//bplus = new Button("+");
//bplus = new Button("+");
bresult = new Button("=");//결과버튼
pl_1.add(b9);
pl_1.add(b8);
pl_1.add(b7);
pl_1.add(b6);
pl_1.add(b5);
pl_1.add(b4);
pl_1.add(b3);
pl_1.add(b2);
pl_1.add(b1);
pl_1.add(b0);
pl_1.add(bplus);
pl_1.add(bminus);
pl_1.add(bslush);
pl_1.add(bsq);
pl_1.add(bc);
pl_1.add(bresult);
//pl.setLayout(new GridLayout());
f.add(pl_1,BorderLayout.CENTER);
f.add(pl_0,BorderLayout.NORTH);
//f.add(main);
//이벤트 구현
ActionListener button_al = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
//System.out.println(ae.getSource().getText());
non_check_number = (String)ae.getActionCommand();
if(non_check_number.length()==1)
{
ch = non_check_number.charAt(0);
if(Character.isDigit(ch))//숫자인지판단
{
str_Result += non_check_number;
final_result_number = str_Result;
//System.out.println("!!!!!!!!!!! ++++ : "+str_Result);
}
else
{
switch(ch)
{
case '=':
Caculate(privous_ch);
break;
case 'C':
privous_number = "";
str_Result = "";
//privous_math=new char('');
non_check_number = "";
//System.out.println("c");
break;
default :
privous_ch=ch;
break;
}
privous_number = str_Result;
final_result_number = non_check_number;
str_Result = "";//초기화
}
}
//System.out.println("결과창 : " + non_check_number);
result_tf.setText(final_result_number);
}
};//ActionListener
b1.addActionListener(button_al);
b2.addActionListener(button_al);
b3.addActionListener(button_al);
b4.addActionListener(button_al);
b5.addActionListener(button_al);
b6.addActionListener(button_al);
b7.addActionListener(button_al);
b8.addActionListener(button_al);
b9.addActionListener(button_al);
b0.addActionListener(button_al);
bresult.addActionListener(button_al);
bplus.addActionListener(button_al);
bminus.addActionListener(button_al);
bslush.addActionListener(button_al);
bsq.addActionListener(button_al);
bc.addActionListener(button_al);
menu_exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
}
public void Caculate(Character ch1)
{
//System.out.println("111111111,"+privous_number+","+str_Result);
int number1 = Integer.parseInt(privous_number);
int number2 = Integer.parseInt(str_Result);
//System.out.println("22222222,"+number1+":"+number2);
switch(ch1)
{
case '+' :
number1+=number2;
//System.out.println("result : "+number1);
break;
case '-' :
number1-=number2;
break;
case '*' :
number1*=number2;
break;
case '/' :
number1/=number2;
break;
//default :
//System.out.println(result_tf.getText());
}
//텍스트창에서 안나옴..==;
non_check_number = Integer.toString(number1);
//System.out.println(number1+math+number2);
}
public void CreateFrame()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setSize(300,250);
f.setVisible(true);
}
public static void main(String[] args)
{
CalcCreateExam cce = new CalcCreateExam();
cce.CreateFrame();
}
}
완전 허접하다...하지만 조금 더 좋아질 것 같다..ㅋㅋ