如何在JavaFX中向文本添加自定义字体?
现场演示
->
import java.io.FileNotFoundException; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.DropShadow; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.Text; public class CustomFontExample extends Application { public void start(Stage stage) throws FileNotFoundException { //创建一个文本对象 Text text = new Text(30.0, 75.0, "ట్యూ టోరియల్స్ పాయింట్ కి స్వా గతిం"); //从本地文件系统加载字体 Font font = Font.loadFont("file:resources/fonts/TenaliRamakrishna-Regular.ttf", 45); //设置字体 text.setFont(font); //设置文字颜色 text.setFill(Color.BROWN); text.setStroke(Color.BLUEVIOLET); text.setStrokeWidth(0.5); //创建内部阴影效果 //创建阴影效果 DropShadow shadow = new DropShadow(); shadow.setOffsetY(5.0); //将效果设置为文本 text.setEffect(shadow); //设置舞台 Group root = new Group(text); Scene scene = new Scene(root, 595, 150, Color.BEIGE); stage.setTitle("Custom Font"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果