如何根据JavaFx中标签中的文本调整我的VBox和Label大小?

时间:2016-01-31 13:25:25

标签: java javafx fxml

enter image description here

如您所见,我的文本受label s约束。如何使label调整大小以完全适合文本,并相应地调整VBoxGridPane的大小。我只想垂直调整大小。

我的FXML:

<VBox fx:id="body"
    alignment="TOP_CENTER"
    prefHeight="150"
    GridPane.vgrow="SOMETIMES"
    prefWidth="300"
    GridPane.columnIndex="0"
    GridPane.rowIndex="1" spacing="5">

    <Label alignment="CENTER" textAlignment="JUSTIFY" fx:id="word" maxWidth="268"/>
    <Pane prefHeight="10" VBox.vgrow="NEVER"/>
    <Label alignment="CENTER" textAlignment="JUSTIFY" wrapText="true" fx:id="meaning" maxWidth="268"  />
    <Label alignment="CENTER"  textAlignment="JUSTIFY" wrapText="true" fx:id="sentence" maxWidth="268" />

</VBox>

VBox位于GridPane

<GridPane fx:id="base"
      alignment="CENTER"
      prefHeight="210.0"
      maxWidth="310.0"
      stylesheets="@style.css"
      vgap="0" xmlns="http://javafx.com/javafx/8"
      xmlns:fx="http://javafx.com/fxml/1"
      fx:controller="sample.Controller">
...

@James_D要求的最小,完整且可验证的示例

主类:

public class Main extends Application {
private Stage stage;
private StackPane stackPane;
@Override
public void start(Stage primaryStage) throws Exception{
    stage = primaryStage;
    FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
    Parent root = (Parent) loader.load();
    stackPane = new StackPane(root);
    Scene scene = new Scene(stackPane);
    scene.setFill(Color.WHITE);
    stage.setScene(scene);
    stage.show();
}


public static void main(String[] args) {
    launch(args);
}
}

控制器类:

public class Controller implements Initializable{
    @FXML private Label label1;
    @FXML private Button changeLabel;
    private StringProperty labelString = new SimpleStringProperty();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        labelString.setValue("aasdasd sad asdasd asdasda");
        label1.textProperty().bind(labelString);

    }
    @FXML
    public void clicked(MouseEvent e){
        labelString.setValue("asdsadasd asdasdasd sfdgsfwoef fgtrhfgbdrgdf dfgdfivbjkfd gdfgidfjvdf gdfgjldkvbdf gjdilgjdfv dfgojdflkgdf ");
    }
}

sample.fxml:

<GridPane fx:controller="sample.Controller"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10" maxHeight="Infinity">
<children>
    <VBox fx:id="body"
          alignment="CENTER"
          maxHeight="Infinity"
          GridPane.vgrow="SOMETIMES"
          prefWidth="300"
          GridPane.columnIndex="0"
          GridPane.rowIndex="1" spacing="5">

    <Label alignment="CENTER" VBox.vgrow="ALWAYS" wrapText="true" textAlignment="CENTER" fx:id="label1" maxWidth="268"/>
    <Button fx:id="changeLabel" text="Change" minWidth="50" onMouseClicked="#clicked" maxWidth="Infinity" />
    </VBox>
</children>

期望: 当我按“更改”时按钮,一切都应调整大小,以便为文本显示提供足够的空间。

问题: 当我按“更改”时按钮,UI保持不变,不显示全文。

1 个答案:

答案 0 :(得分:3)

假设您希望文本换行,标签需要能够垂直增长。添加属性

maxHeight="Infinity"

到每个标签。您还使用VBox约束prefHeight="150"的总体高度;删除该属性,让VBox找出自己的高度。同样,您可能希望从prefHeight删除GridPane属性。