首页 > 文章列表 > 如何在JavaFX中添加删除线和下划线文本?

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

javafx 添加删除线 下划线文本
307 2023-08-19

在JavaFX中,文本节点由Javafx.scene.text.Text类表示。要在JavaFx窗口中插入/显示文本,您需要执行以下操作:

  • 实例化Text类。

  • 使用setter方法或将其作为构造函数的参数绕过它们,设置基本属性,如位置和文本字符串。

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

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

javafx.scene.text.Text类的underline属性确定每行文本下方是否应该有一条直线。您可以使用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);
   }
}

输出

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