2016-02-20 112 views
0

我需要幫忙解釋如何在我的程序中顯示文本,以便它可以在我創建的多邊形形狀中間顯示「停止」。我想要做的是創建一個停車標誌。我已經採取了創建並顯示該站牌的照顧,所以現在我只需要在t顯示「停止」在JavaFX中顯示字符串/標籤

package application; 
public class Main extends Application { 
@Override 
public void start(Stage primaryStage) { 
    Pane pane = new Pane(); 
    Polygon polygon = new Polygon(); 
    pane.getChildren().add(polygon); 
    polygon.setFill(javafx.scene.paint.Color.RED); //sets the color of polygon 
    ObservableList<Double> list = polygon.getPoints(); 

    //create a label to display in the middle of the "stop sign" polygon shape 
    //This is what I need most help on 
    Label sign = new Label("Stop"); 
    sign.setAlignment(Pos.CENTER); 
    System.out.print(sign); 

    final double WIDTH = 200, HEIGHT = 200; 
    double centerX = WIDTH/2, centerY = HEIGHT/2; 
    double radius = Math.min(WIDTH, HEIGHT) * 0.4; 

    // Add points to the polygon list 
    for (int i = 0; i < 6; i++){ 
     list.add(centerX + radius * Math.cos(2 * i * Math.PI/6)); 
     list.add(centerY - radius * Math.sin(2 * i * Math.PI/6)); 
    } 

    // Create a scene and place it in the stage 
    Scene scene = new Scene(pane, WIDTH, HEIGHT); 
    primaryStage.setTitle("ShowPolygon"); //Set the stage title 
    primaryStage.setScene(scene); //Place the scene in the stage 
    primaryStage.show(); //Display the stage 



} 

public static void main(String[] args) { 
    launch(args); 
} 

}

回答

1

更換PaneStackPane並添加sign它(多邊形後):

public void start(Stage primaryStage) { 
    StackPane pane = new StackPane(); 
    Polygon polygon = new Polygon(); 
    polygon.setFill(javafx.scene.paint.Color.RED); //sets the color of polygon 
    ObservableList<Double> list = polygon.getPoints(); 

    //create a label to display in the middle of the "stop sign" polygon shape 
    //This is what I need most help on 
    Label sign = new Label("STOP"); 
    //sign.setStyle("-fx-text-fill: white; -fx-font-size: 3em"); 
    sign.setAlignment(Pos.CENTER); 
    System.out.print(sign); 


    pane.getChildren().add(polygon); 
    pane.getChildren().add(sign); 

    final double WIDTH = 200, HEIGHT = 200; 
    double centerX = WIDTH/2, centerY = HEIGHT/2; 
    double radius = Math.min(WIDTH, HEIGHT) * 0.4; 

    // Add points to the polygon list 
    for (int i = 0; i < 6; i++) { 
     list.add(centerX + radius * Math.cos(2 * i * Math.PI/6)); 
     list.add(centerY - radius * Math.sin(2 * i * Math.PI/6)); 
    } 

    // Create a scene and place it in the stage 
    Scene scene = new Scene(pane, WIDTH, HEIGHT); 
    primaryStage.setTitle("ShowPolygon"); //Set the stage title 
    primaryStage.setScene(scene); //Place the scene in the stage 
    primaryStage.show(); //Display the stage 


} 

的Javadoc有關StackPane

StackPane在後到前佈置其子疊加。兒童的z順序 由兒童列表的順序定義,其中第0個孩子是最後一個孩子。如果設置了邊框和/或填充,則孩子將被放置在這些插入內。

堆疊面板將嘗試調整每個子區域的大小以填充其內容 區域。如果孩子的體積不足以填充堆疊面板( ,因爲它不可調整大小或最大尺寸阻止),那麼它將使用alignment屬性在該區域內對齊,其中 默認爲Pos.CENTER。

1

只要改變new Pane()new StackPane()和你的標籤添加到窗格的孩子像你做你的多邊形列表。

StackPane是一個佈局管理器,允許您將項目層疊在一起(默認爲分層項目居中)。

相關問題