JavaFX抽象控制器继承不起作用

时间:2017-12-17 08:37:15

标签: javafx

编写应用程序时遇到了一个大问题。 好吧,我尝试扩展我的ReaderController类,它是抽象的,并在我的fxml文件中设置为控制器。

有我的代码:

public abstract class ReaderController extends AnchorPane{
@FXML
TextField id;
@FXML
 TextField name;
@FXML
TextField surname;
@FXML
 TextField PESEL;
@FXML
TextField dateOfBirth;
@FXML
TextField documentType;
@FXML
TextField documentNumber;
@FXML
TextField email;
@FXML
TextField phoneNumber;
@FXML
TextField street;
@FXML
TextField city;
@FXML
Button addReader;

Parent root;
public ReaderController()
{

}

还有我的子类:

public class EditReaderController extends ReaderController{

public EditReaderController() throws IOException 
{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/AddUser.fxml"));
    root= (Parent) loader.load();
    loader.setController(this);
    Stage stage = new Stage();
    stage.setTitle("Edit User");
    stage.setScene(new Scene(root));
    stage.setResizable(false);
    stage.show();
}
public void initialize(){
    addReader.setText("Edit!");
    id.setText(String.valueOf(SQLController.getInstance().getLastIndexOfReader()));
}

我有一个错误:

aused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 75 more

那么如何将我的抽象控制器扩展到我的EditReaderController?

UPD 1 我的fxml文件

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.library.controllers.ReaderController">
   <children>
      <HBox layoutX="151.0" layoutY="64.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <VBox prefHeight="400.0" prefWidth="129.0">
               <children>
                  <GridPane prefHeight="300.0" prefWidth="129.0">
                     <columnConstraints>
                        <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                     </columnConstraints>
                     <rowConstraints>
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                     </rowConstraints>
                     <children>
                        <Label text="Nr czytelnika" />
                        <Label text="Imię" GridPane.rowIndex="1" />
                        <Label text="Nazwisko" GridPane.rowIndex="2" />
                        <Label text="PESEL" GridPane.rowIndex="3" />
                        <Label text="Data urodzenia" GridPane.rowIndex="4" />
                     </children>
                     <VBox.margin>
                        <Insets top="15.0" />
                     </VBox.margin>
                  </GridPane>
               </children>
            </VBox>
            <VBox prefHeight="400.0" prefWidth="178.0">
               <children>
                  <GridPane prefHeight="300.0" prefWidth="178.0">
                     <columnConstraints>
                        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                     </columnConstraints>
                     <rowConstraints>
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                     </rowConstraints>
                     <children>
                        <TextField fx:id="id" editable="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="26.0" prefWidth="34.0" />
                        <TextField fx:id="name" GridPane.rowIndex="1">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                        <TextField fx:id="surname" GridPane.rowIndex="2">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                        <TextField fx:id="PESEL" GridPane.rowIndex="3">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                        <TextField fx:id="dateOfBirth" editable="false" GridPane.rowIndex="4">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                     </children>
                     <VBox.margin>
                        <Insets top="15.0" />
                     </VBox.margin>
                  </GridPane>
               </children>
            </VBox>
            <VBox prefHeight="200.0" prefWidth="100.0">
               <children>
                  <GridPane prefHeight="300.0" prefWidth="99.0">
                     <columnConstraints>
                        <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                     </columnConstraints>
                     <rowConstraints>
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                     </rowConstraints>
                     <children>
                        <Label text="Typ dokumentu" />
                        <Label text="Nr dokumentu" GridPane.rowIndex="1" />
                        <Label text="E-mail" GridPane.rowIndex="2" />
                        <Label text="Nr telefonu" GridPane.rowIndex="3" />
                        <Label text="Ulica" GridPane.rowIndex="4" />
                        <Label text="Miasto" GridPane.rowIndex="5" />
                     </children>
                     <VBox.margin>
                        <Insets top="15.0" />
                     </VBox.margin>
                  </GridPane>
               </children>
            </VBox>
            <VBox prefHeight="400.0" prefWidth="195.0">
               <children>
                  <GridPane prefHeight="300.0" prefWidth="195.0">
                     <columnConstraints>
                        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                     </columnConstraints>
                     <rowConstraints>
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                     </rowConstraints>
                     <children>
                        <TextField fx:id="documentType">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                        <TextField fx:id="documentNumber" GridPane.rowIndex="1">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                        <TextField fx:id="email" GridPane.rowIndex="2">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                        <TextField fx:id="phoneNumber" GridPane.rowIndex="3">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                        <TextField fx:id="street" GridPane.rowIndex="4">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                        <TextField fx:id="city" GridPane.rowIndex="5">
                           <GridPane.margin>
                              <Insets right="25.0" />
                           </GridPane.margin>
                        </TextField>
                     </children>
                     <VBox.margin>
                        <Insets top="15.0" />
                     </VBox.margin>
                  </GridPane>
                  <Button fx:id="addReader" alignment="CENTER" mnemonicParsing="false" onAction="#onAddReaderAction" text="Dodaj" textAlignment="CENTER">
                     <VBox.margin>
                        <Insets left="100.0" top="25.0" />
                     </VBox.margin>
                  </Button>
               </children>
            </VBox>
         </children>
      </HBox>
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:1)

为什么在fxml中使用抽象类?

看看这一行:

fx:controller="com.library.controllers.ReaderController"

你有一个非抽象的子类。为什么不使用它?

fx:controller="com.library.controllers.EditReaderController"

或者使用你自己的非抽象类。

我在ReaderController中没有看到任何抽象成员 删除abstract修饰符也可以解决此问题。

此外,您的控制器类正在扩展AnchorPane ..

,这实在令人困惑

另据@sillyfly在评论中说,

  

在调用FXMLLoader #load后调用setController真的没什么用,因为此时已经注入了字段......我想它甚至会引发一个关于已经设置的控制器的异常... < / p>

根据评论,

  

我知道,但我不知道,如何通过操作设置新的控制器(例如点击编辑或添加按钮)

您应首先阅读https://docs.oracle.com/javafx/2/ui_controls/button.htm以了解如何在单击按钮时运行某些代码,然后运行

loader.setController(your new controller);

单击按钮时,删除fx:controller字段,因为您已经在逻辑代码中设置了控制器。