java JavaFX - 场景中的居中文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37541279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:36:34  来源:igfitidea点击:

JavaFX - centered Text in Scene

javajavafxalignment

提问by Valentin Emil Cudelcu

I've got a JavaFX Text,Sceneand Group

我有一个 JavaFX TextScene并且Group

Text waitingForKey
Scene scene
Group root

I have a string String waitingForKeyStringwhich I'm adding to waitingForKeyand I want to have a center align.

我有一个waitingForKeyString要添加的字符串 StringwaitingForKey并且我想要居中对齐。

The problem is that sometimes the string has two sentences and I can't aligned it

问题是有时字符串有两个句子,我无法对齐

 String waitingForKeyString= " Level 2 \n\n" + "Press ENTER to start a new game";

OR

或者

String waitingForKeyString= " Press ENTER \n"+ "to start the game", messageString="";

String waitingForKeyString=" 按ENTER \n"+ "开始游戏", messageString="";

THEN

然后

Scene scene = new Scene(root,SCREEN_WIDTH, SCREEN_HEIGHT, Color.BLACK);
waitingForKey.setText(waitingForKeyString);
root.getChildren().add(waitingForKey);

So how could I align this to center? When I tried to align first waitingForKeyStringI destroyed the second `waitingForKeyString and vicecersa.

那么我怎么能把它对齐到中心呢?当我尝试先对齐时,waitingForKeyString我破坏了第二个 `waitingForKeyString,反之亦然。

回答by DVarga

You could use a StackPaneas root of your Sceneas it has an alignmentProperty.

您可以使用 aStackPane作为您的根,Scene因为它有一个alignmentProperty.

If you want individually align Nodesin the StackPaneyou can use public static void setAlignment(Node child, Pos value)method of StackPane.

如果你想单独地对准NodesStackPane可以使用public static void setAlignment(Node child, Pos value)的方法StackPane

Example (one Text is aligned in CENTER, another one is aligned in TOP LEFT)

示例(一个文本在中心对齐,另一个文本在左上角对齐)

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            StackPane root = new StackPane();
            Text text2 = new Text("I will be aligned TOPLEFT");
            Text text = new Text(" Level 2 \n\n" + "Press ENTER to start a new game");
            text.setTextAlignment(TextAlignment.CENTER);
            root.getChildren().addAll(text2, text);
            StackPane.setAlignment(text2, Pos.TOP_LEFT);
            StackPane.setAlignment(text, Pos.CENTER);


            Scene scene = new Scene(root, 400, 400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

回答by trashgod

Specify TextAlignment.CENTERfor the Textand use a layout like StackPanethat "defaults to Pos.CENTER."

指定TextAlignment.CENTERText,并使用一个布局就像StackPane是“默认Pos.CENTER”。

image

图片

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
/**
 * @see http://stackoverflow.com/a/37541777/230513
 */
public class Test extends Application {

    @Override
    public void start(Stage primaryStage) {
        Text waitingForKey = new Text("Level 2 \n\n" + "Press ENTER to start a new game");
        waitingForKey.setTextAlignment(TextAlignment.CENTER);
        waitingForKey.setFont(new Font(18));
        waitingForKey.setFill(Color.ALICEBLUE);
        StackPane root = new StackPane();
        root.getChildren().add(waitingForKey);
        Scene scene = new Scene(root, 320, 240, Color.BLACK);
        primaryStage.setTitle("Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}