| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 네이버 클라우드 플랫폼
- GitHub Mirroring
- 실행시간 측정
- GitLab Mirroring
- 초대장
- 프레시업 #풀무원 #하루한병 #건강만들기 #풀무원 녹즙
- '티스토리 초대장/ 티스토리초대'
- 음료같은녹즙
- 유니옥션
- webstorm
- 티스토리초대
- 니돈내먹
- Path Alias
- currentTimeMillis
- gitlab 연동
- 프리티어
- visual studio code cli
- GitHub 미러링
- React Native
- code 설치
- code .
- GitLab미러링
- code 세팅
- react native #gradle
- eslint-import-resolver-typescript
- visual studio code
- settings sync
- 티스토리 초대장
- Emmet
- 티스토리 초대장/ 티스토리초대
Archives
- Today
- Total
방치하기
홍대 자바 수업:계산기 임 깜박하고 int로해서 소수점안됨 시간도 늦어서 그냥 제출 본문
반응형
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 );
}
}
}
반응형
'홍익대 Java > 과제' 카테고리의 다른 글
| 학생 3명 객체 생성후 . 과목 3개 값 대입 하고 평균 구해주고 석차 순위대로 출력(야매.. 정식 은 조만간 올리겠음) 덤으로 버그도 있다 . 토탈이 같을경우는 문제가 발생한다 ㅎ 고치기 귀찮아서 그냥 했을뿐 (0) | 2009.07.21 |
|---|---|
| Calclulator 핵심은 스캐너 겠지 . (0) | 2009.07.20 |
| 현금 입출기 (0) | 2009.07.18 |
| in.read in.skip 사칙 연산자 (0) | 2009.07.16 |
| 주민 번호님 인수로 받아서 남녀 구별해주기 .substring (0) | 2009.07.16 |
Comments