Open In App

JavaFX | FontPosture Class

Last Updated : 18 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

FontPosture class is a part of JavaFX. FontPosture class specifies whether a font is REGULAR or ITALIC. FontPosture class inherits Enum class.

Commonly Used Methods:

Method Explanation
findByName(String n) Returns FontPosture by its name.
valueOf(String n) Returns a FontPosture with the specified name.
values() returns an array with all the elements of the FontPosture type.

Java Program to create a font object and set a specified posture and apply it to a text: In this program we will create two Text object and set one’s font posture to REGULAR and the other one to ITALIC. Then set the text to the TextFlow and add this textflow to the VBox and add the vbox to the scene, and add the scene to the stage. Also, set the line spacing and text alignment of the textflow and the spacing of the vbox. Call the show() function to display the final results.




// Java Program to create a font object and set
// a specified posture and  apply it to a text
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
  
public class FontPosture_1 extends Application {
  
    // launch the application
    public void start(Stage stage)
    {
  
        try {
  
            // set title for the stage
            stage.setTitle("FontPosture");
  
            // create TextFlow
            TextFlow text_flow = new TextFlow();
  
            // create text
            Text text_1 = new Text("GeeksforGeeks\n");
  
            // set the text color
            text_1.setFill(Color.GREEN);
  
            // create a font
            Font font = Font.font("Verdana", FontWeight.EXTRA_BOLD,
                                          FontPosture.REGULAR, 25);
  
            // set font of the text
            text_1.setFont(font);
  
            // create text
            Text text_2 = new Text("A Computer Science portal for geeks\n");
  
            // set the text color
            text_2.setFill(Color.GREEN);
  
            // create a font
            Font font1 = Font.font("Verdana", FontWeight.EXTRA_BOLD,
                                            FontPosture.ITALIC, 12);
  
            // set font of the text
            text_2.setFont(font1);
  
            // set text
            text_flow.getChildren().add(text_1);
            text_flow.getChildren().add(text_2);
  
            // set line spacing
            text_flow.setLineSpacing(20.0f);
  
            // set text alignment
            text_flow.setTextAlignment(TextAlignment.CENTER);
  
            // create VBox
            VBox vbox = new VBox(text_flow);
  
            // set alignment of vbox
            vbox.setAlignment(Pos.CENTER);
  
            // create a scene
            Scene scene = new Scene(vbox, 400, 300);
  
            // set the scene
            stage.setScene(scene);
  
            stage.show();
        }
  
        catch (Exception e) {
  
            System.out.println(e.getMessage());
        }
    }
  
    // Main Method
    public static void main(String args[])
    {
  
        // launch the application
        launch(args);
    }
}


Output:

Note: The above programs might not run in an online IDE please use an offline compiler.

Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/FontPosture.html



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads