JavaFX Button with multiple text lines

前端 未结 6 2120
一个人的身影
一个人的身影 2021-01-13 04:35

I need to create a toolbar in my screen that will have multiple buttons, and each button must have multiple lines of Text. For example:

6条回答
  •  半阙折子戏
    2021-01-13 05:15

    My solution is pretty much the same as the one given by the OP, but instead of Label uses Text so it's more flexible to changes in the size of the button, as it will use as many lines as needed. If required, also one can set a wrapping width, to define a width constraint.

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        ImageView imageView = new ImageView(new Image(getClass().getResource().toExternalForm()));
        Text text=new Text("Some long text that may be line wrapped");
        text.setWrappingWidth(100);
        VBox vBox = new VBox(5, imageView,text);
        vBox.setAlignment(Pos.CENTER);
        btn.setGraphic(vBox);
        btn.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    
        Scene scene = new Scene(new StackPane(btn), 300, 250);
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

提交回复
热议问题