当前位置: 首页 > news >正文

第三百七十二节 JavaFX教程 - JavaFX HTMLEditor

JavaFX教程 - JavaFX HTMLEditor

HTMLEditor控件是一个富文本编辑器,具有以下功能。

  • 粗体
  • 斜体
  • 下划线
  • 删除线
  • 字体系列
  • 字体大小
  • 前景色
  • 背景颜色
  • 缩进
  • 项目符号列表
  • 编号列表
  • 对齐
  • 水平线
  • 复制文本片段
  • 粘贴文本片段

HTMLEditor类返回HTML字符串中的编辑内容。

创建HTML编辑器

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Main extends Application {
 
    @Override
    public void start(Stage stage) {
        HTMLEditor htmlEditor = new HTMLEditor();
        htmlEditor.setPrefHeight(245);
        Scene scene = new Scene(htmlEditor);       
        stage.setScene(scene);
        stage.show();
    } 
    public static void main(String[] args) {
        launch(args);
    }
}

上面的代码生成以下结果。

null

HTML内容

要将内容设置为HTMLEditor类,请使用setHtmlText方法。

htmlEditor.setHtmlText(INITIAL_TEXT);
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Main extends Application {
 
    @Override
    public void start(Stage stage) {
        HTMLEditor htmlEditor = new HTMLEditor();
        htmlEditor.setPrefHeight(245);
        String INITIAL_TEXT = "Lorem ipsum dolor sit "
            + "amet, consectetur adipiscing elit. Nam tortor felis, pulvinar "
            + "aliquam sagittis gravida eu dolor. Etiam sit amet ipsum "
            + "sem.";
        htmlEditor.setHtmlText(INITIAL_TEXT);
        Scene scene = new Scene(htmlEditor);       
        stage.setScene(scene);
        stage.show();
    } 
    public static void main(String[] args) {
        launch(args);
    }
}

上面的代码生成以下结果。

null

格式

我们可以使用此字符串中的HTML标记为最初渲染的内容应用特定的格式。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Main extends Application {
 
    @Override
    public void start(Stage stage) {
        HTMLEditor htmlEditor = new HTMLEditor();
        htmlEditor.setPrefHeight(245);
        String INITIAL_TEXT = "Lorem ipsum dolor sit "
            + "amet, consectetur adipiscing elit. <i>Nam tortor felis</i>, pulvinar "
            + "<UL><li>a</li><li>a</li><li>a</li></UL>"
            + "aliquam sagittis gravida <b>eu dolor</b>. Etiam sit amet ipsum "
            + "sem.";
        htmlEditor.setHtmlText(INITIAL_TEXT);
        Scene scene = new Scene(htmlEditor);       
        stage.setScene(scene);
        stage.show();
    } 
    public static void main(String[] args) {
        launch(args);
    }
}

上面的代码生成以下结果。

null

获取HTML内容

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Main extends Application {
 
    @Override
    public void start(Stage stage) {
        HTMLEditor htmlEditor = new HTMLEditor();
        htmlEditor.setPrefHeight(245);
        String INITIAL_TEXT = "Lorem ipsum dolor sit "
            + "amet, consectetur adipiscing elit. <i>Nam tortor felis</i>, pulvinar "
            + "<UL><li>a</li><li>a</li><li>a</li></UL>"
            + "aliquam sagittis gravida <b>eu dolor</b>. Etiam sit amet ipsum "
            + "sem.";
        htmlEditor.setHtmlText(INITIAL_TEXT);
        
        Button showHTMLButton = new Button("Produce HTML Code");
        
        showHTMLButton.setOnAction((ActionEvent arg0) -> {
          System.out.println(htmlEditor.getHtmlText());
        });
        
        VBox vbox = new VBox();
        vbox.getChildren().addAll(htmlEditor,showHTMLButton);
        Scene scene = new Scene(vbox);       
        stage.setScene(scene);
        stage.show();
    } 
    public static void main(String[] args) {
        launch(args);
    }
}

上面的代码生成以下结果。

null

相关文章:

  • spring事件
  • 【推荐项目】023-游泳俱乐部管理系统
  • 优博讯,蓝禾,三七互娱,顺丰,oppo,游卡,汤臣倍健,康冠科技,作业帮,高途教育25届春招内推
  • 基于 Python 深度学习的电影评论情感分析可视化系统(2.0 全新升级)
  • 算法日常刷题笔记(3)
  • 【Java项目】基于SpringBoot的藏区特产销售平台
  • 目标检测算法——YOLOV11——算法详解
  • 【考试大纲】中级网络工程师考试大纲(最新版与旧版对比)
  • 斐波那契数列的可视化
  • Linux tar命令
  • DOM HTML:深入理解与高效运用
  • 基于Spring Boot + Vue的常规应急物资管理系统设计与实现
  • C++ 循环结构练习题目集
  • 计算机面试项目经历描述技巧
  • 从浏览器地址栏输入URL到页面加载发生了什么?
  • apload-lab打靶场
  • 视觉图像坐标转换
  • 影刀RPA开发拓展--SQL常用语句全攻略
  • Linux内核配置与构建原理
  • 大摩闭门会:250228 学习总结报告
  • 做刷网站流量犯法吗/汕头seo公司
  • 做网站买狗怎么样/搜索引擎谷歌
  • 网上做网站/如何做网站优化seo
  • 怎样免费做彩票网站/网络营销软件推广
  • 四川绵阳网站建设/关键词优化软件排行
  • 郑州公司做网站/百度电脑版网址