0

所以......我正在尝试javafx。我试图更改 UI 的一些内容(在这种情况下为 TableView),但有时我有点困惑......

如果我们有这个示例代码

public <S, T> TableCell<S, T> getTableCellFactory(BiFunction<T, TableCell, Node> content) {
    return new TableCell<S, T>() {
        @Override
        protected void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);
            setText(null);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            setGraphic(null);

            if (item != null && !empty && getTableRow() != null) {
                setGraphic(content.apply(item, this));
            }
        }
    };
}

public static final <S, T extends Object> TableColumn<S, T> getTWColumn(
        final String title, final double width,
        Callback<TableColumn.CellDataFeatures<S, T>, ObservableValue<T>> cellValueFactory,
        Callback<TableColumn<S, T>, TableCell<S, T>> cellFactory) {

    TableColumn clm = new TableColumn<>(title);
    clm.setCellValueFactory(cellValueFactory);
    if (cellFactory != null) {
        clm.setCellFactory(cellFactory);
    }
    clm.setPrefWidth(width);

    return clm;
}

public ObservableList<TableColumn<T, ?>> getTableColumns() {
    return FXCollections.observableArrayList(
            getTWColumn("columnTitle", 500.0,
                    (TableColumn.CellDataFeatures<T, T> p) -> new ReadOnlyObjectWrapper(p.getValue()),
                    (TableColumn<T, T> p) -> new FXContent().<T, T>getT_TableCellFactory()
            )
    );
}

1、2、3在加载和执行上有什么区别?

1 - (TableColumn p) -> FXContent.getT_TableCellFactory()

public abstract class FXContent {

    public static <S, T> TableCell<S, T> getT_TableCellFactory() {
        return getTableCellFactory((item, tc) -> {
            Text t = getBaseText(item.getSome1());
            t.wrappingWidthProperty().bind(tc.getTableColumn().widthProperty());

            return getBaseRightLayout(t);
        });
    }
}

2 - (TableColumn p) -> new FXContent().getT_TableCellFactory()

public final class FXContent {

    public FXContent() {}

    public <S, T> TableCell<S, T> getT_TableCellFactory() {
        return getTableCellFactory((item, tc) -> {
            Text t = getBaseText(item.getSome1());
            t.wrappingWidthProperty().bind(tc.getTableColumn().widthProperty());

            return getBaseRightLayout(t);
        });
    }
}

3 - 已实现 (TableColumn p) -> this.getT_TableCellFactory()

public Interface FXContent {

    default <S, T> TableCell<S, T> getT_TableCellFactory() {
        return getTableCellFactory((item, tc) -> {
            Text t = getBaseText(item.getSome1());
            t.wrappingWidthProperty().bind(tc.getTableColumn().widthProperty());

            return getBaseRightLayout(t);
        });
    }
}

问候。

4

1 回答 1

0

关于 1) 类加载器必须首先找到并加载类,如果它没有被缓存,在调用方法之前。

数字 2) 还要求类加载器完成它的工作,然后创建/实例化一个对象,然后才调用该方法。

第三个选项只是调用该方法。

于 2015-02-10T08:51:00.147 回答