ใน JavaFX โหนดข้อความแทนด้วยคลาส Javafx.scene.text.Text เพื่อแทรก/แสดงข้อความในหน้าต่าง JavaFX ต้อง:
- สร้างอินสแตนซ์ของคลาส Text
- ตั้งค่าคุณสมบัติพื้นฐาน เช่น ตำแหน่งและสตริงข้อความ ใช้ setter methods หรือส่งเป็นอาร์กิวเมนต์ให้คอนสตรัคเตอร์
- เพิ่มโหนดที่สร้างเข้าไปในออบเจ็กต์ Group
คุณสมบัติ strikethrough ของคลาส javafx.scene.text.Text กำหนดว่าแต่ละบรรทัดของข้อความควรมีเสตรงผ่านกลางหรือไม่ สามารถตั้งค่าได้ด้วยเมธอด setStrikeThrough() รับค่า boolean ส่ง true เพื่อทำขีดทับ
คุณสมบัติ underline กำหนดว่าแต่ละบรรทัดควรมีเสตรงด้านล่างหรือไม่ ตั้งค่าด้วย setUnderline() รับค่า boolean ส่ง 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 {
// สร้างออบเจ็กต์ Text
String str = "Welcome to Tutorialspoint";
Text text = new Text(30.0, 80.0, str);
// ตั้งค่าฟอนต์
Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 65);
text.setFont(font);
// ตั้งค่าสีข้อความ
text.setFill(Color.DARKCYAN);
// ตั้งค่าความกว้างและสี stroke
text.setStrokeWidth(2);
text.setStroke(Color.DARKSLATEGRAY);
// ขีดเส้นใต้
text.setUnderline(true);
// ขีดทับ
text.setStrikethrough(true);
// ตั้งค่า 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);
}
}
ผลลัพธ์
