JavaFX |字体重量类
FontWeight 类是 JavaFX 的一部分。 FontWeight 类定义字体的粗细。 FontWeight 类指定在系统上搜索字体时可以使用的不同字体粗细。 FontWeight 类继承Enum类。
常用方法:
Method | Explanation |
---|---|
findByName(String n) | Returns FontWeight by its name. |
findByWeight(int w) | Returns the closest FontWeight for a weight value. |
getWeight() | Return the visual weight. |
valueOf(String n) | Returns the enum constant of this type with the specified name. |
values() | returns all the values of the FontWeight type. |
下面的程序说明了 FontWeight 类的使用:
- Java程序创建一个 TextFlow 并向其添加文本对象,设置文本对齐,还设置文本字体的字体粗细和设置文本流的行距:在这个程序中,我们将创建一个名为 tile_pane 的TilePane 。将名为 label 的标签和一些按钮添加到tile_pane 。使用setAlignment()函数设置tile_pane的对齐方式。将字体的字体粗细设置为EXTRA_BOLD 。将tile_pane添加到场景中并将场景添加到舞台并调用show()函数以显示最终结果。
// Java program to create a TextFlow and // add text object to it, set text Alignment // and also set font weight of the font of text // and set line spacing of the text flow. 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 FontWeight_1 extends Application { // launch the application public void start(Stage stage) { try { // set title for the stage stage.setTitle("FontWeight"); // 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); // set font of the text text_1.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 25)); // set text text_flow.getChildren().add(text_1); // set text Alignment text_flow.setTextAlignment(TextAlignment.CENTER); // 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); } }
输出:
- Java程序创建一个 TextFlow 并向其中添加文本对象,设置文本对齐方式,还设置文本字体的字体粗细,还设置一个组合框来更改字体粗细和设置文本流的行距:在这个程序中,我们将创建一个名为 tile_pane 的TilePane 。将名为 label 的标签和一些按钮添加到tile_pane 。使用setAlignment()函数设置tile_pane的对齐方式。现在将字体的 FontWeight 设置为EXTRA_BOLD 。将所有 FontWeight 值的名称存储在 String 数组中。现在创建一个组合框,其中将包含 FontWeight 值的名称,并创建一个动作事件来处理组合框事件。事件处理程序会将字体的字体粗细设置为所选的 FontWeight 值。现在创建一个 VBox 并将tilepane和组合框添加到vbox 。最后,将vbox添加到场景中并将场景添加到舞台并调用show()函数显示最终结果。
// Java program to create a TextFlow and // add text object to it, set text Alignment // and also set font weight of the font of text // and also set a combo box to change font weight // and set line spacing of the text flow. 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 FontWeight_2 extends Application { // launch the application public void start(Stage stage) { try { // set title for the stage stage.setTitle("FontWeight"); // 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); // set font of the text text_1.setFont(Font.font(Font.getFontNames().get(0), FontWeight.EXTRA_BOLD, 50)); // 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 action event EventHandler
event = new EventHandler () { public void handle(ActionEvent e) { // set font of the text text_1.setFont(Font.font(Font.getFontNames().get(0), FontWeight.valueOf((String)combo_box.getValue()), 50)); } }; // Set on action combo_box.setOnAction(event); // set text text_flow.getChildren().add(text_1); // set text Alignment text_flow.setTextAlignment(TextAlignment.CENTER); // set line spacing text_flow.setLineSpacing(20.0f); // create VBox VBox vbox = new VBox(combo_box, 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); } } 输出:
注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/FontWeight.html