在上一篇文章中,我們一起學習了jlink和jdk14的模塊化. 在本篇文章中,我們一起來學習gradle來新建一個javafx的Hello World 程序.
本文中這段最重要這段準備講一下, 為什麼要學習javafx, 小刀學這個, 是出於自己的興趣,當時是公司用的k8s,但是想看日誌啥的,還要先打開網頁,登錄阿里雲,然後選對應的集群,然後找到對應的命名空間,然後找到對應的容器組,然後再點日誌. 一個還好, 要是有兩個以上的容器組,看日誌就相當的麻煩了.
然後我就做了這個可以直接查看k8s日誌的東東. 寫著寫著, 就想,其實可以把改host, 連redis,連資料庫這些簡單的小功能都做一起. 然後做著做著,就有了小刀平時在朋友圈發的工具. 然後現在陸陸續續寫了幾個月了, 自我感覺對javaFX的一些常用的方法和坑都踩了一些,所以開始更新javaFX系列. 希望能對大家有所幫助 工具包下載地址: https://download.lixiang.red/sunflower/current/
有什麼問題,歡迎反饋給小刀~
環境準備gradle 6.3及以上(因為6.3之後才開始支持jdk14)
openJDK14(上篇文章有小夥伴留言說jdk14商用不免費,我們這裡特別聲明了是openJDK14)
idea 社區版 2020.1及以上(其實社區版功能完全是夠用的)
小刀的微信交流群(一個人學哪行, 來, 這裡有很多少夥伴陪你一起呢)
文件結構和平常的gradle項目沒有什麼兩樣, 文件結構如下所示:
這也是最簡的javafx程序的結構,有一個fxml做頁面布局,有一個Main函數,有一個Controller進行事件處理
主要代碼build.gradleplugins { id 'application' id 'org.openjfx.javafxplugin' version '0.0.8'}javafx { version = "14" modules = [ 'javafx.controls' ,'javafx.fxml']}sourceCompatibility = '11'mainClassName = 'red.lixiang.tools.sunflower/red.lixiang.tools.sunflower.SunflowerMain'compileJava.options.encoding = 'UTF-8'
dependencies {
}repositories{ mavenLocal() maven { url 'https://maven.aliyun.com/repository/public/' } maven { url 'https://maven.aliyun.com/repository/spring/'} mavenCentral()}sunflower.fxml<?import javafx.scene.control.Button?><?import javafx.scene.control.Label?><?import javafx.scene.layout.AnchorPane?><?import javafx.scene.layout.VBox?>
<AnchorPane fx:id="pane" prefHeight="640" prefWidth="480.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="red.lixiang.tools.sunflower.SunflowerController"> <children> <VBox layoutX="223.0" layoutY="640.0" prefHeight="640.0" prefWidth="480.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <children> <Button fx:id="button" mnemonicParsing="false" text="button" /> <Label fx:id="label" text="texttext" visible="false" /> </children> </VBox> </children></AnchorPane>SunflowerMain.javapublic class SunflowerMain extends Application {
@Override public void start(Stage stage) { FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(getClass().getResource("/sunflower/sunflower.fxml")); fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()); Parent root = null; try { root = fxmlLoader.load(); } catch (IOException e) { e.printStackTrace(); } Scene scene = new Scene(root, 640, 480); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); }}
SunflowerController.javapublic class SunflowerController { @FXML private Button button;
@FXML private Label label;
@FXML private ResourceBundle resources; public void initialize() { button.setOnAction(e -> { label.setText(resources.getString("label.text") + " " + System.getProperty("javafx.version")); label.setVisible(! label.isVisible()); }); }
}啟動效果java的 module化的應用, 不在建議使用idea直接點main函數啟動,最好是用gradle右側面板的run任務來啟動,如下圖所示:
開源的代碼
運行效果如下所示:
其實我一直不想在文章中寫很多的代碼,平常小刀在朋友圈發的那個工具,已經開源, 而且也有小夥伴下載下來成功運行了, 所以,,,不要糾結上面的代碼,,可以直接下載這個就行 https://github.com/xiaodaojava/sunflower-free
JAVAFX 系列文章JAVAFX(一) java Module 模塊化簡介