JavaFx:采用SceneBuilder方式实现格式化java源文件(引入fxml文件完成样式布局)

本文主要是介绍JavaFx:采用SceneBuilder方式实现格式化java源文件(引入fxml文件完成样式布局),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

开发环境:jdk8openjfx11.0.2eclipse

1. 声明

当前内容主要为本人学习和了解使用SceneBuilder方式实现样式布局,并实现之前的格式化java源文件并显示(本人直接以lib方式导入javafx依赖)

基本步骤:

  1. 使用SceneBuilder画图
  2. 使用Controller控制调用事件
  3. 加载fxml文件完成渲染
  4. 读取java源文件并使用javaparser进行格式化输出在TextArea
  5. 弹出消息窗口提示错误事件(alert)

基本pom

<dependency><groupId>com.github.javaparser</groupId><artifactId>javaparser-symbol-solver-core</artifactId><version>3.22.1</version>
</dependency>

关于javaparser的使用参考之前的博文

2. 下载和使用SceneBuilder

1.直接在openjfx官方下载(这里选择jdk1.8的版本):openjfx-java1.8

在这里插入图片描述
这里直接下载jar的可执行包,使用java -jar启动

2. 开始画图(直接开始画图操作)
在这里插入图片描述
3.为所有的组件添加fx:id属性,方便与controller中的属性进行绑定操作

在这里插入图片描述
在这里插入图片描述

4.最后生成一个fxml文件,考入启动main方法相同目录即可
在这里插入图片描述

javaFileFormatTolls.fxml其内容如下:

<?xml version="1.0" encoding="UTF-8"?><?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?><VBox prefHeight="436.0" prefWidth="657.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.hy.java.gui.javafx.javafile.controller.JavaFileFormatAppController" ><children><GridPane alignment="CENTER_RIGHT" prefHeight="28.0" prefWidth="640.0"><columnConstraints><ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /><ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /><ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /></columnConstraints><rowConstraints><RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /></rowConstraints><children><TextField fx:id="sourceJavaFilePath" GridPane.columnIndex="1" /><Label alignment="CENTER_RIGHT" prefHeight="20.0" prefWidth="216.0" text="源文件" textAlignment="RIGHT"><GridPane.margin><Insets /></GridPane.margin></Label><Button mnemonicParsing="false" onAction="#handlerFormatButtonClick" text="格式化" GridPane.columnIndex="2" /></children><opaqueInsets><Insets /></opaqueInsets><VBox.margin><Insets bottom="10.0" top="10.0" /></VBox.margin></GridPane><AnchorPane><children><SplitPane dividerPositions="0.5" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"><items><AnchorPane prefHeight="469.0" prefWidth="324.0"><children><TextArea fx:id="leftTextArea" layoutY="7.0" prefHeight="278.0" prefWidth="315.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /></children></AnchorPane><AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="278.0" prefWidth="253.0"><children><TextArea fx:id="rightTextArea" editable="false" layoutX="7.0" prefHeight="278.0" prefWidth="315.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /></children></AnchorPane></items></SplitPane></children></AnchorPane></children>
</VBox>

3.开始编写代码

1.controller层:

package com.hy.java.gui.javafx.javafile.controller;import java.io.File;
import java.io.IOException;
import java.util.Optional;import com.hy.java.gui.javafx.javafile.FormatTools;import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.control.ButtonBar;public class JavaFileFormatAppController {@FXMLTextArea leftTextArea;@FXMLTextArea rightTextArea;@FXMLTextField sourceJavaFilePath;@FXMLpublic void handlerFormatButtonClick() {//System.out.println("点击当前的格式化按钮");//System.out.println("开始获取当前左边文本框中的内容:" + leftTextArea.getText());//System.out.println("开始获取当前右边文本框中的内容:" + rightTextArea.getText());//System.out.println("开始和获取输入框的内容:" + sourceJavaFilePath.getText());String text = sourceJavaFilePath.getText();if (isEmpty(text)) {// 提示输入文件showErrorAlert("错误", "请输入需要格式化的java源文件");return;} else {File file = new File(text);if (file.exists() && !file.isDirectory() && file.canRead()) {try {String leftTextAreaContext = this.leftTextArea.getText();if (isEmpty(leftTextAreaContext)) {String fileContext = FormatTools.getFileContext(file);leftTextArea.setText(fileContext);String formatJavaFile = FormatTools.formatJavaFile(fileContext);rightTextArea.setText(formatJavaFile);}} catch (IOException e) {//e.printStackTrace();showErrorAlert("错误",e.getMessage());}} else {// 不可操作showErrorAlert("错误", "当前文件不可读");}}}/*** * @author hy* @createTime 2021-07-25 10:02:01* @description 弹出一个消息框* @param title* @param msg**/private void showErrorAlert(String title, String msg) {Alert alert = new Alert(AlertType.ERROR, msg, ButtonType.YES, ButtonType.NO);alert.setTitle(title);alert.setHeaderText("");Optional<ButtonType> showAndWait = alert.showAndWait();if (showAndWait.get() == ButtonType.YES) {alert.close();}}private boolean isEmpty(String text) {if (text == null || "".equals(text.trim())) {return true;}return false;}}

其中以@FXML开始的就是绑定的对应fxml中的组件中的fx:controller范围内的fx:id,或者#方法名称

2.开始编写文件格式化工具(FormatTools.java)

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Optional;import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.PackageDeclaration;import de.hunsicker.jalopy.Jalopy;public abstract class FormatTools {public static String formatJavaFile(String fileContext) {JavaParser javaParser = new JavaParser();ParseResult<CompilationUnit> parse = javaParser.parse(fileContext);StringBuffer buffer = new StringBuffer();if (parse.isSuccessful()) {Optional<CompilationUnit> result = parse.getResult();CompilationUnit compilationUnit = result.get();List<Node> childNodes = compilationUnit.getChildNodes();for (Node node : childNodes) {if (node instanceof ImportDeclaration) {buffer.append(String.valueOf(node));} else if (node instanceof PackageDeclaration) {buffer.append(String.valueOf(node));} else {buffer.append(String.valueOf(node));buffer.append("\r\n");}}}return buffer.toString();}public static String getFileContext(File file) throws IOException {StringBuffer buffer = new StringBuffer();try (BufferedReader bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {String line = null;while ((line = bufferReader.readLine()) != null) {buffer.append(line);buffer.append("\r\n");}}return buffer.toString();}}

3.开始编写入口类

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;public class JavaFileFormatTest extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage stage) throws Exception {Parent root = FXMLLoader.load(getClass().getResource("javaFileFormatTools.fxml"));Scene scene = new Scene(root, 1200,800);stage.setTitle("Java File Formatter");stage.setScene(scene);stage.show();}
}

4. 测试

在这里插入图片描述
输入需要格式化的文件名称路径,并点击格式化
在这里插入图片描述
实现代码格式化成功,如果未填写直接格式化就会出现提示框

在这里插入图片描述
注意这个alert,实现成功!

这篇关于JavaFx:采用SceneBuilder方式实现格式化java源文件(引入fxml文件完成样式布局)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/488417

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u