Quick Contact

    Hibernate

    JavaFX Font

    In JavaFX, font is a class that is used to denote fonts that renders the text available on screen. It is inherited from the object class.Font size is explained as mentioned in the points that are real-world measurementroughly 1/72 inch.Fonts are given to the text based on the user requirement and can be modified at any time. Some of the commonly used fonts include Verdana, Times new roman etc. In this article, we will see different aspects such as syntax, constructors, methods and examples of JavaFX font in the following sections.

    Syntax of JavaFX Font

    Below is the syntax of JavaFX font.

    Font f = Font.font(FONT, FONT TYPE, SIZE);

    • Here, FONT denotes different fonts like verdana, times new roman etc.
    • FONT TYPE is bold, italics etc.
    • SIZE denotes the font size.

    Constructors

    Following are the two constructors of JavaFX font.

    • Font(double size):

      A font will be constructed with “system” as the default face.

    • Font(Stringname, double size):

      A font will be constructed with mentioned size and face name.

    Methods

    Let us see different methods of JavaFX font.

    • equals(Objectobj):

      This method denotes whether any of the other objects available is same as this.

    • font(double s):

      An appropriate font will be searched based on the font name and size that is default.

    • font(Stringf):

      An appropriate font will be searched based on the mentioned font name and size that is the default.

    • font(Stringf, double s):

      An appropriate font will be searched based on the mentioned font name and size.

    • font(Stringf, FontPosture p, double s):

      An appropriate font will be searched based on the mentioned family name, posture and size.

    • font(Stringf, FontWeight w, double s):

      An appropriate font will be searched based on the mentioned family name, weight and size.

    • font(Stringf, FontWeight w, FontPosture p, double s):

      An appropriate font will be searched based on the mentioned family name, weight, posture and size.

    • getDefault():

      Default font will be retrieved that is from the “System” family, “Regular “style, and consistent size with the desktop environment of the user,to the limit that can be determined.

    • getFamilies():

      All the families of font will be retrieved on the system of the user, including the SDK fonts as well as application fonts.

    • get family():

      Family of font will be retrieved.

    • getFontNames():

      All the names of font will be retrieved on the system of the user, including the SDK fonts as well as application fonts.

    • getFontNames(Stringfamily):

      All the names of font will be retrieved on the system of the user that is of the mentioned family, including the SDK fonts as well as application fonts.

    • getName():

      Name of font will be retrieved.

    • getSize():

      Size of font will be retrieved.

    • getStyle():

      The font mentioned string explaining the style inside the font family.

    • hashCode():

      Hash code will be retrieved for the font object.

    • loadFont(InputStreami, double s):

      A font resource will be loaded from the mentioned input stream.

    • loadFont(StringurlStr, double size):

      A font resource will be loaded from the mentioned URL.

    • toString():

      Font object will be converted to a string representation.

    Examples of JavaFX Font

    Following are the examples are given below:

    Example #1

    Java Program that demonstrates the working of font class

    Code:

    importjavafx.application.Application;
    importjavafx.scene.Scene;
    importjavafx.scene.control.*;
    importjavafx.scene.layout.*;
    importjavafx.stage.Stage;
    importjavafx.scene.paint.*;
    importjavafx.scene.text.*;
    importjavafx.geometry.*;
    importjavafx.scene.shape.*;
    //main class
    publicclassFontSampleextends Application {
    // application launches here
    publicvoidstart(Stage st)
    {
    //exception handling
    try {
    // stage title
    st.setTitle(" Sample Font ");
    //  TextFlow creation
    TextFlowtf = newTextFlow();
    // create text
    Text txt = newText("Sample \n");
    // color setting of the text
    txt.setFill(Color.RED);
    // FONT CREATION
    Font f = Font.font("Verdana", FontWeight.BOLD, 33);
    // Text font setting
    txt.setFont(f);
    // text setting
    tf.getChildren().add(txt);
    //line space setting
    tf.setLineSpacing(21.0f);
    //  VBox creation
    VBoxvb = newVBox(tf);
    // svbox alignment setting
    vb.setAlignment(Pos.CENTER);
    // scene creation
    Scene sc = newScene(vb, 400, 200);
    // scene setting
    st.setScene(sc);
    st.show();
    }
    //catch exception
    catch (Exception ec) {
    System.out.println(ec.getMessage());
    }
    }
    // Main Method
    publicstaticvoidmain(String args[])
    {
    // application launches here
    launch(args);
    }
    }
    

    Example #2

    Java Program that demonstrates font class with a drop box.

    Code:

    importjavafx.application.Application;
    importjavafx.scene.Scene;
    importjavafx.scene.control.*;
    importjavafx.scene.layout.*;
    importjavafx.stage.Stage;
    importjavafx.scene.paint.*;
    importjavafx.scene.text.*;
    importjavafx.geometry.*;
    importjavafx.collections.*;
    importjavafx.event.ActionEvent;
    importjavafx.event.EventHandler;
    publicclassFontSampleextends Application {
    // application launches here
    publicvoidstart(Stage st)
    {
    try {
    // stage title
    st.setTitle(" Sample Font ");
    //  TextFlow creation
    TextFlowtf = newTextFlow();
    // create text
    Text txt = newText("Sample \n");
    // color setting of the text
    txt.setFill(Color.RED);
    // FONT CREATION
    Font f = Font.font("Verdana", FontWeight.BOLD, 33);
    //  names
    String w[] = { "RED", "BLACK", "PINK", "LIGHT GREEN",
    "YELLOW", "BLUE", "CYAN", "ORANGE", "VIOLET",
    };
    ComboBoxcb =
    newComboBox(FXCollections.observableArrayList(w));
    // Create action event
    EventHandler< ActionEvent>event =
    newEventHandler< ActionEvent>() {
    publicvoidhandle(ActionEvente)
    {
    // set font of the text
    txt.setFont(Font.font((String)cb.getValue(),
    FontWeight.valueOf((String)cb.getValue()), 20));
    }
    };
    // Create action event
    EventHandler< ActionEvent>event1 =
    newEventHandler< ActionEvent>() {
    publicvoidhandle(ActionEvente)
    {
    // set font of the text
    txt.setFont(Font.font((String)cb.getValue(),
    FontWeight.valueOf((String)cb.getValue()), 20));
    }
    };
    // Set on action
    cb.setOnAction(event);
    // set font of the text
    txt.setFont(f);
    // set text
    tf.getChildren().add(txt);
    // set line spacing
    tf.setLineSpacing(20.0f);
    // create a HBox
    HBoxhbox = newHBox(cb);
    // create VBox
    VBoxvbox = newVBox(hbox, tf);
    // set spacing
    vbox.setSpacing(30.0);
    // set alignment of vbox
    vbox.setAlignment(Pos.CENTER);
    // create a scene
    Scenesc = newScene(vbox, 400, 300);
    // set the scene
    st.setScene(sc);
    st.show();
    }
    catch (Exception ev) {
    System.out.println(ev.getMessage());
    }
    }
    // Main Method
    publicstaticvoidmain(String args[])
    {
    // launch the application
    launch(args);
    }
    }
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.