天天看點

Java: JavaFX桌面GUI開發

1、基本概念

視窗          Stage
  -場景       Scene
    -布局     stackPane
      -控件   Button      

2、最小架構代碼

建立指令行應用

package com.company;

import javafx.application.Application;
import javafx.stage.Stage;


public class HelloWorld extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}      

對就是啥都沒有,空白的窗體

Java: JavaFX桌面GUI開發

3、控件布局

package com.company;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 執行個體化按鈕
        Button button = new Button("這是按鈕上的文字");

        // 建立布局控件
        StackPane stackPane = new StackPane();

        // 将button添加到布局
        stackPane.getChildren().add(button);

        // 建立場景 寬=400 高=400
        Scene scene = new Scene(stackPane, 400, 400);

        // 将場景添加到視窗
        primaryStage.setScene(scene);

        // 顯示視窗
        primaryStage.show();
    }
}
      
Java: JavaFX桌面GUI開發

4、事件添加

Main.java

package com.company;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class Main extends Application implements EventHandler<MouseEvent> {
    private Button button;

    public static void main(String[] args) {
        // write your code here
//        System.out.println("你好");
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 執行個體化按鈕
        button = new Button("這是按鈕");

        // 1、添加按鈕點選事件, this.handle 處理事件
//        button.setOnMouseClicked(this);

//        2、使用單獨實作的類 事件監聽
//        button.setOnMouseClicked(new MyMouseEvent());

//        3、使用匿名類添加事件監聽
        button.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("滑鼠點選按鈕了");
            }
        });

//        4、jdk 8  使用簡寫執行一條輸出
        button.setOnMouseClicked(e -> System.out.println("簡寫的監聽事件"));

//        5、同時輸出多條
        button.setOnMouseClicked(e -> {
            System.out.println("簡寫的監聽事件1");
            System.out.println("簡寫的監聽事件2");
        });

        // 建立布局控件
        StackPane stackPane = new StackPane();

        // 将button添加到布局
        stackPane.getChildren().add(button);

        // 建立場景
        Scene scene = new Scene(stackPane, 400, 400);

        // 給場景添加事件處理的對象
//        scene.setOnMousePressed(this);
        scene.setOnMousePressed(new MySceneMouseEvent());

        // 将場景添加到視窗
        primaryStage.setScene(scene);

        // 顯示視窗
        primaryStage.show();
    }

    @Override
    public void handle(MouseEvent event) {

        // event.getSource() 擷取事件對象
        if (event.getSource() == button) {
            System.out.println("點選了按鈕");
        } else {
            System.out.println("點選了場景");
        }
    }
}
      

MyMouseEvent.java 處理滑鼠點選事件的類

package com.company;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;

public class MyMouseEvent implements EventHandler<MouseEvent> {
    @Override
    public void handle(MouseEvent event) {
        System.out.println("MyMouseEvent click");
    }
}
      

MySceneMouseEvent.java 處理場景點選事件的類

package com.company;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;

public class MySceneMouseEvent implements EventHandler<MouseEvent> {
    @Override
    public void handle(MouseEvent event) {
        System.out.println("場景滑鼠點選");
    }
}
      

5、場景切換

package com.company;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class SceneChange extends Application {
    Scene scene1,  scene2;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 場景1
        Button button1 = new Button("場景1 的button");

        // 事件監聽 點選後切換到場景2
        button1.setOnMouseClicked(e -> {
            primaryStage.setScene(scene2);
        });

        VBox vBox = new VBox();
        vBox.getChildren().add(button1);
        scene1 = new Scene(vBox, 400, 400);

        // 場景2
        Button button2 = new Button("場景2 的button");

        // 事件監聽 點選後切換到場景1
        button2.setOnMouseClicked(e -> {
            primaryStage.setScene(scene1);
        });

        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(button2);
        scene2 = new Scene(stackPane, 400, 400);

        primaryStage.setScene(scene1);
        primaryStage.show();
    }
}
      
Java: JavaFX桌面GUI開發
Java: JavaFX桌面GUI開發

6、窗體切換

package com.company;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {
    private Stage stage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;

        // 視窗點選叉号關閉詢問
        stage.setOnCloseRequest(event -> {
            event.consume();  // 消除預設事件
            handleClose();
        });

        // 布局
        Button button = new Button("關閉視窗");

        // 滑鼠點選關閉視窗
        button.setOnMouseClicked(event -> handleClose());

        VBox vBox = new VBox();
        vBox.getChildren().add(button);
        Scene scene = new Scene(vBox, 400, 400);

        stage.setScene(scene);
        stage.show();
    }

    public void handleClose() {
        // 接收窗體傳回值
        boolean ret = WindowAlert.display("關閉視窗", "是否關閉視窗?");
        System.out.println(ret);
        if (ret) {
            stage.close();
        }

    }

    public static void main(String[] args) {
        launch(args);
    }
}
      

WindowAlert.java

package com.company;

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;


public class WindowAlert {
    public static boolean answer;

    /**
     * @param title 标題
     * @param msg   消息
     */
    public static boolean display(String title, String msg) {
        // 建立舞台
        Stage stage = new Stage();

        // 設定顯示模式
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle(title);

        // 建立控件
        Button buttonYes = new Button("是");
        buttonYes.setOnMouseClicked(event -> {
            answer = true;
            stage.close();
        });

        Button buttonNo = new Button("否");
        buttonNo.setOnMouseClicked(event -> {
            answer = false;
            stage.close();
        });

        Label label = new Label(msg);

        // 建立布局
        VBox vBox = new VBox();
        vBox.getChildren().addAll(label, buttonYes, buttonNo);
        vBox.setAlignment(Pos.CENTER); // 布局居中顯示

        // 建立場景
        Scene scene = new Scene(vBox, 200, 200);

        // 顯示舞台
        stage.setScene(scene);
//        stage.show();
        stage.showAndWait();  // 等待窗體關閉才繼續

        // 窗體傳回值
        return answer;
    }
}
      
Java: JavaFX桌面GUI開發