gpt4 book ai didi

JavaFx:标签文本不换行

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:13 25 4
gpt4 key购买 nike

我有一个 BorderPane,其中 Pane 用于左、右和中心元素,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 中的内容变大,空间也会变小,但是,当它变得太大时,就会与中心 Pane 混在一起。

enter image description here

我尝试使用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;
}

关于JavaFx:标签文本不换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com