我需要帮助弄清楚如何在程序中显示文本,以便它可以在我创建的多边形形状的中间显示“停止”。我想做的是创建一个停车标志。我已经负责创建和显示该停车标志,因此现在只需要在屏幕上显示“停止”即可
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); }
}
更换Pane用StackPane,并添加sign到它(多边形后):
Pane
StackPane
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)在区域内对其进行对齐。
StackPane将其子级放在从后到前的堆栈中。子级的z顺序由子级列表的顺序定义,第0个子级是底部,最后一个子级在顶部。如果已设置边框和/或填充,则将在这些插图内布置孩子。
堆栈窗格将尝试调整每个子项的大小以填充其内容区域。如果无法调整子级的大小来填充堆栈窗格(因为无法调整大小或最大尺寸阻止了它),那么它将使用alignment属性(默认值为Pos.CENTER)在区域内对其进行对齐。