我已将项目的字体存储在*>root*目录中的*>font*目录中。
我尝试使用以下代码:
public class NewController implements Initializable {
@FXML
private Label titleLabel;
@Override
public void initialize(URL url, ResourceBundle rb) {
titleLabel.setFont(Font.loadFont("fonts/Archivo-Regular.ttf", 30));
}
}
Run Code Online (Sandbox Code Playgroud)
但是它不能设置字体。
您可以将字体作为样式表添加到场景中
scene.getStylesheets().add(getClass().getResource("/font.css").toExternalForm());
Run Code Online (Sandbox Code Playgroud)
CSS文件
@font-face {
font-family: 'Archivo';
src: url('font/Archivo-Regular.ttf');
}
Run Code Online (Sandbox Code Playgroud)
之后,您可以轻松地将字体添加到CSS中的元素
.label {
-fx-font-family: 'Archivo';
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以在类中加载字体。但是在使用样式之前先加载字体。
类
Font.loadFont(NewController.class.getResource("Archivo-Regular.ttf").toExternalForm(), 30);
Run Code Online (Sandbox Code Playgroud)
@FXML
这是该站点的一些示例。
@Override
public void initialize(URL url, ResourceBundle rb) {
text.setText("My Text!");
text.setFill(Color.RED);
text.setFont(Font.font("Arial", FontWeight.BOLD, FontPosture.ITALIC, 24));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27 次 |
| 最近记录: |