簡體   English   中英

JavaFx:標簽文本不換行

[英]JavaFx: Label text doesn't wrap

我有一個BorderPane其中Pane用於左,右和中心元素,而HBox用於頂部元素。 在右側Pane有一個Label Label包含一些文本,這些文本不會自動換行,因此會被剪切。

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的內容變大時,空間會變小,但是,當它變大時,它將與中心Pane

在此處輸入圖片說明

我嘗試使用cellsCountLabel.setWrapText(true) ,但是沒有用。

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM