如何在 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.