방치하기

홍대 자바 수업:계산기 임 깜박하고 int로해서 소수점안됨 시간도 늦어서 그냥 제출 본문

홍익대 Java/과제

홍대 자바 수업:계산기 임 깜박하고 int로해서 소수점안됨 시간도 늦어서 그냥 제출

Yi Junho 2009. 7. 29. 23:53
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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 );
     }
    }
}
반응형
Comments