홍익대 Java/과제
홍대 자바 수업:계산기 임 깜박하고 int로해서 소수점안됨 시간도 늦어서 그냥 제출
Yi Junho
2009. 7. 29. 23:53
반응형
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame
{
static int c;
static int i=0;
StringBuffer mem01=new StringBuffer();
StringBuffer mem02=new StringBuffer();
char mem03='v';
char [] in=new char [10];
//숫자 입력받는곳
private JTextField input;
//각 버튼들
private Button []btn = new Button[23];
//각 버튼에 들어갈 이름
private String btnText[] = {
"BackSpace", "C", "CE",
"MC", "7", "8", "9", "/",
"MR", "4", "5", "6", "*",
"MS", "1", "2", "3", "-",
"M+", "0", ".", "+", "="
};
//버튼의 너비와 높에
int btn_width = 65;
int btn_height = 20;
//버튼의 시작 x좌표 y좌표
int x = 12, y = 40;
public Calculator()
{
super("계산기");
//패널의 레이아웃 설정
this.setLayout( null );
//메소드 호출
this.initComponent();
//윈도우 리스너 1
}
private class Handler implements ActionListener {
public void actionPerformed( ActionEvent event )
{
String st=event.getActionCommand();
in=st.toCharArray();
if(Character.isDigit(in[i])&&(mem03=='v'))
{
mem01.append(in[i]);
st=mem01.substring(0);
System.out.println(mem01);
input.setText(st);
}
else if (Character.isDigit(in[i])&&!(mem03=='v'))
{
mem02.append(in[i]);
st=mem02.substring(0);
input.setText(st);
}
else if (in[0]=='=')
{
st=mem02.substring(0);
int a=Integer.parseInt(st);
st=mem01.substring(0);
int b=Integer.parseInt(st);
if(mem03=='+'){
c=a+b;
input.setText(Integer.toString(c));
System.out.println(c);
}
else if (mem03=='-')
{
c=a-b;
input.setText(Integer.toString(c));
}
else if (mem03=='*')
{
c=a*b;
input.setText(Integer.toString(c));
}
else if (mem03=='/')
{
c=a/b;
input.setText(Integer.toString(c));
}
mem01.setLength(0);
mem02.setLength(0);
mem01.append(Integer.toString(c));
}
else
{
mem03=in[i];
System.out.println(mem03);
input.setText("");
}
}
}
public void initComponent()
{
//TextField객체 생성
input = new JTextField();
Handler hand=new Handler();
//버튼 객채 생성하면서 각 버튼에 표시될 text주기
for( int index = 0; index < btn.length; index++ )
{
btn[index] = new Button( btnText[index] );
btn[index].addActionListener( hand );
}
//TextField위치시키기
this.insert( input, x, y, btn_width*5, btn_height );
//y시작좌표 증가시키기
y = y + btn_height;
//맨위의 3가지 버튼 위치시키기 BackSpace, C, CE
this.insert( btn[0], x, y, btn_width * 2, btn_height );
this.insert( btn[1], x + 95, y, btn_width * 2, btn_height );
this.insert( btn[2], x + 195, y, btn_width * 2, btn_height );
//y좌표 증가시키
y = y + btn_height;
//3개의 버튼은 이미 위치시켰으므로 btn배열의 갯수에서 3개 부족하게 반복하면서 버튼 추가하기
for( int index = 0; index < btn.length - 3; index++ )
{
//버튼삽입
this.insert( btn[index+3], x, y, btn_width, btn_height );
//x값을 버튼의 넓이 만큼 우측으로 이동
x = x + btn_width;
//버튼을 6개 넣었다면
if( (index+1)%5 == 0 )
{
//x좌표값을 10으로 변경
x = 12;
//y좌표값을 버튼의 높이만큼 옮김
y = y + btn_height;
}
}
}
//컴포넌트를 패널에 위치하기위한 메소드
private void insert(Component cmpt, int x, int y, int w, int h)
{
cmpt.setSize( w, h );
cmpt.setLocation( x, y );
this.add( cmpt ); }
//메인 메소드
static public void main( String arg[] )
{
Calculator c = new Calculator();
c.setSize( 350, 200 );
c.setVisible( true );
}
class windo extends WindowAdapter
{
public void windowClosing( WindowEvent we )
{
System.exit( 0 );
}
}
}
반응형