您正在使用适用于 的 CSS 属性,Region
但不适用于Rectangle
(请参阅 CSS 参考)。a 的所有受支持属性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
orwidth
的样式Rectangle
。另请注意,fill
a是使用CSS 属性Rectangle
分配的,而不是使用.-fx-fill
-fx-background-color
解决方法
使用Region
您可以简单地Rectangle
用 a替换Region
并将最小和最大尺寸设置为宽度/高度:
.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;
}