首页 > 解决方案 > 有没有其他方法可以在 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);
  }
}

标签: javafx

解决方案


推荐阅读