JavaFX将GridPane宽度保持在Scene宽度内并在Text

时间:2017-06-28 18:26:04

标签: java user-interface javafx textwrapping

我有一个JavaFX BorderPane,其中有一个GridPane用于保存内容。 GridPane由两列组成,一列为TextLabel(均在VBox中),另一列为Button。文本列应该是80%宽度,按钮列应该是20%宽度。 当我设置Text的文本并且它比列宽时,它会包裹到VBox的宽度。但是,当我更改窗口的大小以使其更宽,然后缩小窗口时,文本中的文本不会缩回。

您可以看到我尝试在wireListeners的{​​{1}}函数中限制宽度。我尝试了不同的组合,但没有任何工作。

这是我的代码:

发射器:

TestMainView

GUI:

package com.test.app;

import com.test.app.view.TestMainView;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TestGuiApplication extends Application {

    @Override
    public void start(Stage initialStage) throws Exception {

        TestMainView mainView = new TestMainView();

        initialStage.setScene(new Scene(mainView.getView(), 300, 300));

        mainView.init();
        initialStage.show();
    }

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

}

如何获取package com.test.app.view; import javafx.geometry.Insets; import javafx.scene.Parent; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.VBox; import javafx.scene.text.Text; public class TestMainView { private BorderPane borderPane; private GridPane gridPane; private VBox viewBox; private VBox selectedItemsColumn; private Label selectedItemsTitleLabel; private Text selectedItemsList; private Button goButton; public TestMainView() { this.borderPane = new BorderPane(); } public void init() { createView(); wireListeners(); } public BorderPane createView(){ borderPane.setCenter(createCenterView()); return borderPane; } public void wireListeners(){ goButton.setOnMouseClicked((event) -> { selectedItemsList.setText(selectedItemsList.getText() + "- X X X X X X"); }); //Tried the following listeners to prevent the text from growing otuside column size: //Keep border pane width matched to Scene borderPane.prefHeightProperty().bind(borderPane.getScene().heightProperty()); borderPane.prefWidthProperty().bind(borderPane.getScene().widthProperty()); //Set text wrapping width to bind to its VBox, this worked selectedItemsList.wrappingWidthProperty().bind(selectedItemsColumn.widthProperty()); //Set VBox width to match the gridpane column width selectedItemsColumn.prefWidthProperty().bind(gridPane.getColumnConstraints().get(0).percentWidthProperty()); } public VBox createCenterView(){ viewBox = new VBox(); viewBox.getChildren().add(createGridView()); return viewBox; } public GridPane createGridView(){ gridPane = new GridPane(); selectedItemsColumn = new VBox(); selectedItemsTitleLabel = new Label("Selected items:"); selectedItemsList = new Text("X X X X X X"); selectedItemsColumn.getChildren().addAll(selectedItemsTitleLabel, selectedItemsList); gridPane.add(selectedItemsColumn, 0, 0); goButton = new Button("Go!"); gridPane.add(goButton, 1, 0); ColumnConstraints col1 = new ColumnConstraints(); col1.setPercentWidth(80); ColumnConstraints col2 = new ColumnConstraints(); col2.setPercentWidth(20); gridPane.getColumnConstraints().addAll(col1, col2); gridPane.setHgap(10); gridPane.setVgap(10); gridPane.setPadding(new Insets(10,10,10,10)); gridPane.setGridLinesVisible(true); return gridPane; } public Parent getView(){ return borderPane; } } 的{​​{1}}内容以使其宽度与Text列的宽度相匹配而不会溢出列?

0 个答案:

没有答案