javafx - 有没有其他方法可以在 JavaFX 中解析和呈现 HTML 文本,而不是使用 WebView?
问题描述
由于 JavaFX Label 不能像 Swing JLabel 那样解析和呈现 HTML 内容,因此使用 WebView 来解析和呈现简单的 HTML 文本的方法听起来像是使用战斧导弹杀死蚊子。任何人都有替代想法如何在 JavaFX 中解析和呈现简单的 HTML 文本,而不是使用 WebView?下面的代码片段显示了一个使用 WebView 的示例,该示例看起来过于繁琐和复杂。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
stage.setTitle("StackOverFlow Example");
stage.setWidth(816);
stage.setHeight(310);
Scene scene = new Scene(new Group());
VBox root = new VBox();
HBox option1 = buildLineOption("balls", "blue", 1, 2);
HBox option2 = buildLineOption("bikes", "red", 2, 3);
HBox option3 = buildLineOption("bananas", "magenta", 3, 4);
HBox option4 = buildLineOption("squares", "orange", 1, 2);
HBox option5 = buildLineOption("lions", "yellow", 2, 4);
HBox option6 = buildLineOption("numbers", "green", 3, 5);
HBox option7 = buildLineOption("letters", "white", 1, 4);
HBox option8 = buildLineOption("days", "black", 2, 5);
HBox option9 = buildLineOption("keys", "purple", 3, 4);
root.getChildren().addAll(option1, option2, option3, option4, option5, option6, option7, option8, option9);
scene.setRoot(root);
stage.setScene(scene);
stage.show();
}
private HBox buildLineOption(String controlType, String color, int minInit, int maxInit) {
CheckBox checkBox = new CheckBox();
final WebView browser = new WebView();
browser.setMaxWidth(450);
browser.setMinWidth(450);
browser.setPrefWidth(450);
final WebEngine webEngine = browser.getEngine();
SpinnerValueFactory<Integer> minValueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 5, minInit);
SpinnerValueFactory<Integer> maxValueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 5, maxInit);
final Spinner<Integer> minSpinner = new Spinner<Integer>();
minSpinner.setValueFactory(minValueFactory);
final Spinner<Integer> maxSpinner = new Spinner<Integer>();
maxSpinner.setValueFactory(maxValueFactory);
HBox hBox = new HBox(5);
hBox.setAlignment(Pos.CENTER);
hBox.setPadding(new Insets(0, 5, 0, 5));
hBox.setStyle("-fx-background-color: lightgrey");
hBox.setMaxSize(800, 30);
hBox.setMinSize(800, 30);
hBox.setPrefSize(800, 30);
hBox.getChildren().addAll(checkBox, browser, minSpinner, maxSpinner);
webEngine.loadContent(String.format("<html><style>body {background-color: lightgrey}</style>"
+ "Keep the quantity of <font color=%s><b>%s </b></font> <b>%s</b> between limits:</html>", color, color, controlType));
return hBox;
}
public static void main(String[] args) {
launch(args);
}
}
解决方案
推荐阅读
- python - 除了 time.sleep() 在 python 中创建延迟的其他方法
- c - C:结构声明中是否允许成员之间的操作?
- python - 组合列表中的特定元素
- angular - 根组件中的路由模块和带有 scss 扩展名的样式文件
- reactjs - 错误:要使用需要 `addCSS` 的插件(例如关键帧、媒体查询),请将您的应用程序包装在 StyleRoot 组件中
- c++ - C++17 中的 Lambda 表达式:用于类型转换的尾随返回类型与 static_cast
- javascript - 有没有调试失败的承诺的好方法?
- c++ - 用户定义数据类型的大小和继承
- mongodb - 如何使belongsTo关系在loopback4中工作
- javascript - 为什么扩展运算符将数据嵌套在另一个对象中?