在JavaFX中显示字符串/标签 [英] Displaying String/Label in JavaFX

查看:485
本文介绍了在JavaFX中显示字符串/标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,想象一下如何在我的程序中显示文本,以便它可以在我创建的多边形形状的中间显示停止。我要做的是创建一个停止标志。我已经处理了创建和显示停止标志的问题,所以现在我只需要在t

I need help figouring out simply how to display a text in my program so that it can display "Stop" in the middle of my polygon shape I've created. What I'm trying to do is create a stop sign. I have already taken care of creating and displaying that stop sign, so now I just need to display "Stop" in 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);
}

}

推荐答案

StackPane 替换窗格并添加签名到它(在多边形之后):

Replace the Pane with StackPane and add sign to it (after the polygon):

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

Javadoc about the StackPane:


StackPane以后向前的方式布置其子节点。子项的z-order
由子项列表的顺序定义,其中
第0个孩子是最下面的孩子,最后一个孩子在最上面。如果设置了边框和/或
填充,则子项将在
insets内布局。

StackPane lays out its children in a back-to-front stack. The z-order of the children is defined by the order of the children list with the 0th child being the bottom and last child on top. If a border and/or padding have been set, the children will be layed out within those insets.

stackpane将尝试调整大小每个孩子填写其内容
区域。如果孩子的大小无法填充堆栈窗格(
,因为它不可调整大小或其最大大小阻止它),那么
将使用alignment属性在区域内对齐,
默认为Pos.CENTER。

The stackpane will attempt to resize each child to fill its content area. If the child could not be sized to fill the stackpane (either because it was not resizable or its max size prevented it) then it will be aligned within the area using the alignment property, which defaults to Pos.CENTER.

这篇关于在JavaFX中显示字符串/标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