Form(表單、表格)在視窗程式或是網頁中,都是很常見的顯示方式。以下就以「登入表單」來作為 JavaFX Form 的練習吧!

在 Hello JavaFX 中,我們已經知道 Stage 和 Scene 的關係,也大概知道要如何製作出 JavaFX 的視窗程式。因此底下將會直接從 Scene 開始製作出我們想要的 Form。

建立登入表單

Layout(Pane)

Layout 的概念,在 Awt 的時代就有了,可以藉由特殊的排版演算法來對畫面中的 UI 控制項進行較有彈性的排版。在 JavaFX 中,也提供了 javafx.scene.layout APIs,經典的 Border、Flow、Grid 等排版方式也都還存在。只是到了 JavaFX,Layout 有個新的名稱──Pane。Layout 也可以直接作為 Scene 的 Root node。

要排出一個登入表單,Oracle 提供的作法是用 GridPane。

GUI 元件

要製作登入表單,我們需要將 Text、Label 和 TextField、PasswordField、Button 加到 GridPane 內。但在這邊很奇怪的是,Text 和 Label 差別在哪呢?以繼承關係來看,Text 屬於 shape 底下的子類別,而 Label 屬於 control 底下的子類別,這表示 Text 並不算是控制項,而是需要被 draw 出來的「形狀」。在以往的 AWT 或是 Swing 中,沒有提供類似 Text 的套件,所以這在 JavaFX 中算是一個新概念:Layout 中不再只能放置控制項,形狀也可以被放入 Layout 進行排版!

登入事件(Event)

在按下表單上的登入按鈕後,通常要執行一段驗證帳號密碼是否正確,然後判斷是否登入成功的程式。要如何在按鈕被按下後執行這段程式,就得靠 EventHandler 介面了,直接用 Lambda 語法來實作該介面即可。

撰寫程式

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class LoginJavaFX extends Application {

    @Override
    public void start(Stage primaryStage) {
        //Layout
        GridPane grid = new GridPane(); //Grid排版
        grid.setAlignment(Pos.CENTER); //Layout置中
        grid.setHgap(10); //水平距離
        grid.setVgap(10); //垂直距離

        grid.setPadding(new Insets(25, 25, 25, 25)); //填充邊界。Insets定義上、右、下、左四個方向的長度。

        //用Layout作為Scene的root node
        Scene scene = new Scene(grid, 300, 275);
        primaryStage.setScene(scene);

        //GUI元件
        Text scenetitle = new Text("歡迎");
        scenetitle.setFont(Font.font("Serif", FontWeight.NORMAL, 20));
        grid.add(scenetitle, 0, 0, 2, 1); //2,1是指這個元件要佔用2格的column和1格的row

        Label userName = new Label("帳號:");
        grid.add(userName, 0, 1);

        TextField userTextField = new TextField();
        grid.add(userTextField, 1, 1);

        Label pw = new Label("密碼:");
        grid.add(pw, 0, 2);

        PasswordField pwBox = new PasswordField();
        grid.add(pwBox, 1, 2);

        Button btn = new Button("登入");
        HBox hbBtn = new HBox(btn); //以新的HBox Layout來對Button進行排版,目的是為了讓Button在GridPane 1,4的位置中可以靠到最右邊。
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        grid.add(hbBtn, 1, 4);

        //登入事件
        final Text actiontarget = new Text(); //顯示結果的Text
        grid.add(actiontarget, 1, 6);
        btn.setOnAction(e -> { //用lambda語法省略實作EventHandler介面
            actiontarget.setFill(Color.FIREBRICK);
            actiontarget.setText("登入成功!");
        });

        //顯示Stage
        primaryStage.setTitle("登入");
        primaryStage.show();
    }
}

執行結果:

JavaFX 登入表單的執行結果