在JavaFX中显示字符串/标签

时间:2016-02-20 00:40:38

标签: java string javafx label

我需要帮助,想象一下如何在我的程序中显示文本,以便它可以在我创建的多边形形状的中间显示“停止”。我要做的是创建一个停止标志。我已经处理了创建和显示停止标志的问题,所以现在我只需要在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);
}

}

2 个答案:

答案 0 :(得分:1)

只需将new Pane()更改为new StackPane(),然后将您的标签添加到窗格的子列表中,就像使用Polygon一样。

StackPane是一个布局管理器,允许您将项目叠加在一起(默认情况下以分层项为中心)。

答案 1 :(得分:1)

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个孩子是最底层的孩子。如果是边界和/或   已经设置了填充,孩子们将在那些内部铺设   插图。

     

stackpane将尝试调整每个子节点的大小以填充其内容   区域。如果孩子的大小不能填充堆叠窗格(或者   因为它不可调整大小或其最大大小阻止了它然后它   将使用alignment属性在区域内对齐   默认为Pos.CENTER。