在textarea中允许下标和上标文本

时间:2017-01-24 06:04:55

标签: javafx

我想在textarea中允许上标和下标文本。在搜索了一下之后,我偶然发现了用于将字体描述为上标,下标或常规的javafx.scene.text.FontPosition https://docs.oracle.com/cd/E17802_01/javafx/javafx/1.1/docs/api/javafx.scene.text/javafx.scene.text.FontPosition.html。但是我无法弄清楚如何在打字时将当前字体流从常规字体流改为下标/上标,反之亦然。

1 个答案:

答案 0 :(得分:0)

您可以使用" WebView" richtext字段的节点。示例代码如下。希望它有用。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebviewSample extends Application {
   @Override
   public void start(final Stage stage) {
   stage.setWidth(400);
   stage.setHeight(500);
   Scene scene = new Scene(new Group());


   final WebView browser = new WebView();
   final WebEngine webEngine = browser.getEngine();

   ScrollPane scrollPane = new ScrollPane();
   scrollPane.setContent(browser);

   String htmlText = "<b> This is bold text </b>"
                    + "<br/>" 
                    + "<h1> This is h1 text </h1>"
                    + "<br/>"
                    + "<h2 style=\"background-color:red\">Background-color set by using red</h2>";

webEngine.loadContent(htmlText);

scene.setRoot(scrollPane);

stage.setScene(scene);
stage.show();
}

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

输出就像:

enter image description here

相关问题