文章目录
对javafx了解,java1.8加入jdk,java11移除jdk,成为单独的开源项目,大多数关于javafx的框架,对jdk版本要求比较高,都在11左右
JavaFX官网
https://openjfx.cn/
https://github.com/openjfxcn/jfx
成品工具
https://gitee.com/xwintop/xJavaFxTool
样式组件
https://gitee.com/mirrors/JFoenix
相关不错的文章
https://blog.youkuaiyun.com/YCJ_xiyang/article/details/103462592
https://gitee.com/xiyang_ycj/springboot-javafx/tree/master/javafx-springboot-3
界面拖拽布局
Scene Builder
实践链接
https://blog.youkuaiyun.com/cnds123321/article/details/104507487/
Scene Builder
- 写javafx离不开拖拽 Scene Builder是可以嵌套在idea中使用的
- 优点 写成fxml可以刚容易看到组件布局
- 缺点 只能支持原生打开,如果用了继承第三方的子类样式声明在fxml中,直接回报错
SpringBoot需要的一些jar包(Maven)
<!-- JavaFX-->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>12</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>12</version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>12</version>
<classifier>mac</classifier>
</dependency>
<!-- 组件库-->
<dependency>
<groupId>com.jfoenix</groupId>
<artifactId>jfoenix</artifactId>
<version>8.0.10</version>
</dependency>
<!-- JavaFX-->
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.1</version>
<configuration>
<mainClass>HelloFX</mainClass>
</configuration>
</plugin>
</plugins>
需要常用的监听器与事件
//鼠标点击按压
node.setOnMousePressed(event->{
//do something
MouseButton button = event.getButton();
//单击操作
if (button == MouseButton.PRIMARY) {
}
//右键点击
if (button == MouseButton.SECONDARY) {
}
//双击操作
if (event.getClickCount() == 2) {
}
});
//鼠标点击释放
node.setOnMouseReleased(event->{
//do something
});
//鼠标点击(按压-释放)
node.setOnMouseClicked(event->{
//do something
});
节点监听点击事件
//鼠标按压
node.addEventFilter(MouseDragEvent.MOUSE_PRESSED,event->{
//do something
});
//鼠标拖拽释放
node.addEventFilter(MouseDragEvent.MOUSE_RELEASED,event->{
//do something
});
//鼠标点击(按压-释放)
node.addEventFilter(MouseDragEvent.MOUSE_CLICKED, event -> {
//do something
});
坑之路,君之伤(因为是一个没写过JavaFX的小白,必然需要的数之不尽的坑)
注意点一
treeRoot.addEventFilter(MouseDragEvent.MOUSE_CLICKED, event -> {
//点击获取name
Node node = event.getPickResult().getIntersectedNode();
if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null)) {
String name = (String) ((TreeItem) treeRoot.getSelectionModel().getSelectedItem()).getValue();
//name对比拿出对应的对象
finalConfigs.forEach(o -> {
if (Strings.isNotEmpty(o.getTitle()) && o.getTitle().equals(name)) {
interfaceConfigShow(o);
try {
interfaceConfigTag = g_config_tagService.queryG_Config_TagId(interfaceConfig.getOID());
} catch (Exception e) {
AppUtil.dialogError(anchorPaneRoot, e.getMessage(), e);
}
interfaceConfigTagShow();
}
});
}
});
这是项目的的一段代码
treeRoot 是
@FXML
protected TreeView<String> treeRoot;
view中的TreeView根目录,由于要点击TreeItem实现一些动能
Treeltem绑定监听器一直不生效,浪费了较多时间,网上差多说是给父节点绑定传递给子节点
然后又出现了,父节点绑定后,子节点有监听到却拿不到对应的值
treeRoot.addEventFilter(MouseDragEvent.MOUSE_CLICKED, event -> {
//点击获取name
Node node = event.getPickResult().getIntersectedNode();
if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null)) {
String name = (String) ((TreeItem) treeRoot.getSelectionModel().getSelectedItem()).getValue();
注意点二
还有遇到布局问题,比较伤人,作为一个后端,页面拉伸的问题,最后用的网格布局,10*10的px一个网格,网格最少值是10,铺满
注意点三
javafx+springboot封住问题,这个布设点比较多,其他文章给予demo
注意点四
异常处理
- 可抛异常
- 不可抛异常
- 内部异常
最后封装统一的错误弹框处理,把本页面的布局对象传递,弹框处理
注意点五
TableView加不上数据问题
TableColumn TagColumn= new TableColumn("列名");
TagColumn.setPrefWidth(300);
TagColumn.setCellValueFactory(
new PropertyValueFactory<自己的对象, String>("对象的属相名"));
加上以后才能直接加list对象