在.fxml中添加custome项

时间:2016-06-20 22:34:58

标签: java javafx fxml

我创建了一个名为NavButton的类。我希望能够将其用于.fxml文件。

问题是---> 我创建了一个带有SceneBuilder的.fxml文件,其中包含一些按钮,然后将标记Button替换为NavButton,但它不起作用。

我可以找到有关如何使用SceneBuilder在.fxml中添加服装项目的教程,但是我没有办法如何手动执行此操作。

  

NavButton 类:

 import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.BooleanPropertyBase;
 import javafx.css.PseudoClass;
 import javafx.scene.control.Button;

 public class NavButton extends Button {

        /** Define a pseudo class */
        private PseudoClass MARKED_PSEUDO_CLASS = PseudoClass.getPseudoClass("marked");
        BooleanProperty marked = new BooleanPropertyBase(false) {
            public void invalidated() {
                pseudoClassStateChanged(MARKED_PSEUDO_CLASS, marked.get());
            }

            @Override
            public Object getBean() {
                return NavButton.this;
            }

            @Override
            public String getName() {
                return "marked";
            }

        };

        public void setMarked(boolean m) {
            marked.set(m);
        }
    }
  

有问题的.fxml文件:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>

<fx:root prefHeight="44.0" prefWidth="626.0" style="-fx-border-color: white;" type="BorderPane" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
  <center>
  <GridPane prefHeight="32.0" prefWidth="514.0" BorderPane.alignment="CENTER">
     <columnConstraints>
        <ColumnConstraints />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
     </columnConstraints>
     <rowConstraints>
        <RowConstraints />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
     </rowConstraints>
     <children>
        <NavButton id="navigationBarButton" fx:id="djMode" mnemonicParsing="false" prefHeight="40.0" prefWidth="150.0" text="DJMode" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
        <NavButton id="navigationBarButton" fx:id="simpleMode" mnemonicParsing="false" prefHeight="40.0" prefWidth="150.0" text="SimpleMode" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
        <NavButton id="navigationBarButton" fx:id="librariesMode" mnemonicParsing="false" prefHeight="40.0" prefWidth="170.0" text="LibrariesMode" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
     </children>
   </GridPane>
  </center>
 <padding>
   <Insets bottom="10.0" left="5.0" right="5.0" top="5.0" />
 </padding>
<left>
   <Button fx:id="settings" mnemonicParsing="false" BorderPane.alignment="CENTER" />
 </left>
 <right>
   <HBox prefHeight="32.0" prefWidth="94.0" BorderPane.alignment="CENTER">
      <children>
         <Button fx:id="minimize" mnemonicParsing="false" styleClass="specialButton" />
         <Button fx:id="maximize_normalize" mnemonicParsing="false" styleClass="specialButton" />
         <Button fx:id="close" mnemonicParsing="false" styleClass="specialButton" />
      </children>
   </HBox>
 </right>
</fx:root>
  

错误是:

javafx.fxml.LoadException: NavButton is not a valid type.
 /C:/eclipse/Building%20The%20XRPlayer/builder/bin/fxmls/NavigationBar.fxml:25

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2774)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at windows.MainWindow$NavigationBar.<init>(MainWindow.java:533)
 ..........

1 个答案:

答案 0 :(得分:2)

当您更改Button的类时,您似乎没有更新导入。

首先 - 因为FXML真的不能很好地使用默认包(无论如何强烈反对),你需要将NavButton类放在一个包中:

package com.mycompany.mydepartment.myproject ;

// imports ...

public class NavButton extends Button {
   // code ...
}

然后将导入添加到FXML文件中:

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

<?import com.mycompany.mydepartment.myproject.NavButton?>
<!-- existing code... -->