javaFX普通应用方式

javafx下载

​javafx官网地址​​ 下载sdk(对模块化不熟悉推荐使用)或者jmod

配置环境变量(windows)

javaFX入门_java

idea创建javafx项目及导包

javaFX入门_maven_02

idea配置

javaFX入门_ide_03

--module-path J:\javafx-sdk-11.0.2\lib --add-modules javafx.controls,javafx.fxml,javafx.graphics

运行结果

javaFX入门_ide_04


javaFX maven及模块化方式

模块

# 分析 easytext-analysis
easytext-analysis

# 命令
easytext-analysis

# gui easytext-gui
easytext-gui

parent pom

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.maven.plugin.version}</version>

</plugin>
</plugins>
</pluginManagement>
</build>

easytext-analysis

主要java文件

FleschKincaid.java

package com.easytext.analysis;

import java.util.List;

/**
* @author caifan
* @created 2021/11/22
* @description:
*/
public class FleschKincaid {
public double analyze(List<List<String>> sentences) {
float totalsentences = sentences.size();
float totalwords = sentences.stream().mapToInt(sentence -> sentence.size()).sum();
float totalsyllables = sentences.stream()
.flatMapToInt(sentence ->
sentence.stream().mapToInt(word -> countSyllables(word)))
.sum();
return 206.835 - 1.015 * (totalwords / totalsentences) - 84.6 * (totalsyllables / totalwords);
}

private int countSyllables(String word) {
int syllables = 0;
boolean prevNonVowel = false;
for(int i = 0; i < word.length(); i++) {
boolean isVowel = isVowel(word.toLowerCase().charAt(i));
if(prevNonVowel && isVowel && i != word.length() - 1) {
syllables++;
}
prevNonVowel = !isVowel;
}
syllables = syllables == 0 ? 1 : syllables;
return syllables;
}

private boolean isVowel(char letter) {
return letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u';
}
}

module-info.java

module easytext.analysis {
# 导出的包才能被外部模块使用
exports com.easytext.analysis;
}

easytext-gui

pom.xml核心

<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
</dependency>

<dependency>
<groupId>com.easytext</groupId>
<artifactId>easytext-analysis</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.5</version>

</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

java类

package com.easytext.gui;

import com.easytext.analysis.FleschKincaid;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

/**
* @author caifan
* @created 2021/11/28
* @description:
*/
public class Main extends Application {
private static ComboBox<String> algorithm;
private static TextArea input;
private static Text output;

public static void main(String[] args) {
Application.launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("EasyText");
Button btn = new Button();
btn.setText("Calculate");
btn.setOnAction(event ->
output.setText(analyze(input.getText(), (String) algorithm.getValue()))
);

VBox vbox = new VBox();
vbox.setPadding(new Insets(3));
vbox.setSpacing(3);
Text title = new Text("Choose an algorithm:");
algorithm = new ComboBox<>();
algorithm.getItems().add("Flesch-Kincaid");

vbox.getChildren().add(title);
vbox.getChildren().add(algorithm);
vbox.getChildren().add(btn);

input = new TextArea();
output = new Text();
BorderPane pane = new BorderPane();
pane.setRight(vbox);
pane.setCenter(input);
pane.setBottom(output);
primaryStage.setScene(new Scene(pane, 300, 250));
primaryStage.show();
}

private String analyze(String input, String algorithm) {
List<List<String>> sentences = toSentences(input);

return "Flesch-Kincaid: " + new FleschKincaid().analyze(sentences);
}


public static List<List<String>> toSentences(String text) {
String removedBreaks = text.replaceAll("\\r?\\n", " ");
ArrayList<List<String>> sentences = new ArrayList<>();
for(String rawSentence: removedBreaks.split("[\\.\\?\\!]")) {
List<String> words = toWords(rawSentence);
if(words.size() > 0) {
sentences.add(words);
}
}

return sentences;
}

public static List<String> toWords(String sentence) {
String[] rawWords = sentence.split("\\s+");
List<String> words = new ArrayList<>();
for(String rawWord: rawWords) {
String word = rawWord.replaceAll("\\W", "");
if(word.length() > 0) {
words.add(word);
}
}

return words;
}
}

module-info.java

module easytext.gui {
requires javafx.controls;
requires javafx.graphics;
requires easytext.analysis;

exports com.easytext.gui;
}

​参考​