Javafx 入门教程前言

  声明:   为什么要学习Javafx, 我学的目的是希望能够使用java做一些工具。当然,使用Java 不是一个最好的选择,但是谁让我只会Java呢?所以有时间就学习了下javafx。

      在了解 Javafx 后,准备借助 SceneBuilder 工具 逐渐探索 javafx 各个组件,以及参考bootstrap 书写一个 Javafx 的bootstrap css 样式。

  组成元素

  首先需要一个窗口,也就是 Stage,在窗口上设置场景,在场景上进行布局,布局上面显示组件

  一个简单的案例

  

package com.sxmd;

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;

/**
 * Description:
 *
 * @author cy
 * @date 2019年10月30日 8:55
 * Version 1.0
 */
public class WebsocketToolApplication extends Application implements EventHandler<MouseEvent> {
    Button button;

    @Override
    public void start(Stage primaryStage) {
        // 窗口显示
        primaryStage.show();
        // 创建一个按钮
        button = new Button("按钮");
        button.setOnMouseClicked(this);
        // 创建一个布局
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(button);
        // 创建一个场景
        Scene scene = new Scene(stackPane,200,200);
        scene.setOnMousePressed(this);
        primaryStage.setScene(scene);

    }

    public static void main(String[] args) {
        System.out.println("========程序开始启动=======");
        launch(args);
    }

    @Override
    public void handle(MouseEvent event) {
        if(event.getSource() == button){
            System.out.println("你点击了按钮");
        }else {
            System.out.println("你点击了场景");
        }
    }
}
原文地址:https://www.cnblogs.com/chengyangyang/p/11762885.html