在“场景”构建器中将css应用于fxml时无法加载外观:null

时间:2014-05-02 20:35:50

标签: javafx builder fxml skin scene

我正在尝试使用此示例为JavaFX创建旋钮控件作为起点:http://fxexperience.com/2012/01/fun-javafx-2-0-audio-player/

在该示例中,他们将皮肤(KnobSkin.java)应用于css文件中的滑块。

我创建了一个这样的自定义控件:

Knob.fxml

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<fx:root type="javafx.scene.layout.AnchorPane" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <children>
    <AnchorPane id="myTestButton" layoutX="0.0" layoutY="5.0" minHeight="49.0" prefHeight="140.0" prefWidth="14.0">
      <children>
        <Slider id="volume" orientation="VERTICAL" styleClass="knobStyle" />
      </children>
    </AnchorPane>
  </children>
</fx:root>

Knob.java

package application;
public class Knob extends AnchorPane {

    @FXML
    private AnchorPane myTestButton;

    public Knob() {
        FXMLLoader fxmlLoader = new FXMLLoader(

        getClass().getResource("/application/Knob.fxml"));

        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }
}

我的主要场景如下:

test.fxml

<?import application.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?scenebuilder-classpath-element ../../bin?>

<AnchorPane prefHeight="217.0" prefWidth="221.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <Knob layoutX="5.0" layoutY="4.0" />
</AnchorPane>

当我尝试在Scene Builder中打开test.fxml文件并将此css应用于它时:

.slider.knobStyle {
    -fx-skin: "application.KnobSkin";
}

我收到以下错误:

无法加载皮肤'StringProperty [bean:Slider [id = volume,styleClass = slider knobStyle],name:skinClassName,value:application.KnobSkin]'for control Slider [id = volume,styleClass = slider knobStyle]:null

如果我运行该应用程序,旋钮将完美显示。

知道我做错了吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

阅读此博文后:http://www.guigarage.com/2012/11/custom-ui-controls-with-javafx-part-1/

我发现如果我将我的班级Knob.java改为

package application;
public class Knob extends AnchorPane {

    @FXML
    private AnchorPane myTestButton;

    public Knob() {
        setSkinClassName(KnobSkin.class.getName());
    }
}

当我选择样式表时,我删除了旋钮将在Scene Builder中显示的Knob.fxml