Ex15_5
/*---------------------------
Ex15_5.java
イベント処理:ボタンで電卓?
----------------------------*/
package jp_bunkyo;
import java.applet.*;
import java.awt.*;
import java.awt.event.*; // イベント処理用
public class Ex15_5 extends Applet implements ActionListener {
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; // ボタン:数値キー用
Button c0,c1,c2,c3,c4; // ボタン:演算キー用
String msg; // メッセージ用
int x, y, z; // x 演算 y = z
boolean flg = false, flg0 = false, flg1 = false, flg2 = false, flg3 = false, flg4 = false; // 演算用フラグ
public void init() { // 初期化
this.setSize(280,120); //描画領域:280×120
msg = ""; // メッセージをクリアしておく
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
c0 = new Button("+");
c1 = new Button("−");
c2 = new Button("×");
c3 = new Button("÷");
c4 = new Button("=");
add(b0); add(b1); add(b2); add(b3); add(b4); add(b5); add(b6); add(b7); add(b8); add(b9);
add(c0); add(c1); add(c2); add(c3); add(c4);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
c0.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
c4.addActionListener(this);
}
public void paint(Graphics g) {
g.drawString("簡易電卓", 10, 50);
g.drawString("注)一桁の計算のみで,答えは整数だけだよ", 10, 115);
g.setFont(new Font("Elephant", Font.BOLD + Font.ITALIC, 42));
g.drawString(msg, 10, 100);
}
public void actionPerformed(ActionEvent e) {
if (flg4 == true) { flg4 = false; flg = false; msg = ""; } // "="ボタンが押されたら初期化
if (e.getSource() == b0) { msg += " 0 "; if (flg == false) x=0; else y=0; }; // "1"ボタン押下時
if (e.getSource() == b1) { msg += " 1 "; if (flg == false) x=1; else y=1; }; // "2"ボタン押下時
if (e.getSource() == b2) { msg += " 2 "; if (flg == false) x=2; else y=2; };
if (e.getSource() == b3) { msg += " 3 "; if (flg == false) x=3; else y=3; };
if (e.getSource() == b4) { msg += " 4 "; if (flg == false) x=4; else y=4; };
if (e.getSource() == b5) { msg += " 5 "; if (flg == false) x=5; else y=5; };
if (e.getSource() == b6) { msg += " 6 "; if (flg == false) x=6; else y=6; };
if (e.getSource() == b7) { msg += " 7 "; if (flg == false) x=7; else y=7; };
if (e.getSource() == b8) { msg += " 8 "; if (flg == false) x=8; else y=8; };
if (e.getSource() == b9) { msg += " 9 "; if (flg == false) x=9; else y=9; };
if (e.getSource() == c0) { msg += "+"; flg0 = true; flg = true; }; // "+"ボタン押下時
if (e.getSource() == c1) { msg += "-"; flg1 = true; flg = true; }; // "-"ボタン押下時
if (e.getSource() == c2) { msg += "*"; flg2 = true; flg = true; };
if (e.getSource() == c3) { msg += "/"; flg3 = true; flg = true; };
if (e.getSource() == c4) { msg += "= "; flg4 = true; flg = true; }; // "="ボタン押下時
if (flg0 == true && flg4 == true) { z = x + y; msg += z; flg0 = false; }; // "="ボタン押下後"+"演算
if (flg1 == true && flg4 == true) { z = x - y; msg += z; flg1 = false; }; // "="ボタン押下後"-"演算
if (flg2 == true && flg4 == true) { z = x * y; msg += z; flg2 = false; }; // "="ボタン押下後"*"演算
if (flg3 == true && flg4 == true) { z = x / y; msg += z; flg3 = false; }; // "="ボタン押下後"/"演算
repaint(); // 再描画
}
}