我正在尝试在JavaFx中创建自定义控件。主要目的是创建一个充当按钮的节点。它只是一个标签,用css设计为看起来像一个按钮。我创建了一个类MyControl
,它扩展了控件类,如下所示:
public class MyControl extends Control {
@Override
protected String getUserAgentStylesheet() {
return MyControl.class.getResource("myControl.css").toExternalForm();
}
public MyControl() {
getStyleClass().add("new-control");
}
public MyControl(String item) {
getStyleClass().add("new-control");
}
}
}
对于SkinBase类MyControlSkin
,我有:
public class MyControlSkin extends SkinBase<MyControlSkin> {
public MyControlSkin(MyControlSkin control) {
super(control);
Label label = new Label("Some text here");
getChildren.add(label);
}
}
在myControl.css
文件中,我只需:
.tree-node-view {
-fx-skin: "treeviewsample.TreeViewSkin";
}
.label {
-fx-text-fill: -fx-text-background-color;
}
我为此创建了css,但问题是我没有看到场景上显示的标签:
MyControl control = new MyControl();
但屏幕上没有显示标签。请帮助我,我是新手,所以如果我的问题没有意义,请向我询问更多信息。
我正在使用javaFx 2.2
答案 0 :(得分:2)
我不知道你是否在将代码从IDE转移到帖子时出错,但是因为它的皮肤类不能编译。你需要
public class MyControlSkin extends SkinBase<MyControl> {
public MyControlSkin(MyControl control) {
super(control);
Label label = new Label("Some text here");
getChildren().add(label);
}
}
css必须为与控件匹配的类定义fx:skin
属性。所以你需要
.new-control {
-fx-skin: "MyControlSkin";
}
.label {
-fx-text-fill: -fx-text-background-color;
}