如何在JavaFX中垂直显示按钮上的文本?

如何在JavaFX中垂直显示按钮上的文本?,java,javafx,Java,Javafx,我想创建一个按钮,在JavaFX中垂直显示它的文本。我知道rotate,但我不想使用它。是否有其他方法垂直命名按钮。rotate是一种方法,没有其他解决方案 示例代码: import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.StackPane; impor

我想创建一个按钮,在JavaFX中垂直显示它的文本。我知道rotate,但我不想使用它。是否有其他方法垂直命名按钮。

rotate是一种方法,没有其他解决方案

示例代码:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/* Displays a button with it's text rotated 90 degrees to the left */
public class RotatedText extends Application {
    @Override public void start(Stage stage) {
        Button feedbackButton = createButtonWithRotatedText("Feedback Form");

        StackPane layout = new StackPane();
        layout.getChildren().setAll(feedbackButton);
        StackPane.setAlignment(feedbackButton, Pos.TOP_RIGHT);
        layout.setPrefSize(225, 275);
        layout.setStyle("-fx-background-color: lightcyan;");

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private Button createButtonWithRotatedText(String text) {
        Button button = new Button();
        Label  label  = new Label(text);
        label.setRotate(-90);
        button.setGraphic(new Group(label));

        // in-line css just used for sample purposes,
        // usually you would apply a stylesheet.
        button.setStyle(
                "-fx-base: orange; " +
                "-fx-font-size: 30px; " +
                "-fx-text-background-color: whitesmoke;"
        );

        return button;
    }

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

button.setGraphic(新组(标签))
button.setGraphic(标签)
有什么好处?试试看。如果你把它包装成一个组,你就会得到想要的结果。如果你不把它打包,你会弄得乱七八糟。这是因为a的布局工作方式(与其他节点类型不同):“如果直接在此组的子级上设置变换和效果,则这些变换和效果将包含在此组的布局边界中”。这并不能解决此问题,但您可以通过在“场景生成器”中的按钮文本字段上设置多行自上而下显示文本。或者在文本字符串中设置换行符。“E\nX\nA\nM\nP\nL\nE”