javafx中的自定义控件

时间:2014-05-25 12:04:02

标签: java javafx-8 fxml

我在JavaFX中创建一个自定义的AnchorPane(ItemCard),以便在另一个FXML文件中使用它。

ItemCard.fxml文件

    <?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>

<fx:root type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <AnchorPane fx:id="ItemCardPane" prefHeight="360.0" prefWidth="266.0" stylesheets="@ItemCard.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">

            <children>
                <ImageView fx:id="ItemCardView" fitHeight="399.0" fitWidth="245.0" layoutX="11.0" layoutY="6.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" BorderPane.alignment="CENTER" />
            </children>

        </AnchorPane>
    </children>
</fx:root>

ItemCard的控制器是:

import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;

/**
 *
 * @author ola
 */
public class ItemCard extends AnchorPane {

    @FXML
    ImageView ItemCardView;
    @FXML
    AnchorPane ItemCardPane;

    public ItemCard() {

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ItemCard.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();

        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    public void setImage(Image image) {
        ItemCardView.setImage(image);
//        getChildren().add(ItemCardView);
    }

}

我想在另一个fxml中使用这个itemCard Anchore Pane。 home.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import com.syrianep.reader.ui.KidsTheme.resources.*?>
<?import javafx.scene.layout.*?>


<AnchorPane fx:id="home" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1000.0" prefWidth="1250.0" style="-fx-border-radius: 5; -fx-background-color: tranparant;" stylesheets="@mainView.css" AnchorPane.bottomAnchor="0" AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml" fx:controller="com.syrianep.reader.ui.KidsTheme.resources.HomeController" >
    <children>
        <ItemCard layoutX="50.0" layoutY="50.0"AnchorPane.bottomAnchor="0" AnchorPane.leftAnchor="0"/>  
    </children>
</AnchorPane>

但问题是,当我使用场景构建器打开home.fxml时,不会查看itemCard。 错误是; 在javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)

0 个答案:

没有答案