JavaFX | HTMLEditor类

HTMLEditor类是JavaFX的一部分。HTMLEditor允许用户编辑现有的HTML文本,并对文本应用样式。底层数据模型是HTML,但用户看不见。

null

类的构造函数:

  • HTMLEditor() :创建HTMLEditor的新对象。

常用方法:

方法 解释
getHtmlText() 返回编辑器的HTML内容。
打印(PrinterJob j) 使用给定的打印机作业打印编辑器的内容。
setHtmlText(字符串h) 设置编辑器的HTML文本。

下面的程序演示了HTMLEditor类的使用:

  1. 创建HTMLEditor并添加到舞台的Java程序: 在这个程序中,我们将创建一个名为 htmleditor .我们还将创建一个名为 替利班 ,然后使用 getChildren()。添加() 作用我们将创建一个场景并添加tilepane。我们将使用 setScene() 使用 show() 函数来显示最终结果。

    // Java program to create a html editor
    // and add to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.web.HTMLEditor;
    public class Editor_1 extends Application {
    // launch the application
    public void start(Stage stage)
    {
    // set title for the stage
    stage.setTitle( "Creating HTMLEditor" );
    // create a tile pane
    TilePane tilepane = new TilePane();
    // HTML editor
    HTMLEditor htmleditor = new HTMLEditor();
    // add html editor
    tilepane.getChildren().add(htmleditor);
    // create a scene
    Scene scene = new Scene(tilepane, 600 , 500 );
    // set the scene
    stage.setScene(scene);
    stage.show();
    }
    // Main Method
    public static void main(String args[])
    {
    // launch the application
    launch(args);
    }
    }

    
    

    输出:

  2. Java程序创建HTMLEditor,并为其设置初始HTML文本,然后添加到后台: 在这个程序中,我们将创建一个名为 htmleditor .我们将使用 setHtmlText() 作用我们还将创建一个名为 替利班 ,我们将使用 getChildren()。添加() 作用我们将创建一个场景并添加tilepane。我们将使用 setScene() 使用 show() 函数来显示最终结果。

    // Java program to create a html editor
    // and set initial HTML text to it and
    // add to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.web.HTMLEditor;
    public class Editor_2 extends Application {
    // launch the application
    public void start(Stage stage)
    {
    // set title for the stage
    stage.setTitle( "creating HTMLEditor" );
    // HTML text
    String text = "<html><body><h1>Geeks</h1></body></html>" ;
    // create a tile pane
    TilePane tilepane = new TilePane();
    // HTML editor
    HTMLEditor htmleditor = new HTMLEditor();
    // set html text
    htmleditor.setHtmlText(text);
    // add html editor
    tilepane.getChildren().add(htmleditor);
    // create a scene
    Scene scene = new Scene(tilepane, 600 , 500 );
    // set the scene
    stage.setScene(scene);
    stage.show();
    }
    // Main Method
    public static void main(String args[])
    {
    // launch the application
    launch(args);
    }
    }

    
    

    输出: 图片[1]-JavaFX | HTMLEditor类-yiteyi-C++库

注: 上述程序可能无法在联机IDE中运行。请使用脱机编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/web/HTMLEditor.html

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享