TextAlignment类是JavaFX的一部分。TextAlignment类表示水平文本对齐方式。TextAlignment类继承枚举类。
null
常用方法:
方法 | 解释 |
---|---|
valueOf(字符串n) | 返回指定名称的文本对齐方式。 |
价值观() | 返回一个数组,其中包含文本对齐的所有值。 |
例子: Java程序创建文本流并向其添加文本对象,设置文本对齐方式,还设置组合框以更改对齐方式并设置文本流的行距: 在这个程序中,我们将创建一个名为 平铺窗格 .将名为Label的标签和一些按钮添加到 平铺窗格 .设置屏幕的对齐方式 平铺窗格 使用 setAlignment() 作用将TextAlignment值的所有名称存储在字符串数组中。现在创建一个 下拉列表框 它将包含TextAlignment值的名称,还将创建一个操作事件来处理 下拉列表框 事件。事件处理程序会将TextFlow的TextAlignment设置为所选的TextAlignment值。现在创建一个VBox并添加 替利班 还有组合框 vbox .最后,将vbox添加到场景,并将场景添加到舞台,然后调用 show() 函数来显示最终结果。
// Java program to create a TextFlow and // add text object to it, set text Alignment // and also set a combo box to change Alignment // 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 Alignment_1 extends Application { // launch the application public void start(Stage stage) { try { // set title for the stage stage.setTitle( "Alignment" ); // create TextFlow TextFlow text_flow = new TextFlow(); // create text Text text_1 = new Text( "GeeksforGeeks" ); // set the text color text_1.setFill(Color.GREEN); // set font of the text text_1.setFont(Font.font( "Verdana" , 25 )); // create text Text text_2 = new Text( "How many times were you frustrated " + "while looking out for a good " + "collection of programming/algorithm/ " + "interview questions? What did you " + "expect and what did you get? " + "This portal has been created to " + "provide well written, well " + "thought and well explained solutions " + "for selected questions." ); // set the text color text_2.setFill(Color.BLUE); // set font of the text text_2.setFont(Font.font( "Helvetica" , FontPosture.ITALIC, 15 )); // add text to textflow text_flow.getChildren().add(text_1); text_flow.getChildren().add(text_2); // alignment names String weight[] = { "CENTER" , "JUSTIFY" , "LEFT" , "RIGHT" }; // Create a combo box ComboBox combo_box = new ComboBox(FXCollections.observableArrayList(weight)); // Create action event EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { // set alignment text_flow.setTextAlignment(TextAlignment.valueOf( (String)combo_box.getValue())); } }; // Set on action combo_box.setOnAction(event); // 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/TextAlignment.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END