| 小数計算もできます。 |
|
import java.applet.*;
import java.awt.*;
import java.util.*;
public class ja206 extends Applet
{
ja206Keisan Keisan = new ja206Keisan(); //ja206Keisanクラスのインスタンス
public void init(){
setLayout(new BorderLayout()); //コンポーネント配置方法をBorderLayoutに設定
Panel PA = new Panel(); //パネルの作成
PA.setLayout(new GridLayout(5,4,4,4)); //パネル1にGridLayoutを設定
Button B = new Button(); //ボタンを作成
Font f = new Font("TimesRoman",Font.ITALIC,15);
B.setFont(f); //ボタンのフォントを設定
B.setBackground(Color.lightGray); //ボタンの背景色を灰色に,文字色を赤に
B.setForeground(Color.red);
//(注意)JDK1.02では,Buttonのフォント,背景色,前景色は変更できないようだ。
PA.add(B = new Button("7")); //各ボタンを作成し,パネルに配置する
PA.add(B = new Button("8"));
PA.add(B = new Button("9"));
PA.add(B = new Button("/"));
PA.add(B = new Button("4"));
PA.add(B = new Button("5"));
PA.add(B = new Button("6"));
PA.add(B = new Button("*"));
PA.add(B = new Button("1"));
PA.add(B = new Button("2"));
PA.add(B = new Button("3"));
PA.add(B = new Button("-"));
PA.add(B = new Button("."));
PA.add(B = new Button("0"));
PA.add(B = new Button("+/-"));
PA.add(B = new Button("+"));
PA.add(B = new Button("C"));
PA.add(B = new Button("="));
add("North",Keisan); //ja206Keisanクラスのインスタンスを上部に,パネルを中央に配置
add("Center",PA);
}
public boolean action(Event ev,Object arg){
if(ev.target instanceof Button){ //ボタンがクリックされた場合
String la = (String)arg; //選択したボタンのラベルを取得
if(la.equals("C")){ //ボタンのラベルが"C"の場合
Keisan.Clear(); //ja206KeisanクラスのClear()メソッドを呼び出す
return true;
}else if(la.equals(".")){
Keisan.Ten();
return true;
}else if(la.equals("+")){
Keisan.Sisoku();
Keisan.n = 1; //四則計算の種類を識別するフラグ
return true;
}else if(la.equals("-")){
Keisan.Sisoku();
Keisan.n = 2;
return true;
}else if(la.equals("*")){
Keisan.Sisoku();
Keisan.n = 3;
return true;
}else if(la.equals("/")){
Keisan.Sisoku();
Keisan.n = 4;
return true;
}else if(la.equals("+/-")){ //数値の+と-を反転する
Keisan.Hanten();
return true;
}else if(la.equals("=")){
Keisan.Dewa();
return true;
}
else{ //それ以外(数値ボタン)の場合
Keisan.Suji(la);
return true;
}
}
return false;
}
}
class ja206Keisan extends Panel //Panelクラスを継承したサブクラス
{
Label LA = new Label("ここに表示します"); //ラベルの作成
String s = "0"; //表示する数字
double total = 0; //計算結果の数値
String totals; //計算結果の数値を文字列として保持する
public int n = 0; //四則計算(+ - * /) を識別するフラグに利用
ja206Keisan(){ //コンストラクタの定義
setBackground(Color.yellow); //パネルの背景色を設定
setFont(new Font("TimesRoman",Font.BOLD + Font.ITALIC,20));
LA.setBackground(Color.white); //ラベルの背景色を設定
LA.setAlignment(1); //ラベルの配置を中央揃えに
add("Center",LA); //パネルの中央にラベルを配置
}
void Suji(String su){ //数字ボタン(0〜9)が押された時に呼び出される
s = s + su; //suはボタンに表記されている数字
if(s.length() == 2 && s.charAt(0) == '0' && s.charAt(1) != '.'){
//小数でなく,先頭に0の付いた数字の場合
s = s.substring(1); //文字列の2文字目以降を取得(即ち,先頭の0を除去する)
}
LA.setText(s); //ラベルに表示
}
void Hanten(){ //[+/-]ボタンが押された場合
if(s.length() == 0)return; //文字列が無い場合,呼び出し元に制御を戻す。
if(s.charAt(0) == '-')s = s.substring(1); //先頭が-記号の場合,その-記号を除く
else s = "-" + s; //先頭が-記号で無い場合,-記号を付加する
LA.setText(s);
}
void Ten(){ //ドット[.]ボタンが押された場合
if(s.indexOf('.') != -1){ //文字列 s に既に小数点(.)が含まれている場合
return;
}else{
s = s + ".";
}
LA.setText(s);
}
void Sisoku(){ //+,-,*,/の四則計算をする
if(s.length() == 0)return;
Double d = Double.valueOf(s); //Double型に変換する
switch(n){
case 1: //足し算の場合
total = total + d.doubleValue();
break;
case 2: //引き算の場合
total = total - d.doubleValue();
break;
case 3: //掛け算の場合
total = total * d.doubleValue();
break;
case 4: //割り算の場合
total = total / d.doubleValue();
break;
default: //上記以外の場合
total = d.doubleValue();
break;
}
totals = new Double(total).toString(); //Double型をString型に変換
LA.setText(totals);
s = "0";
}
void Dewa(){ //[=]ボタンが押された場合
if(s.length() == 0)return;
Double d = Double.valueOf(s);
switch(n){
case 1:
total = total + d.doubleValue();
break;
case 2:
total = total - d.doubleValue();
break;
case 3:
total = total * d.doubleValue();
break;
case 4:
total = total / d.doubleValue();
break;
default:
total = d.doubleValue();
break;
}
totals = new Double(total).toString();
LA.setText(totals);
s = "0";
}
void Clear(){ //[C]ボタンが押された場合
total = 0; //計算結果,フラグ,文字列を初期化
n = 0;
s = "0";
LA.setText(s);
}
}
末尾