2014-10-17 67 views
1

创建自定义的控制,我想创建JavaFX中的自定义控件。主要目标是创建一个充当按钮的节点。它只是一个标签,可以用css进行样式化,看起来像一个按钮。我创建了一个类MyControl扩展控件类,像这样:问题与JavaFX中

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

你能显示myControl.css吗? – 2014-10-18 00:12:28

+0

我已经添加了css文件myControl.css – assetCorp 2014-10-18 09:11:22

回答

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; 
} 
+1

非常感谢。我把它复制错了。 css'-fx-skin'属性链接到错误的文件。谢谢。 – assetCorp 2014-10-19 16:25:30