0

我在 javafx 有一个表格视图scene-builder。我想ALWAYS通过 css 使用 Vgrow 属性。我的表格视图在HBox. 场景构建器允许我使用这个属性。如图所示。

在此处输入图像描述

但是我如何使用 css 做到这一点。请帮我。

我已经以这种方式在css中尝试过

-fx-Vgrow: ALWAYS;

/* also */

Vgrow: ALWAYS;

没有任何效果。有什么帮助吗?

4

1 回答 1

1

只是为了使其 CSS 可定制,您正在考虑在控件上进行大量工作。所有需要的 css 元实现都定义在超类级别(因此大部分 css 元数据都定义在 Region 类中)。由于所有 javafx 控件都是 Region 的子类,它们确实满足 Region 中设置的所有 css 元数据属性。

如果您想拥有自己的指定 css 元数据,那么您必须明确定义您想要拥有的每个控件。(这又是一项大工作)。

我并不是说这是无法做到的。下面是自定义 ScrollPane 的演示代码,其中包含 vgrow 和 hgrow 作为 css 元属性。它完美地工作!试想一下,如果您想要所有控件的以下功能,则必须自定义要通过 css 配置的每个控件;)。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CustomCSSPropertyDemo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        VBox root = new VBox();
        root.setPadding(new Insets(15));
        root.setSpacing(15);
        Scene sc = new Scene(root, 600, 600);
        sc.getStylesheets().add(getClass().getResource("custom.css").toString());
        stage.setScene(sc);
        stage.show();

        Label label = new Label("Testing");
        MyScrollPane scrollPane = new MyScrollPane();
        scrollPane.getStyleClass().add("myclass");
        root.getChildren().addAll(label,scrollPane);
    }

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

css文件代码:

.myclass{
   -fx-vgrow-policy:always;
}

以及自定义的 ScrollPane 控件:

import com.sun.javafx.css.converters.EnumConverter;
import javafx.beans.property.ObjectProperty;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.css.StyleableObjectProperty;
import javafx.css.StyleableProperty;
import javafx.scene.control.Control;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyScrollPane extends ScrollPane {
    private ObjectProperty<Priority> hgrowPolicy;
    private ObjectProperty<Priority> vgrowPolicy;

    public final void setHgrowPolicy(Priority var1) {
        this.hgrowPolicyProperty().set(var1);
    }

    public final Priority getHgrowPolicy() {
        return this.hgrowPolicy == null ? Priority.SOMETIMES : (Priority)this.hgrowPolicy.get();
    }

    public final ObjectProperty<Priority> hgrowPolicyProperty() {
        if (this.hgrowPolicy == null) {
            this.hgrowPolicy = new StyleableObjectProperty<Priority>(Priority.SOMETIMES) {
                @Override
                protected void invalidated() {
                    HBox.setHgrow(MyScrollPane.this, this.get() );
                }

                public CssMetaData<MyScrollPane, Priority> getCssMetaData() {
                    return MyScrollPane.StyleableProperties.HGROW_POLICY;
                }

                public Object getBean() {
                    return MyScrollPane.this;
                }

                public String getName() {
                    return "hgrowPolicy";
                }
            };
        }

        return this.hgrowPolicy;
    }

    public final void setVgrowPolicy(Priority var1) {
        this.vgrowPolicyProperty().set(var1);
    }

    public final Priority getVgrowPolicy() {
        return this.vgrowPolicy == null ? Priority.SOMETIMES : (Priority)this.vgrowPolicy.get();
    }

    public final ObjectProperty<Priority> vgrowPolicyProperty() {
        if (this.vgrowPolicy == null) {
            this.vgrowPolicy = new StyleableObjectProperty<Priority>(Priority.SOMETIMES) {
                @Override
                protected void invalidated() {
                    VBox.setVgrow(MyScrollPane.this, this.get() );
                }
                public CssMetaData<MyScrollPane, Priority> getCssMetaData() {
                    return StyleableProperties.VGROW_POLICY;
                }

                public Object getBean() {
                    return MyScrollPane.this;
                }

                public String getName() {
                    return "vgrowPolicy";
                }
            };
        }

        return this.vgrowPolicy;
    }

    public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
        return MyScrollPane.StyleableProperties.STYLEABLES;
    }

    public List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() {
        return getClassCssMetaData();
    }

    private static class StyleableProperties {
        private static final CssMetaData<MyScrollPane, Priority> HGROW_POLICY;
        private static final CssMetaData<MyScrollPane, Priority> VGROW_POLICY;
        private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;

        private StyleableProperties() {
        }

        static {
            HGROW_POLICY = new CssMetaData<MyScrollPane, Priority>("-fx-hgrow-policy", new EnumConverter(Priority.class), Priority.SOMETIMES) {
                public boolean isSettable(MyScrollPane var1) {
                    return var1.hgrowPolicy == null || !var1.hgrowPolicy.isBound();
                }

                public StyleableProperty<Priority> getStyleableProperty(MyScrollPane var1) {
                    return (StyleableProperty)var1.hgrowPolicyProperty();
                }
            };
            VGROW_POLICY = new CssMetaData<MyScrollPane, Priority>("-fx-vgrow-policy", new EnumConverter(Priority.class), Priority.SOMETIMES) {
                public boolean isSettable(MyScrollPane var1) {
                    return var1.vgrowPolicy == null || !var1.vgrowPolicy.isBound();
                }

                public StyleableProperty<Priority> getStyleableProperty(MyScrollPane var1) {
                    return (StyleableProperty)var1.vgrowPolicyProperty();
                }
            };
            ArrayList list = new ArrayList(Control.getClassCssMetaData());
            list.add(HGROW_POLICY);
            list.add(VGROW_POLICY);
            STYLEABLES = Collections.unmodifiableList(list);
        }
    }

}

现在回头看,虽然我们的目标是让事情变得简单,但我们确实在让它变得更复杂。现在由您决定如何进行:)

于 2018-12-07T06:15:33.440 回答