當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JavaFX 類 TextAlignment用法及代碼示例


TextAlignment類是JavaFX的一部分。 TextAlignment類表示水平文本對齊方式。 TextAlignment類繼承Enum類。

常用方法:

方法 說明
valueOf(String n) 返回指定名稱的文本對齊方式。
values() 返回一個包含所有文本對齊方式值的數組。

例: Java程序創建一個TextFlow並向其中添加文本對象,設置文本Alignment並設置一個組合框以更改Alignment並設置文本流的行距:在此程序中,我們將創建一個名為tile_pane的TilePane。將名為label的Label和一些按鈕添加到tile_pane。使用setAlignment()函數設置tile_pane的對齊方式。將所有TextAlignment值的名稱存儲在String數組中。現在創建一個組合框,其中將包含TextAlignment值的名稱,還創建一個Action Event來處理組合框事件。事件處理程序會將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\n"); 
   
        // 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



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | TextAlignment Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。