【问题标题】:How to display HTML in JavaFX Application如何在 JavaFX 应用程序中显示 HTML
【发布时间】:2015-08-30 00:05:44
【问题描述】:

我正在开发一个 FontViewer 应用程序,它可以根据所选字体样式更改文本的字体。

这是我的应用程序的控制器类

public class FXMLDocumentController implements Initializable {

    @FXML
    private ListView fontListView;

    @FXML
    private TextArea fontTextArea;

    int[] fontSizes = {34, 28, 24, 20, 18, 16, 14, 12, 11, 10, 9, 8, 7, 6};

    String fontText = "";

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        ObservableList<String> fontsList = FXCollections.observableArrayList(Font.getFontNames());
        fontListView.setItems(fontsList);
    }

    @FXML
    public void handleMouseClickEvent(MouseEvent mouseEvent) {
        changeFont();
    }

    public void changeFont() {
        for (int i = 0; i < fontSizes.length; i++) {
            fontText += "<p style='font-family:" + fontListView.getSelectionModel().getSelectedItem() + ";font-size:" + fontSizes[i] + "'>" + "This is Sample Text</p>";
        }

        fontTextArea.setText(fontText);
    }
}

我的应用程序的屏幕截图:

当我使用TextArea 时,它只显示纯 HTML 代码,而不是将 HTML 转换为文本。我应该使用哪个控件来完成此操作?

P.S:我尝试了TextFlow,但它不起作用,因为我需要为所需的文本显示不同的样式和字体大小。

我也查看了WebView,但不明白它如何解决上述问题。

【问题讨论】:

    标签: java javafx javafx-8


    【解决方案1】:

    这是一个很晚的答案,但我想把它传递给那些需要将字体应用于各种 FX 控件的人(正如我需要做的那样):

    通过使用TextArea 的setFont 方法,您可以在不使用WebView 的情况下显示字体。这具有适用于大多数文本显示FX控件的优点,包括ButtonTextField等...,并且可以与下载的TTF或OTF文件一起使用。

    // examples create a 20 pt font
    // system font
    Font f = Font.font("SansSerif", Font.PLAIN, 20);
    // TTF/OTF added to your project as a resource:
    Font f = Font.loadFont(ClassLoader.getSystemClassLoader().getResourceAsStream("ResourceName"), 20);
    // TTF/OTF loaded from a file dynamically... read the font file into an input stream, and:
    Font f = Font.loadFont(inputStream, 20);
    // to use:
    fontTextArea.setFont(f);
    

    注意:某些脚本 TTF/OTF 字体可以在系统字体适合的区域上方或下方扩展,因此在使用下载的字体时进行适当的测试。

    【讨论】:

      【解决方案2】:

      使用网络视图:

      @FXML
      private WebView fontWebView ;
      
      // ...
      
      public void changeFont() {
          StringBuilder sb = new StringBuilder(fontText);
          for (int i = 0; i < fontSizes.length; i++) {
              sb.append("<p style='font-family:")
                .append(fontListView.getSelectionModel().getSelectedItem())
                .append(";font-size:")
                .append( fontSizes[i])
                .append("'>This is Sample Text</p>");
          }
          fontText = sb.toString();
          fontWebView.getEngine().loadContent(fontText);
      }
      

      【讨论】:

      • 感谢它的工作,但每次我单击左侧的字体时,文本都会在另一个下方堆叠。即字体 A 文本后跟字体 B 文本后跟字体 C 等;是否可以在每次迭代时重置 WebView 内容,以便只有选定的字体文本在 webview 上可见?
      • 我认为这就是你想要的;这与您在原始代码中编码的逻辑相同。只需更改逻辑以将 fontText 构建为您想要的方式。
      • 要获取不包含先前内容的新文本,只需初始化 StringBuilder 不带参数。 (对不起,如果这被认为是 necroposting。)
      猜你喜欢
      • 2010-10-23
      • 2011-05-15
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2011-08-12
      • 2011-05-18
      相关资源
      最近更新 更多