我需要幫忙解釋如何在我的程序中顯示文本,以便它可以在我創建的多邊形形狀中間顯示「停止」。我想要做的是創建一個停車標誌。我已經採取了創建並顯示該站牌的照顧,所以現在我只需要在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);
}
}