import java.awt.*;
import java.awt.event.*;
class Calc implements ActionListener{
Button btn[] = new Button[10];
Button btnMinus = new Button("-");
Button btnPlus = new Button("+");
Button btnMul = new Button("*");
Button btnDiv = new Button("/");
Button btnDot = new Button(".");
Button btnEqual = new Button("=");
Button btnClear = new Button("C");
Button btnCE = new Button("CE");
Button btnBack = new Button("BS");
Button btnDummy1 = new Button("");
TextField txtAnswer = new TextField("0");
TextField txtInput = new TextField("");
int sig;
static final int NONE = 0;
static final int MINUS = 1;
static final int PLUS = 2;
static final int MUL = 3;
static final int DIVIDE = 4;
public Calc(){
/* フレームを作成します。*/
Frame frm = new Frame();
/* レイアウトを設定します。*/
frm.setLayout(new BorderLayout());
/* フレームのサイズを設定します。*/
frm.setSize(new Dimension(200,200));
/* パネルを作成します。*/
Panel pnl1 = new Panel();
Panel pnl2 = new Panel();
/* 各パネルのレイアウトを設定します */
pnl1.setLayout(new GridLayout(2,1));
pnl2.setLayout(new GridLayout(5,4));
/* 数字ボタンを作成し、リスナー登録 */
for(int i = 0 ; i < btn.length ; i++ ){
btn[i] = new Button((new Integer(i)).toString());
btn[i].addActionListener(this);
}
/* パネルにボタンとテキストフィールドを追加します。 */
pnl1.add(txtInput);
pnl1.add(txtAnswer);
pnl2.add(btnBack);
pnl2.add(btnCE);
pnl2.add(btnClear);
pnl2.add(btnEqual);
pnl2.add(btn[7]);
pnl2.add(btn[8]);
pnl2.add(btn[9]);
pnl2.add(btnDiv);
pnl2.add(btn[4]);
pnl2.add(btn[5]);
pnl2.add(btn[6]);
pnl2.add(btnMul);
pnl2.add(btn[1]);
pnl2.add(btn[2]);
pnl2.add(btn[3]);
pnl2.add(btnMinus);
pnl2.add(btn[0]);
pnl2.add(btnDummy1);
pnl2.add(btnDot);
pnl2.add(btnPlus);
/* 領域を確保するだけのダミーボタンは非表示にします。*/
btnDummy1.setVisible(false);
/* リスナーに登録します。*/
btnMinus.addActionListener(this);
btnPlus .addActionListener(this);
btnMul .addActionListener(this);
btnDiv .addActionListener(this);
btnDot .addActionListener(this);
btnEqual.addActionListener(this);
btnClear.addActionListener(this);
btnBack .addActionListener(this);
btnCE .addActionListener(this);
/* テキストフィールドは編集不可とします。*/
txtInput.setEditable(false);
txtAnswer.setEditable(false);
/* パネルをフレームに登録します。*/
frm.add(pnl1, BorderLayout.NORTH);
frm.add(pnl2, BorderLayout.CENTER);
/* フレームを表示させます。*/
frm.setVisible(true);
/* ダミーボタンがあった場所は背景が白なので、グレーに変えます。*/
/* この部分の解説は後述します。*/
frm.setBackground(btnDummy1.getBackground());
}
/* ボタンが押された場合に呼ばれるメソッドです。*/
/* 通常、ここは押されたボタンを判断して、メソッドを呼ぶだけにとどめます。*/
/* ここですべてのボタンの処理を書き込むと長くなってプログラムのメンテナンス性が落ちるからです。*/
public void actionPerformed(ActionEvent e){
/* 本来ならばキャスト可能かをinstanceof演算して確認するか、try〜catchします。*/
Component c = (Component)e.getSource();
if( c == btnMinus )
minus(); /* -(減算)ボタン*/
else if( c == btnPlus )
plus(); /* +(加算)ボタン*/
else if( c == btnMul )
mul(); /* *(乗算)ボタン*/
else if( c == btnDiv )
divide(); /* /(除算)ボタン*/
else if( c == btnEqual )
equal(); /* =ボタン*/
else if( c == btnBack )
backspace(); /* BS(バックスペース)ボタン*/
else if( c == btnClear )
clear(); /* C(すべてクリア)ボタン*/
else if( c == btnCE )
ce(); /* CE(入力した数値のみクリア)ボタン*/
else if( c == btnDot )
period(); /* .(小数点)ボタン*/
else
number((Button)c); /* 数字ボタン*/
}
/* 数字キーが押された場合 */
private void number(Button b){
/* 最初に0は受け付けない */
if( b == btn[0] && txtInput.getText().length() == 0 )
return;
/* 計算が終了している時に、次の数字が押されたら結果領域をクリアする */
if( sig == NONE )
txtAnswer.setText("");
/* 入力領域に数字を入れる */
txtInput.setText( txtInput.getText() + b.getLabel() );
}
/* ピリオド(小数点)が押された場合 */
private void period(){
/* 数値の最初にピリオドが入れられたら、"0."と表示する。 */
if( txtInput.getText().length() == 0 ){
txtInput.setText("0");
}
/* すでに小数点が入力されていなければ、ピリオドを追加する */
if( txtInput.getText().indexOf('.' ) == -1 ){
txtInput.setText( txtInput.getText() + "." );
}
}
/* +,-, /, * =が押された場合に呼ばれる計算するメソッド */
double calculate(double an, double in, int sig){
switch(sig){
case MINUS :
return an - in;
case PLUS :
return an + in;
case MUL :
return an * in;
case DIVIDE :
return an / in;
}
/* sigが四則計算以外の場合 */
/* ここに来るのはプログラムミスの時。*/
/* これを追加しないとコンパイラでエラーが出る */
return Double.NaN;
}
/* -(減算)ボタンが押された時の処理 */
private void minus(){
/* 四則計算ボタンがまだ押されていない場合 */
if( sig == NONE ){
/* 数字が入力されていなかったら何もしない */
/* 数字が入力されていたら、入力のテキストフィールドの数字を結果領域に移動する。*/
if( txtInput.getText().length() != 0 ){
txtAnswer.setText(txtInput.getText());
txtInput.setText("");
}
/* 四則計算ボタンが押されていた場合、*/
/* 入力フィールドに数字が入っていればその計算をする。*/
/* 入っていなければ何もしない */
}else{
double in = getNumber(txtInput .getText());
double an = getNumber(txtAnswer.getText());
if( !Double.isNaN(in) ){
txtAnswer.setText(String.valueOf( calculate(an, in, sig) ));
txtInput.setText("");
}
}
/* −ボタンが押されたことを記憶する。*/
sig = MINUS;
}
/* +(加算)ボタンが押された時の処理 */
/* 以下、−の時とほとんど同じなので、コメントは割愛する。*/
private void plus(){
if( sig == NONE ){
if( txtInput.getText().length() != 0 ){
txtAnswer.setText(txtInput.getText());
txtInput.setText("");
}
}else{
double in = getNumber(txtInput .getText());
double an = getNumber(txtAnswer.getText());
if( in != Double.NaN ){
txtAnswer.setText(String.valueOf( calculate(an, in, sig) ));
txtInput.setText("");
}
}
sig = PLUS;
}
/* *(乗算)ボタンが押された時の処理 */
private void mul(){
if( sig == NONE ){
if( txtInput.getText().length() != 0 ){
txtAnswer.setText(txtInput.getText());
txtInput.setText("");
}
}else{
double in = getNumber(txtInput .getText());
double an = getNumber(txtAnswer.getText());
if( in != Double.NaN ){
txtAnswer.setText(String.valueOf( calculate(an, in, sig) ));
txtInput.setText("");
}
}
sig = MUL;
}
/* /(除算)ボタンが押された時の処理 */
private void divide(){
if( sig == NONE ){
if( txtInput.getText().length() != 0 ){
txtAnswer.setText(txtInput.getText());
txtInput.setText("");
}
}else{
double in = getNumber(txtInput .getText());
double an = getNumber(txtAnswer.getText());
if( in != Double.NaN ){
/* 本来ならば0で割り算しないことを確認する方がよい。*/
/* Javaではdouble型の場合0で割ると[Infinity:無限]となり、エラーとならない。*/
txtAnswer.setText(String.valueOf( calculate(an, in, sig) ));
txtInput.setText("");
}
}
sig = DIVIDE;
}
/* =ボタンが押された */
private void equal(){
if( sig == NONE ){
if( txtInput.getText().length() != 0 ){
txtAnswer.setText(txtInput.getText());
txtInput.setText("");
}
}else{
double in = getNumber(txtInput .getText());
double an = getNumber(txtAnswer.getText());
if( !Double.isNaN(in) ){
txtAnswer.setText(String.valueOf( calculate(an, in, sig) ));
txtInput.setText("");
}
}
sig = NONE;
}
/* バックスペースが押された場合、入力フィールドから1文字消す。*/
private void backspace(){
String str = txtInput.getText();
int length = str.length();
/* 1文字以上入力されているか */
if( length > 0 ){
/* 1文字削除 */
str = str.substring(0, length-1);
/* 1文字削除されて0だけ残ったら、それも削除する。*/
if( str.equals("0") )
str = "";
txtInput.setText(str);
}
}
/* 入力フィールドをクリアする */
private void ce(){
txtInput.setText("");
}
/* すべてをクリアする */
private void clear(){
txtInput.setText("");
txtAnswer.setText("");
sig = NONE;
}
/* 文字列を数字に変換する。 */
private double getNumber(String s){
try{
return Double.valueOf(s).doubleValue();
}catch(Exception e){
return Double.NaN;
}
}
public static void main(String args[]){
new Calc();
}
}
|