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包。