使用JShell導入第三方類庫OpenJFX(JavaFX)繪製簡單圖形

import javafx.application.Application
import javafx.stage.Stage
import javafx.fxml.FXMLLoader
import javafx.scene.Scene
import javafx.scene.layout.AnchorPane
import javafx.scene.paint.Color
import javafx.scene.shape.Line
import java.io.File

class MainWindow extends Scene {
    public MainWindow() throws Exception {
        super(FXMLLoader.load(new File("D:\\CAH\\Creat\\2019\\Learn\\Applicarion\\JShell\\main.fxml").toURL()));
        AnchorPane anchorPane = (AnchorPane) lookup("#anchorPane");
        for(int i = 0; i < 11; i++){
            Line lineH = new Line();
            lineH.setStartX(50);
            lineH.setStartY(i * 50 + 50);
            lineH.setEndX(600 - 50);
            lineH.setEndY(i * 50 + 50);
            lineH.setStroke(Color.RED);
            lineH.setStrokeWidth(5);
            Line lineV = new Line();
            lineV.setStartX(i * 50 + 50);
            lineV.setStartY(50);
            lineV.setEndX(i * 50 + 50);
            lineV.setEndY(600 - 50);
            lineV.setStroke(Color.RED);
            lineV.setStrokeWidth(5);
            anchorPane.getChildren().add(lineH);
            anchorPane.getChildren().add(lineV);
        }
    }
}

class MainStage extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        // 50 per
        primaryStage.setMinWidth(600);
        primaryStage.setMinHeight(600);
        // 設置標題
        primaryStage.setTitle("畫格子");
        // 加載顯示面板到Stage容器內
        primaryStage.setScene(new MainWindow());
        // 設置窗口大小禁止改變
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}

Application.launch(MainStage.class)

上面這一段是JShell腳本

下面是main.fxml文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            prefHeight="400.0" prefWidth="600.0" id="anchorPane">

</AnchorPane>

下面是運行結果:

使用命令 jshell --class-path * .\darwLine.java 導入OpenJFX並執行.\darwLine.java中的腳本。

--class-path後面是第三方jar包classpath的路徑,*表示當前目錄下全部jar包。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章