JavaFx:标签文本未换行

时间:2017-12-07 11:29:57

标签: java javafx javafx-8

我有BorderPanePane个用于左,右和中间元素,HBox用于顶部元素。在Pane右边有一个LabelLabel包含一些文本,它不会换行,因此会被剪切掉。

public class Main extends Application implements EventHandler<ActionEvent> {
    private static BorderPane gamePane;
    private static Pane cellsPane;
    private static HBox buttonsPane;
    private static Pane statsPane;
    private static Pane setupsPane;

    Label cellsCountLabel;    

    static int  gameWidth= 700;
    static int gameHeight=600;

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Hello World");

        gamePane = new BorderPane();

        buttonsPane = new HBox(5);
        statsPane = new Pane();
        cellsPane = makeGrid(20);
        setupsPane = new Pane();

        cellsPane.setStyle("-fx-background-color:  #ffffff; -fx-border-color: black");
        buttonsPane.setStyle("-fx-background-color:  #f2f2f2; -fx-border-color: black");
        statsPane.setStyle("-fx-background-color:  #f2f2f2; -fx-border-color: black");
        setupsPane.setStyle("-fx-background-color:  #f2f2f2; -fx-border-color: black");

        cellsPane.setMaxWidth(400);
        statsPane.setMaxWidth((gameWidth-400)/2);
        setupsPane.setMaxWidth((gameWidth-400)/2);

        createCellButton = new Button();
        deleteCellsButton = new Button();

        createCellButton.setText("Create a cell");
        deleteCellsButton.setText("Delete cells");

        ...

        buttonsPane.setSpacing(10);
        buttonsPane.setPadding(new Insets(10, 10, 10, 10));
        buttonsPane.getChildren().add(createCellButton);
        buttonsPane.getChildren().add(deleteCellsButton);

        gamePane.setTop(buttonsPane);
        gamePane.setCenter(cellsPane);

        ...

        cellsCountLabel = new Label("Cells Count: " + (cellId + 1));
        statsPane.getChildren().add(cellsCountLabel);
        gamePane.setLeft(statsPane);

        setupsPane.getChildren().add(new Label("Setups Panel1111115552222222133331111"));
        gamePane.setRight(setupsPane);

        gamePane.setMargin(statsPane, new Insets(0, 0, 0, 0));
        gamePane.setMargin(cellsPane, new Insets(0,0,0,0));
        gamePane.setMargin(setupsPane, new Insets(0,0,0,0));

        statsPane.setPrefWidth(150);
        setupsPane.setPrefWidth(150);

        cellsCountLabel.setWrapText(true);  //doesn't work

        Scene scene = new Scene(gamePane, gameWidth, gameHeight);
        primaryStage.setScene(scene);

        ...

        primaryStage.show();
}
...
}

但是,左,中,右Pane之间存在空格。随着Pane s中的内容变大,空间变小,但是当它变得太大时,它会与中心Pane混淆。

enter image description here

我尝试使用cellsCountLabel.setWrapText(true),但它没有用。

1 个答案:

答案 0 :(得分:0)

试试这个

Label label = new Label("Setups Panel1111115552222222133331111");
label.setWrapText(true);
label.setMaxWidth((gameWidth-400)/2);
setupsPane.getChildren().add(label);

此外,如果你走这条路,你可能想宣布这样的东西

static double sidePaneWidth = (gameWidth-400)/2;

供以后使用

如果需要,你也可以将它变成这样的功能

private Label newSidePaneLabel(String labelString){
    Label label = new Label(labelString);
    label.setWrapText(true);
    label.setMaxWidth(sidePaneWidth);
    return label;
}