Awtcalculation.java



package FrameTest;


import java.awt.Button;

import java.awt.Frame;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.StringTokenizer;

import java.util.Vector;


import javax.naming.spi.DirStateFactory.Result;

import javax.swing.JTextField;


public class Awtcalculation extends Frame implements ActionListener{

private boolean FirstArithmetic; // 첫번째에 사칙연산 삽입 금지

private String beforeChar = ""; // 사칙연산 연속 입력 방지 

private String showText = ""; // 계산식 저장

private Vector<String> arrayAwtcalculation = new Vector<String>(50); // 사칙연산 저장하는 배열

private float lastValue; // = 후에 남은 값 저장

private int maxSize =0;

private float result = 0;


StringTokenizer st;

JTextField Calresult;

public Awtcalculation() {

super("Awtcalculation");

GridBagLayout gridbag = new GridBagLayout();

GridBagConstraints constraint = new GridBagConstraints();


constraint.fill = GridBagConstraints.BOTH;


/** TextField Result */

constraint.gridwidth = 4;

constraint.gridwidth =

GridBagConstraints.REMAINDER;

Calresult = new JTextField("");

Calresult.setHorizontalAlignment(JTextField.RIGHT);

gridbag.setConstraints(Calresult, constraint);

add(Calresult);


/** Button */

constraint.gridwidth = 2;

Button backBtn = new Button("←");

gridbag.setConstraints(backBtn, constraint);

add(backBtn);

backBtn.addActionListener(new ButtonListener());


constraint.gridwidth =

GridBagConstraints.REMAINDER;

Button clearBtn = new Button("C");

gridbag.setConstraints(clearBtn, constraint);

add(clearBtn);

clearBtn.addActionListener(new ButtonListener());

constraint.weightx = 1.0;

constraint.gridwidth = 1;

Button numSevenBtn = new Button("7");

gridbag.setConstraints(numSevenBtn, constraint);

add(numSevenBtn);

numSevenBtn.addActionListener(new ButtonListener());

Button numEightBtn = new Button("8");

gridbag.setConstraints(numEightBtn, constraint);

add(numEightBtn);

numEightBtn.addActionListener(new ButtonListener());

Button numNineBtn = new Button("9");

gridbag.setConstraints(numNineBtn, constraint);

add(numNineBtn);

numNineBtn.addActionListener(new ButtonListener());

constraint.gridwidth =

GridBagConstraints.REMAINDER;

constraint.weightx = 1.0;

Button numDeivideBtn = new Button("/");

gridbag.setConstraints(numDeivideBtn, constraint);

add(numDeivideBtn);

numDeivideBtn.addActionListener(new ButtonListener());

constraint.gridwidth = 1;

Button numFourBtn = new Button("4");

gridbag.setConstraints(numFourBtn, constraint);

add(numFourBtn);

numFourBtn.addActionListener(new ButtonListener());

Button numFiveBtn = new Button("5");

gridbag.setConstraints(numFiveBtn, constraint);

add(numFiveBtn);

numFiveBtn.addActionListener(new ButtonListener());

Button numSixBtn = new Button("6");

gridbag.setConstraints(numSixBtn, constraint);

add(numSixBtn);

numSixBtn.addActionListener(new ButtonListener());

constraint.gridwidth = constraint.REMAINDER;

Button numMultplyBtn = new Button("*");

gridbag.setConstraints(numMultplyBtn, constraint);

add(numMultplyBtn);

numMultplyBtn.addActionListener(new ButtonListener());

constraint.gridwidth = 1;

Button numOneBtn = new Button("1");

gridbag.setConstraints(numOneBtn, constraint);

add(numOneBtn);

numOneBtn.addActionListener(new ButtonListener());

Button numTwoBtn = new Button("2");

gridbag.setConstraints(numTwoBtn, constraint);

add(numTwoBtn);

numTwoBtn.addActionListener(new ButtonListener());


Button numThreeBtn = new Button("3");

gridbag.setConstraints(numThreeBtn, constraint);

add(numThreeBtn);

numThreeBtn.addActionListener(new ButtonListener());


constraint.gridwidth = constraint.REMAINDER;

Button numMinusBtn = new Button("-");

gridbag.setConstraints(numMinusBtn, constraint);

add(numMinusBtn);

numMinusBtn.addActionListener(new ButtonListener());

constraint.gridwidth = 1;

Button numZeroBtn = new Button("0");

gridbag.setConstraints(numZeroBtn, constraint);

add(numZeroBtn);

numZeroBtn.addActionListener(new ButtonListener());

Button numConBtn = new Button(".");

gridbag.setConstraints(numConBtn, constraint);

add(numConBtn);

numConBtn.addActionListener(new ButtonListener());

Button numEqualsBtn = new Button("=");

gridbag.setConstraints(numEqualsBtn, constraint);

add(numEqualsBtn);

numEqualsBtn.addActionListener(new ButtonListener());

Button numPlusBtn = new Button("+");

gridbag.setConstraints(numPlusBtn, constraint);

add(numPlusBtn);

numPlusBtn.addActionListener(new ButtonListener());

/** 프레임 닫기 이벤트 */

addWindowListener(

new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

);


/** Frame set */

setLayout(gridbag);

setLocation(700, 200);

// setSize(400,400); // pack(); 사용 가능

pack();

setVisible(true);

}

private class ButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e){

System.out.println("~~~~~~~~~~~~~~~~~~"+result);

e.getActionCommand();

System.out.println("받아온 값 : "+e.getActionCommand());

System.out.println("length : " + showText.length());

// 시작이 사칙연산으로 시작되면 안됨.

if(showText.isEmpty() || showText == "" || showText == null){

FirstArithmetic = false;

}else{

FirstArithmetic = true;

}

/** 숫자 및 뒤로가기, 초기화 */

if(e.getActionCommand() == "C"){

beforeChar = "";

showText= "";

result =0;

System.out.println(result+"@@@@@@@@@@@@@@@@@@@@");

} else if(e.getActionCommand() == "←"){

showText = showText.substring(0,showText.length()-1);

} else if(e.getActionCommand() == "0" || e.getActionCommand() == "1" || e.getActionCommand() == "2" || e.getActionCommand() == "3" || e.getActionCommand() == "4" || e.getActionCommand() == "5" || e.getActionCommand() == "6" || e.getActionCommand() == "7" || e.getActionCommand() == "8" || e.getActionCommand() == "9"){

showText += beforeChar = e.getActionCommand();

}

System.out.println("-----------------"+FirstArithmetic);

/** 사칙연산 */

System.out.println(beforeChar);

if ((e.getActionCommand() == "+" ||  e.getActionCommand() == "-" || e.getActionCommand() == "*" || e.getActionCommand() == "/" || e.getActionCommand() == ".") && FirstArithmetic){

if(!(beforeChar.equals("+") || beforeChar.equals("-") || beforeChar.equals("*") || beforeChar.equals("/") || beforeChar.equals("="))){ // 사칙연산 연속 두번 방지

beforeChar = "";

showText += beforeChar = e.getActionCommand();

}

}

System.out.println("쌓이는 값 : " + showText);

Calresult.setText(showText);

if (e.getActionCommand() == "="){

if(!e.getActionCommand().equals(beforeChar)){

Calculation(showText);

}

}

}

