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

时间:2013-09-04 09:50:13

标签: java javafx

我想在JavaFX中创建一个按钮并在其上垂直显示它的文本。我知道旋转,但我不想使用它。还有其他方法可以垂直命名按钮。enter image description here

1 个答案:

答案 0 :(得分:7)

旋转是实现此目的的方法,没有其他解决方案。

rotated

示例代码:

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); }
}