如何在 JavaFX 中添加删除线和下划线?


在 JavaFX 中,文本节点由 **Javafx.scene.text.Text** 类表示。要在 JavaFx 窗口中插入/显示文本,您需要 -

  • 实例化 Text 类。

  • 使用 setter 方法或将它们作为参数传递给构造函数来设置基本属性(如位置和文本字符串)。

  • 将创建的节点添加到 Group 对象。

**javafx.scene.text.Text** 类的 **删除线** 属性确定文本的每一行是否应该有一条直线穿过它的中间。您可以使用 **setStrikeThrough()** 方法为此属性设置值。它接受布尔值。您可以通过将 true 作为参数传递给此方法来删除文本(节点)。

**javafx.scene.text.Text** 类的 **下划线** 属性确定文本的每一行是否应该在其下方有一条直线。您可以使用 **setUnderline()** 方法为此属性设置值。它接受布尔值。您可以通过将 true 作为参数传递给此方法来在下划线下方添加一行。

示例

import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class Underline_StrikeThrough extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a text object
      String str = "Welcome to Tutorialspoint";
      Text text = new Text(30.0, 80.0, str);
      //Setting the font
      Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 65);
      text.setFont(font);
      //Setting the color of the text
      text.setFill(Color.DARKCYAN);
      //Setting the width and color of the stroke
      text.setStrokeWidth(2);
      text.setStroke(Color.DARKSLATEGRAY);
      //Underlining the text
      text.setUnderline(true);
      //Striking through the text
      text.setStrikethrough(true);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Underline And Strike-through");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

输出

更新于: 2020年4月14日

3K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.