我需要帮助弄清楚如何在程序中显示文本,以便它可以在我创建的多边形形状的中间显示“停止”。我想做的是创建一个停车标志。我已经负责创建和显示该停止标志,所以现在我只需要在 t 中显示“停止”
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
with 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 个孩子位于底部,最后一个孩子位于顶部。如果边界和/或
填充已设置,子项将放置在这些填充中
插图。
堆栈窗格将尝试调整每个子项的大小以填充其内容
区域。如果无法调整子级的大小以填充堆栈窗格(或者
因为它无法调整大小或其最大尺寸阻止了它)然后它
将使用对齐属性在区域内对齐,其中
默认为 Pos.CENTER。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)