‘Font’ class is used to define fonts. There are multiple ways to get a font instance.
a. Get system default font
b. Using utility methods.
Get System default font
public static synchronized Font getDefault()
Example
Font font1 = Font.getDefault();
Using utility methods.
Font font(String family, FontWeight weight, FontPosture posture, double size)
public static Font font(double size)
public static Font font(String family)
public static Font font(String family, double size)
public static Font font(String family, FontPosture posture, double size)
public static Font font(String family, FontWeight weight, double size)
public static Font font(String family, FontWeight weight, FontPosture posture, double size)
Example
Font font2 = Font.font(10);
Font font3 = Font.font("Verdana");
Font font4 = Font.font("Verdana", 10);
Font font5 = Font.font("Verdana", FontPosture.ITALIC, 10);
Font font6 = Font.font("Verdana", FontWeight.BOLD, 10);
Font font7 = Font.font("Verdana", FontWeight.BOLD, FontPosture.ITALIC, 10);
Find the below working application.
fontDemo.fxml
<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.FontController">
<Text fx:id = "text" text="Learning JavaFX" fill="lightgreen"
style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana;"
stroke="darkblue" strokeWidth="2"
underline="true"/>
</StackPane>
FontController.java
package com.sample.app.controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
public class FontController implements Initializable {
@FXML
private Text text;
@Override
public void initialize(URL location, ResourceBundle resources) {
// Write custom logic here
}
}
FontDemo.java
package com.sample.app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class FontDemo extends Application {
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent) FXMLLoader.load(FontDemo.class.getResource("/fontDemo.fxml"));
Scene scene = new Scene(root, 600, 300, Color.WHITE);
primaryStage.setTitle("StackPane Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Output
No comments:
Post a Comment