📜  JavaFX |字体类

📅  最后修改于: 2022-05-13 01:55:11.969000             🧑  作者: Mango

JavaFX |字体类

字体类是 JavaFX 的一部分。 Font 类表示字体,用于在屏幕上呈现文本。字体的大小被描述为以点为单位,实际测量值约为 1/72 英寸。字体类继承Object 类

类的构造函数:

  1. Font(double s) : 创建一个指定大小的字体对象。
  2. Font(String n, double s) :创建具有指定名称和大小的字体对象。

常用方法:

MethodExplanation
font(double s)Creates a font object with specified size.
font(String f)Creates a font object with specified family name.
font(String f, double s)Creates a font object with specified family name and size.
font(String f, FontPosture p, double s)Creates a font object with specified family name, posture and size.
font(String f, FontWeight weight, double s)Creates a font object with specified family name, fontweight and size.
font(String f, FontWeight w, FontPosture p, double s)Creates a font object with specified family name, fontweight, font posture and size.
getDefault()Returns the default font.
getFamilies()Gets all the font families installed on the user’s system.
getFamily()Returns the family of the font.
getFontNames()Gets the names of all fonts that are installed on the users system.
getFontNames(String f)Gets the names of all fonts in the specified font family that are installed on the users system.
loadFont(InputStream in, double s)Loads a font resource from the specified input stream.
loadFont(String url, double s)Loads a font resource from the specified URL.
getName()The full font name.
getSize()The point size for this font.

下面的程序说明了字体类的使用:

  1. 用于创建字体对象并将其应用于文本的Java程序:在此程序中,我们将创建一个名为font的字体并指定其系列、字体粗细和大小。将此字体应用于文本并将此文本添加到名为 textflow 的 TextFlow。创建一个名为vbox的 VBox 并将文本流添加到vbox并将 vbox 添加到场景并将场景添加到舞台。调用show()函数来显示结果。
    // Java Program to create a font object 
    // 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 Font_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("Font");
      
                // 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, 25);
      
                // set font of the text
                text_1.setFont(font);
      
                // set text
                text_flow.getChildren().add(text_1);
      
                // set line spacing
                text_flow.setLineSpacing(20.0f);
      
                // 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);
        }
    }
    

    输出:

  2. 创建字体对象并将其应用于文本并允许用户从组合框中选择字体的Java程序:在此程序中,我们将创建一个名为font的字体并指定其系列、字体粗细和大小。将此字体应用于文本并将此文本添加到名为 textflow 的 TextFlow。创建一个名为vbox的 VBox 并将文本流添加到 vbox 并将 vbox 添加到场景并将场景添加到舞台。创建两个组合框并将字体名称添加到一个,将字体粗细添加到另一个,并创建一个EventHandler来处理组合框的事件并将字体设置为用户指定的类型。
    // Java Program to create a font object 
    // and apply it to a text and allow the 
    // user to select font from the combo box
    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.*;
    import javafx.collections.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
       
    public class Font_2 extends Application {
       
    // launch the application
    public void start(Stage stage)
    {
      
    try {
      
        // set title for the stage
        stage.setTitle("Font");
      
        // 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(Font.getFontNames().get(0), 
                              FontWeight.EXTRA_BOLD, 20);
      
        // font weight names
        String weight[] = { "BLACK", "BOLD",
                            "EXTRA_BOLD",
                            "EXTRA_LIGHT",
                            "LIGHT",
                            "MEDIUM",
                            "NORMAL",
                            "SEMI_BOLD",
                            "THIN" };
      
        // Create a combo box
        ComboBox combo_box =
          new ComboBox(FXCollections.observableArrayList(weight));
      
        // Create a combo box
        ComboBox combo_box1 = 
          new ComboBox(FXCollections.observableArrayList(Font.getFontNames()));
      
        // Create action event
        EventHandler event =
        new EventHandler() {
      
            public void handle(ActionEvent e)
            {
      
                // set font of the text
                text_1.setFont(Font.font((String)combo_box1.getValue(),
                     FontWeight.valueOf((String)combo_box.getValue()), 20));
            }
        };
      
        // Create action event
        EventHandler event1 = 
           new EventHandler() {
      
            public void handle(ActionEvent e)
            {
      
                // set font of the text
                text_1.setFont(Font.font((String)combo_box1.getValue(),
                      FontWeight.valueOf((String)combo_box.getValue()), 20));
            }
        };
      
        // Set on action
        combo_box.setOnAction(event);
        combo_box1.setOnAction(event1);
      
        // set font of the text
        text_1.setFont(font);
      
        // set text
        text_flow.getChildren().add(text_1);
      
        // set line spacing
        text_flow.setLineSpacing(20.0f);
      
        // create a HBox
        HBox hbox = new HBox(combo_box, combo_box1);
      
        // create VBox
        VBox vbox = new VBox(hbox, text_flow);
      
        // set spacing
        vbox.setSpacing(30.0);
      
        // 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);
    }
    }
    

    输出:

注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/Font.html