owned this note
owned this note
Published
Linked with GitHub
# 簡易計算機OnAction事件
* 參考bmi按鈕 OnAction事件 設定方法:
```java=
btn.setOnAction( new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
String str1=itext1.getText();
String str2=itext2.getText();
double h=Double.parseDouble(str1);
double w=Double.parseDouble(str2);
h=h/100.0; // cm--> m
double bmi=w/(h*h);
label3.setText("BMI值:"+ bmi);
}
} );
```
# 可改成Lambda表達式
```java
btn.setOnAction( (t)-> { //也可 t -> {body}
String str1=itext1.getText();
String str2=itext2.getText();
double h=Double.parseDouble(str1);
double w=Double.parseDouble(str2);
h=h/100.0; // cm--> m
double bmi=w/(h*h);
label3.setText("BMI值:"+ bmi);
} );
```
# 宣告成Global
```java
Button btn0, btn1, btn2, btn3, btn4, btn5;
Button btn6, btn7, btn8, btn9, btnDot, btnC;
Button btnPlus, btnMin, btnMult, btnDiv, btnCal;
TextField itext;
double d1=0, d2=0, result=0;
int flag=0;
```
```java=
itext=new TextField();
HBox hbox1=new HBox(10);
HBox hbox2=new HBox(10);
HBox hbox3=new HBox(10);
HBox hbox4=new HBox(10);
VBox vbox=new VBox(10);
btn0=new Button("0");
btn1=new Button("1");
btn2=new Button("2");
btn3=new Button("3");
btn4=new Button("4");
btn5=new Button("5");
btn6=new Button("6");
btn7=new Button("7");
btn8=new Button("8");
btn9=new Button("9");
btnDot=new Button(".");
btnC=new Button("C");
btnPlus=new Button("+");
btnMin=new Button("-");
btnMult=new Button("*");
btnDiv=new Button("/");
btnCal=new Button(" = ");
initAction();
```
```java
void initAction(){
btn0.setOnAction(e->{});
點擊1按鈕時 textField內容要加"1"
1. 要取得textField的內容
2. str2 取到內容後加1..
3. 判斷str2是否合乎數字..
若合乎數字 才設定設定textField的內容
}
```
```java
void initAction(){
btn1.setOnAction(e->{
//1. 要取得textField的內容
String str1=itext.getText();
//2. 取到內容後加1..
String str2=str1 +"1";
try{
Double.parseDouble(str2);
//3. 設定textField的內容.
itext.setText(str2);
}
catch(Exception ee){}
});
btnDot.setOnAction(e->{
String str1=itext.getText(); //1. 要取得textField的內容
String str2=str1 +".";//2. 取到內容後加1..
try{
Double.parseDouble(str2);
itext.setText(str2);//3. 設定textField的內容.
}
catch(Exception ee){}
});
//按C 清除
btnC.setOnAction(e->{
itext.setText("");
});
}
按 alt+shift+F 格式化你的程式碼
```
# 把btn改成btn0, btn1, ..., btn9..等等.
```java=
btn0.setOnAction( (t)-> { //改成 t-->{body} 也可.
//itext加入"0", 步驟如下:
//1.取得目前itext中的內容字串 String old=itext.getText();
//2.將 old字串後面加上"0". String str=old .....
//將新字串str設給itext. itext.setText(.....);
} );
btn1.setOnAction( (t)-> {
//itext加入"1"
//1.取得目前itext中的內容字串 String old=itext.getText();
//2.將 old字串後面加上"1". String str=old .....
//將新字串str設給itext. itext.setText(.....);
} );
btn2.setOnAction( (t)-> {
//itext加入"2"
//1.取得目前itext中的內容字串 String old=itext.getText();
//2.將 old字串後面加上"2". String str=old .....
//將新字串str設給itext. itext.setText(.....);
} );
...
btn9.setOnAction( (t)-> {
//itext加入"9"
} );
btnDot.setOnAction( (t)-> {
//itext加入"."
} );
btnC.setOnAction( (t)-> {
//itext清除程式碼
itext.SetText("");
} );
btnPlus.setOnAction( (t)-> {
//設定 加法運算
} );
btnMin.setOnAction( (t)-> {
//設定 減法運算
} );
btnMult.setOnAction( (t)-> {
//設定 乘法運算
} );
btnDiv.setOnAction( (t)-> {
//設定 除法運算
} );
btnCal.setOnAction( (t)-> {
// = 計算運算
} );
```
# 設全域變數 存下操作的中間過程 double d1, d2, result;
```java
例如 按 +,-,* / 之前的字串或數字.
例如 itext目前內容為 12 當按下 "+"號時,
要存下 12, 並將itext內容清空. <== d1內容應為12
```
# Double.parseDouble(字串)
```java
將字串轉為浮點數, 可使用Double.parseDouble(itext.getText())
d1=Double.parseDouble(itext.getText());
```
# 考慮按 "="號的動作:
```java
當按下 "="號時, 假設目前itext內容為10 <== d2內容應為10
則計算 d1+d2 結果為22 將結果顯示於itext.
```
# 問題: 當按下 "="號, 怎知是做那種運算?
因之前有按 "+"號, 因此按 "+"號時 要作記號.
# 使用全域變數 flag存下運算符號. int flag=0;
```java
當按下 "+"號時, flag 設成0, 代表 "+"號 <-btnPlus Action事件
當按下 "-"號時, flag 設成1, 代表 "-"號 <-btnMin Action事件
當按下 "*"號時, flag 設成2, 代表 "*"號 <-btnMult Action事件
當按下 "/"號時, flag 設成3, 代表 "/"號 <-btnDiv Action事件
```
```java
btnCal.setOnAction(e->{
String str1=itext.getText();
d2=Double.parseDouble(str1);
if(flag==0) result=d1+d2;
else if(flag==1) result=d1-d2;
else if(flag==2) result=d1*d2;
else{ //flag=3 /除法 可能出錯.
if(d2==0) return; //0不做除法
result=d1/d2;
}
itext.setText(""+result);
});
```
<hr>
```java
btnPlus.setOnAction( (t)-> {
//設定 加法運算
//1.要存下 12, 並將itext內容清空
String str1=itext.getText();
itext.setText("");
d1=Double.parseDouble(str1);
flag=0; //加.
} );
```
<hr>
```java
btnCal.setOnAction( (t)-> {
//= 計算運算
} );
```