在 JavaFX 中显示 String/Label
Displaying String/Label in JavaFX
我需要帮助弄清楚如何在我的程序中显示文本,以便它可以在我创建的多边形形状的中间显示 "Stop"。我想做的是创建一个停车标志。我已经负责创建和显示停车标志,所以现在我只需要在 t
中显示 "Stop"
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);
}
}
只需将 new Pane()
更改为 new StackPane()
并将您的标签添加到窗格的子列表中,就像您添加多边形一样。
StackPane 是一个布局管理器,允许您将项目层叠在一起(默认情况下将分层项目居中)。
将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 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.
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.
我需要帮助弄清楚如何在我的程序中显示文本,以便它可以在我创建的多边形形状的中间显示 "Stop"。我想做的是创建一个停车标志。我已经负责创建和显示停车标志,所以现在我只需要在 t
中显示 "Stop"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);
}
}
只需将 new Pane()
更改为 new StackPane()
并将您的标签添加到窗格的子列表中,就像您添加多边形一样。
StackPane 是一个布局管理器,允许您将项目层叠在一起(默认情况下将分层项目居中)。
将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 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.
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.