public void Calculation(String str){

if(str.contains("+") || str.contains("-") || str.contains("*") || str.contains("/")){

int i = 0; // 홀수는 값 , 짝수는 사칙연산

st = new StringTokenizer(str , "+-*/" , true);


arrayAwtcalculation.clear();

while(st.hasMoreTokens()){

arrayAwtcalculation.addElement(String.valueOf(st.nextToken()));

}

System.out.println(arrayAwtcalculation);

result += Float.parseFloat(arrayAwtcalculation.elementAt(0));

for(int z = 0; z < arrayAwtcalculation.size(); z++){

if(arrayAwtcalculation.elementAt(z).equals("+")){

result += Float.parseFloat(arrayAwtcalculation.elementAt(z+1));  

} else if(arrayAwtcalculation.elementAt(z).equals("-")){

result -= Float.parseFloat(arrayAwtcalculation.elementAt(z+1));

} else if(arrayAwtcalculation.elementAt(z).equals("*")){

result *= Float.parseFloat(arrayAwtcalculation.elementAt(z+1));

} else if(arrayAwtcalculation.elementAt(z).equals("/")){

result /= Float.parseFloat(arrayAwtcalculation.elementAt(z+1));

}

}

System.out.println(result);

Calresult.setText(String.valueOf(result));

}else{

;

}

}

}

public static void main(String args[]){

new Awtcalculation();

}

}

'개발언어 > JAVA' 카테고리의 다른 글

GUI 계산기 Vector 사용  (0) 2015.03.27
[Java Thread]Concurrent Programming  (0) 2015.03.08
Posted by QuickPerson

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.