1

コードで長方形を作成し、HBox作成したシーンビルダー内に配置しています。次に、既存の css スタイルを適用します。作成時に高さと幅を指定しない限り、長方形は表示されません (CSS スタイルで既に高さと幅が指定されていても)。ただし、表示することができた場合、css スタイルは適用されません。なぜこれが起こっているのかわかりません。誰かが私を助けてくれることを願っています! ありがとう

Rectangle rect = new Rectangle();
timesheetSlots.getChildren().addAll(rect);
rect.getStyleClass().add("timesheetSlot");
.timesheetSlot{
    -fx-background-color: #ff6600;
    -fx-width: 20;
    -fx-height: 25;
    -fx-cursor: hand;
}
.timesheetSlot:hover{
    -fx-background-color: #ffffff;
}
.timesheetSlotSelected{
    -fx-background-color: #1Eff00;
    -fx-max-height: 25;
    -fx-max-width: 20;
    -fx-cursor: hand;
}
4

1 に答える 1

2

Regionには適用できますが、には適用できないCSS プロパティを使用していますRectangle( 「CSS リファレンス」を参照)。でサポートされているすべてのプロパティは、Nodeから利用できますNode.getCssMetaData。評価中

new Rectangle().getCssMetaData().stream().map(CssMetaData::getProperty).sorted().forEach(System.out::println);

次のリストが得られます (Java 8 update 92):

-fx-arc-height
-fx-arc-width
-fx-blend-mode
-fx-cursor
-fx-effect
-fx-fill
-fx-focus-traversable
-fx-opacity
-fx-rotate
-fx-scale-x
-fx-scale-y
-fx-scale-z
-fx-smooth
-fx-stroke
-fx-stroke-dash-array
-fx-stroke-dash-offset
-fx-stroke-line-cap
-fx-stroke-line-join
-fx-stroke-miter-limit
-fx-stroke-type
-fx-stroke-width
-fx-translate-x
-fx-translate-y
-fx-translate-z
visibility

heightこれらのいずれも、またはwidthのスタイルを設定できませんRectanglefillまた、 a のは、 ではなく CSS プロパティRectangleを使用して割り当てられることに注意してください。-fx-fill-fx-background-color

回避策

使用するRegion

Rectangleを aRegionに置き換えて、最小サイズと最大サイズを幅/高さに設定するだけです。

.timesheetSlot{
    -fx-background-color: #ff6600;

    -fx-width: 20;
    -fx-min-width: -fx-width;
    -fx-max-width: -fx-width;

    -fx-height: 25;
    -fx-min-height: -fx-height;
    -fx-max-height: -fx-height;

    -fx-cursor: hand;
}

拡張中Rectangle

または、CSS を使用したサイズの割り当てをサポートするクラスでクラスを拡張しRectangleます。これには、2 つの新しいスタイル可能なプロパティを追加する必要があるため、いくつかのコードが必要です。

public class StyleableRectangle extends Rectangle {

    private final StyleableDoubleProperty styleableWidth = new SimpleStyleableDoubleProperty(WIDTH_META_DATA, this, "styleableWidth");
    private final StyleableDoubleProperty styleableHeight = new SimpleStyleableDoubleProperty(HEIGHT_META_DATA, this, "styleableHeight");

    public StyleableRectangle() {
        bind();
    }

    public StyleableRectangle(double width, double height) {
        super(width, height);
        initStyleableSize();
        bind();
    }

    public StyleableRectangle(double width, double height, Paint fill) {
        super(width, height, fill);
        initStyleableSize();
        bind();
    }

    public StyleableRectangle(double x, double y, double width, double height) {
        super(x, y, width, height);
        initStyleableSize();
        bind();
    }

    private void initStyleableSize() {
        styleableWidth.set(getWidth());
        styleableHeight.set(getHeight());
    }

    private final static List<CssMetaData<? extends Styleable, ?>> CLASS_CSS_META_DATA;

    private final static CssMetaData<StyleableRectangle, Number> WIDTH_META_DATA = new CssMetaData<StyleableRectangle, Number>("-fx-width", StyleConverter.getSizeConverter()) {

        @Override
        public boolean isSettable(StyleableRectangle styleable) {
            return !styleable.styleableWidth.isBound();
        }

        @Override
        public StyleableProperty<Number> getStyleableProperty(StyleableRectangle styleable) {
            return styleable.styleableWidth;
        }
    };

    private final static CssMetaData<StyleableRectangle, Number> HEIGHT_META_DATA = new CssMetaData<StyleableRectangle, Number>("-fx-height", StyleConverter.getSizeConverter()) {

        @Override
        public boolean isSettable(StyleableRectangle styleable) {
            return !styleable.styleableHeight.isBound();
        }

        @Override
        public StyleableProperty<Number> getStyleableProperty(StyleableRectangle styleable) {
            return styleable.styleableHeight;
        }
    };

    static {
        // combine already available properties in Rectangle with new properties
        List<CssMetaData<? extends Styleable, ?>> parent = Rectangle.getClassCssMetaData();
        List<CssMetaData<? extends Styleable, ?>> additional = Arrays.asList(HEIGHT_META_DATA, WIDTH_META_DATA);
        List<CssMetaData<? extends Styleable, ?>> own = new ArrayList(parent.size()+ additional.size());
        own.addAll(parent);
        own.addAll(additional);
        CLASS_CSS_META_DATA = Collections.unmodifiableList(own); 
    }

    // make metadata available for extending the class
    public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
        return CLASS_CSS_META_DATA;
    }

    @Override
    public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
        return CLASS_CSS_META_DATA;
    }

    private void bind() {
        this.widthProperty().bind(this.styleableWidth);
        this.heightProperty().bind(this.styleableHeight);
    }


    // -------------------------------------------------------------------------
    // ----------------------- PROPERTY METHODS --------------------------------
    // -------------------------------------------------------------------------

    public final double getStyleableHeight() {
        return this.styleableHeight.get();
    }

    public final void setStyleableHeight(double value) {
        this.styleableHeight.set(value);
    }

    public final DoubleProperty styleableHeightProperty() {
        return this.styleableHeight;
    }

    public final double getStyleableWidth() {
        return this.styleableWidth.get();
    }

    public final void setStyleableWidth(double value) {
        this.styleableWidth.set(value);
    }

    public final DoubleProperty styleableWidthProperty() {
        return this.styleableWidth;
    }

}

サンプル CSS:

.timesheetSlot {
    -fx-fill: brown;
    -fx-width: 20;
    -fx-height: 25;
    -fx-cursor: hand;
}
于 2016-07-20T12:24:19.293 に答える