전자계산기 자바 전자계산기 만들기에서
public class Main {
public static void main(String args[]) {
Calculator cal = new Calculator("AWT 계산기");
cal.run();
}
}
-----------------------------------------------
import java.awt.*;
import java.awt.event.*;
//Panel 클래스를 상속받아 구현한다.
public class ButtonPanel extends Panel {
private Button bt[] = new Button[18];
private ActionListener bal = null;
public ButtonPanel(TextField tf) {
super();
//텍스스필드의 레퍼런스를 이벤트 리스너로 전달
bal = new ButtonActionListener(tf);
//버튼 패널객체의 레이아웃 설정
setLayout(new GridLayout(5,5));
//패널에 올라간 버튼객체를 만든다.
bt[0] = new Button("0");
bt[1] = new Button("1");
bt[2] = new Button("2");
bt[3] = new Button("3");
bt[4] = new Button("4");
bt[5] = new Button("5");
bt[6] = new Button("6");
bt[7] = new Button("7");
bt[8] = new Button("8");
bt[9] = new Button("9");
bt[10] = new Button("/");
bt[11] = new Button("*");
bt[12] = new Button("-");
bt[13] = new Button("+");
bt[14] = new Button("C");
bt[15] = new Button("=");
bt[16] = new Button("(");
bt[17] = new Button(")");
//버튼의 엑션리스너를 등록
for(int index = 0; index < bt.length; index++){
bt[index].addActionListener(bal);
}
//패널에 객체를 순서대로 추가한다.
add(bt[7]);
add(bt[8]);
add(bt[9]);
add(bt[10]);
add(bt[4]);
add(bt[5]);
add(bt[6]);
add(bt[11]);
add(bt[1]);
add(bt[2]);
add(bt[3]);
add(bt[12]);
add(bt[0]);
add(bt[14]);
add(bt[15]);
add(bt[13]);
add(bt[16]);
add(bt[17]);
}
}
------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class Calculator implements WindowListener {
//사용자 인테페이스 구성을 위한 AWT 클래스 레퍼런스 변수
private Frame f = null;
private TextField tf = null;
private Panel tp = null;
private ButtonPanel bp = null;
public Calculator(String fName) {
//새로운 프레임을 만든다.
f = new Frame(fName);
}
public void run() {
//프레임의 윈도우리스터 등록
f.addWindowListener(this);
//프레임의 레이아웃, 위치, 크기 설정
f.setLayout(new BorderLayout());
f.setBounds(10, 10, 200, 250);
//입력값과 결과값 출력을 위한 텍스트필드
tf = new TextField(20);
tf.setEditable(false);
//텍스트필드를 올리기위한 패널 객체를 생성한다.
tp = new Panel();
tp.setBounds(0,30, 200, 40);
//텍스트 필드를 패널 객체에 추가한다.
tp.add(tf);
tp.setVisible(true);
tp.show(true);
//각종 버튼을 포함하고 있는 패털 클래스 객체를 생성한다.
bp = new ButtonPanel(tf);
bp.setBounds(0,70, 200, 180);
bp.setVisible(true);
bp.show(true);
//생성된 패널객체를 프레임 객체에 추가한다.
f.add("North",tp);
f.add("Center",bp);
//프레임을 화면에 출력한다.
f.setVisible(true);
}
public void windowClosing(WindowEvent we) {
System.out.println("계산기 종료");
f.setVisible(false);
f.dispose();
System.exit(0);
}
public void windowOpened(WindowEvent we) {
System.out.println("계산기 시작");
}
public void windowClosed(WindowEvent we) {
System.out.println("계산기 종료될때");
}
public void windowIconified(WindowEvent we) {
System.out.println("계산기 최소화");
}
public void windowDeiconified(WindowEvent we) {
System.out.println("계산기 원래 크기로");
}
public void windowDeactivated(WindowEvent we) {
System.out.println("계산기에서 윈도우 비활성화");
}
public void windowActivated(WindowEvent we) {
System.out.println("계산기 활성화");
}
}
--------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
//ActionListener 인터페이스를 구현한다.
public class ButtonActionListener implements ActionListener {
private int iFlag = 0;
private String strAct = null;
private TextField tf = null;
private String strOP = null;
private String strTmp = null;
//버튼 이벤트에 따라 TextField 객체를 제어하기위해
//계산기의 TextField 객체의 레퍼런스를 받아온다.
public ButtonActionListener(TextField tf) {
this.tf = tf;
}
//Action Event를 처리하기 위한 메서드
public void actionPerformed(ActionEvent ae) {
strAct = ae.getActionCommand();
//이벤트의 내용이 사칙연산자라면
//TextField의 내용을 멤버변수 strTmp에 저장한다.
if(strAct.equals("/") || strAct.equals("*") ||
strAct.equals("-") || strAct.equals("+")) {
strTmp = tf.getText();
strOP = strAct;
iFlag = 1;
}else if(strAct.equals("=")){
// =이면 strOP에 저장된 연산자에 따라 연산을 수행하고
// 결과를 TextField에 출력한다.
if(iFlag == 2) {
long f = Long.parseLong(strTmp);
long e = Long.parseLong(tf.getText());
if(strOP.equals("+")) {
tf.setText(""+(f+e));
}else if(strOP.equals("-")) {
tf.setText(""+(f-e));
}else if(strOP.equals("/")) {
if(e==0||f==0)
tf.setText("Error");
else
tf.setText(""+(f/e));
}else if(strOP.equals("*")) {
tf.setText(""+(f*e));
}
iFlag = 0;
}
}else if(strAct.equals("C")){
//C 라면 TextField의 내용을 0으로 수정한다.
iFlag = 0;
tf.setText("0");
}else {
//숫자 입력이 들어오면 TextField의 내용을 갱신한다.
if(iFlag == 1) {
iFlag = 2;
tf.setText("0");
}
if(tf.getText().equals("0")) {
tf.setText(strAct);
}else {
tf.setText(tf.getText()+strAct);
}
}
}
}
예를들어 5*3*(2+4) 하면 괄호 ()부터 연산처리되게 만들고 싶은데 잘 안되네요.
고수님들 부탁드립니다.
전자계산기정보모음추천 순위선정사이트 보기
클릭하시면 전자계산기관련 추천사이트에 대한 정보를 한눈에 쉽게 보실 수 있습니다.
전자계산기자바 전자계산기 만들기에서
전자계산기
댓글 없음:
댓글 쓰기