类javafx.scene.text.Font生成字体。
可以通过构造器或者静态方法生成字体实例。一个字体实例包含name,weight,posture。其中posture包含两个参数:FontPosture.ITALIC和 FontPosture.REGULAR.下面是一个具体的程序演示JavaFx字体:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.*; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.text.*; import javafx.scene.control.*; import javafx.stage.Stage; public class FontDemo extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a pane to hold the circle Pane pane = new StackPane(); // Create a circle and set its properties Circle circle = new Circle(); circle.setRadius(50); circle.setStroke(Color.BLACK); circle.setFill(new Color(0.5,0.5,0.5,0.1)); pane.getChildren().add(circle);// Add circle to the pane // Create a label and set its properties Label label = new Label("JavaFX"); label.setFont(Font.font("Times New Roman",FontWeight.BOLD, FontPosture.ITALIC, 20)); pane.getChildren().add(label); // Create a scene and place it in the stage Scene scene = new Scene(pane); primaryStage.setTitle("FontDemo");// Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } }
说明:
1、本代码执行的结果是:
2、代码中创建了一个StackPane,并且分别把circle和label放在了里面,这两行代码可以合并成一行:
pane.getChildren().addAll(circle, label);
3、StackPane将node放在中心位置,并且不管窗口大小怎样改变,node始终处在中心位置。
4、Font对象是不可更改的,一但对象创建,它的属性不能更改,除非改变原始代码。
转载于:https://blog.51cto.com/fengbin8606/1423234