Javafx 8 在JavaFX中,如何使用CSS切换皮肤?
在JavaFX中,通过CSS定义皮肤的正确位置是什么?假设我有一个非常简单的自定义控件Javafx 8 在JavaFX中,如何使用CSS切换皮肤?,javafx-8,javafx-css,Javafx 8,Javafx Css,在JavaFX中,通过CSS定义皮肤的正确位置是什么?假设我有一个非常简单的自定义控件 公共类ActionLink扩展了控制{ private final StringProperty text=新SimpleStringProperty(); 公共最终StringProperty textProperty(){return text;} 公共最终字符串getText(){return text.get();} 公共最终void setText(字符串文本){this.text.set(文本);
公共类ActionLink扩展了控制{
private final StringProperty text=新SimpleStringProperty();
公共最终StringProperty textProperty(){return text;}
公共最终字符串getText(){return text.get();}
公共最终void setText(字符串文本){this.text.set(文本);}
}
我知道我可以在控件中指定默认外观。假设我有一个ActionLinkSkin
@覆盖
受保护的皮肤createDefaultSkin(){
返回新的ActionLinkSkin(此);
}
如果我不使用createDefaultSkin()
,我会看到一条严重的
警告,指示找不到皮肤:
CSS中尚未为定义-fx skin属性ActionLink@59ebabd \
并且createDefaultSkin()返回null。
首先,我不确定控件的CSS属于哪里。我应该为控件使用样式表,action link.css
,还是应该为皮肤使用样式表,action link skin.css
?在我看来,皮肤和CSS似乎将是相当相互依赖的,但使用样式表作为控件似乎更受欢迎
第二,也是我的主要问题,我不知道在哪里定义-fx skin
,所以它会被正确地发现
现在,假设我的CSS设置正确,如果我想包括一个备用皮肤,ActionLinkButtonSkin
?是否有一种方法可以设置它,以便父组件可以通过CSS选择任一皮肤?例如:
.action链接{
-fx皮肤:“ActionLinkSkin”;
}
…或:
.action链接{
-fx皮肤:“ActionLinkButtonSkin”;
}
我在这里遇到的问题是,我没有将操作链接
样式添加到我的控件中。下面是我的简单ActionLink
控件的完整版本:
public class ActionLink extends Control {
static {
// Stylesheets is a custom class that resides alongside my CSS files
// and returns properly externalized locations in a convention based
// manner.
StyleManager.getInstance().addUserAgentStylesheet(
Stylesheets.byConvention(ActionLink.class)
);
}
private final StringProperty text = new SimpleStringProperty();
public final StringProperty textProperty() {return text;}
public final String getText() {return text.get();}
public final void setText(String text) {this.text.set(text);}
public ActionLink() {
getStyleClass().setAll("action-link");
}
}