owned this note
owned this note
Published
Linked with GitHub
# BMI計算器0
[javafx.scene.layout.VBox](
https://docs.oracle.com/javase/10/docs/api/javafx/scene/layout/VBox.html)
[javafx.scene.layout.HBox](
https://docs.oracle.com/javase/10/docs/api/javafx/scene/layout/HBox.html)
[javafx.scene.control.Label](
https://docs.oracle.com/javase/10/docs/api/javafx/scene/control/Label.html)
[javafx.scene.control.TextField](
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextField.html)
[javafx.scene.control.Button](
https://docs.oracle.com/javase/10/docs/api/javafx/scene/control/Button.html)
畫面設計:
Label 身高 TextField
Label 體重 TextField
Button 計算
Label BMI值
<hr>
VBOX{
HBOX: Label TextField
HBOX: Label TextField
Button
Label
}
```java=
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.shape.*; //Circle, Rectangle
import javafx.scene.paint.Color;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class BMI0 extends Application {
public static void main(String[] args) {
System.out.println("main()");
launch(args); //launch為Application中的靜態方法
//Application.launch(args);
System.out.println("after launch()");
}
@Override
public void start(Stage stage) {
HBox hbox1=new HBox(10);// spacing = 10
TextField input_h=new TextField();
input_h.setPromptText("輸入身高"); //to set the hint text
hbox1.getChildren().addAll(new Label("身高"), input_h);
HBox hbox2=new HBox(10);// spacing = 10
TextField input_w=new TextField();
input_w.setPromptText("輸入體重"); //to set the hint text
hbox2.getChildren().addAll(new Label("體重"), input_w);
Label lab_bmi=new Label("BMI值:");
Button btn=new Button("計算");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
//計算bmi 程式碼.
}
});
VBox vbox=new VBox();
vbox.getChildren().addAll(hbox1, hbox2, btn, lab_bmi);
//VBox.setMargin(hbox1, new Insets(10, 10, 10, 10));
//VBox.setMargin(hbox2, new Insets(10, 10, 10, 10));
Scene scene = new Scene(vbox, 400, 300);
//stage.getIcons().add(new Image("file:resources/images/address_book_32.png"));
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
@Override
public void stop(){
System.out.println("stop");
}
}
```
* 從TextField抓出身高體重值.
* 計算BMI
* 修改輸出格式
* 修改字體大小..
# 從TextField抓出身高體重值
TextField.getText();
TextField.getText();
# Double.parseDouble()
double h=...;
double w=...;
h=h/100.0;
double bmi=w/(h*h);
# Label.setText("bmi值:"+bmi)
# 修改輸出到小數第2位
DecimalFormat df = new DecimalFormat("###.##");
String str=df.format(bmi);
Label.setText(...);
# 修改字體大小(使用css設定)
https://motleybytes.com/w/JavaFxFonts
```java
vbox.setStyle("-fx-font: 20pt 'verdana';");
```
verdana為 font-family
[JavaFX Fonts](https://jenkov.com/tutorials/javafx/fonts.html)
[JavaFxFonts](https://motleybytes.com/w/JavaFxFonts)
```java=
String fontFamily = "Arial";
double fontSize = 13;
FontWeight fontWeight = FontWeight.BOLD;
FontPosture fontPosture = FontPosture.ITALIC;
Font font1 = Font.font(fontFamily);
Font font2 = Font.font(fontSize);
Font font3 = Font.font(fontFamily, fontSize);
Font font4 = Font.font(fontFamily, fontWeight , fontSize);
Font font5 = Font.font(fontFamily, fontPosture, fontSize);
Font font6 = Font.font(fontFamily, fontWeight , fontPosture, fontSize);
```
```java=
Font font = Font.font("Arial");
Text text = new Text("This is the text");
text.setFont(font);
```
```java=
List<String> fontFamilies = Font.getFamilies();
List<String> fontNames = Font.getFontNames();
for(String item : fontFamilies) {
System.out.println(item);
}
for(String item : fontNames) {
System.out.println(item);
}
```