java.lang.UnsupportedOperationException:无法确定属性javafxml的类型

时间:2018-04-27 09:52:13

标签: java javafx

尝试启动我的应用程序,但是,我得到的只是这个错误。这是我的代码,我认为是根本原因。这种类型的错误对我来说是新的,因为它是我第一次使用javafx时,我无法在错误列表中看到问题的根源。 Main.java加载程序

public class Main extends Application {



public void start(Stage primaryStage) {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("/gui/ClientGui.fxml"));
        Scene scene = new Scene(root,622,588);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setTitle("Denkibot - Laget med Dialogflow");

控制器

package controller;

import javafx.scene.control.Button;

public class FxControl {

    public Button sendBtn;

    public void sendMsg() {
        System.out.println("drep meg");
    }

}

这是 fxml ,这可能是主要问题

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

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

<AnchorPane prefHeight="588.0" prefWidth="622.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.FxControl">
   <children>
      <Pane prefHeight="400.0" prefWidth="600.0">
         <children>
            <MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="634.0">
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="New Session" />
                        <MenuItem mnemonicParsing="false" text="Exit Application" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Options">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Color" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="About">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Dialogflow" />
                        <MenuItem mnemonicParsing="false" text="Creators" />
                  </items>
                </Menu>
              </menus>
               <cursor>
                  <Cursor fx:constant="HAND" />
               </cursor>
            </MenuBar>
            <TextArea fx:id="chatWindow" editable="false" layoutX="25.0" layoutY="41.0" prefHeight="501.0" prefWidth="570.0" promptText="Velkommen til Denkibot Demo. Si &quot;Hallo&quot; for å starte en samtale.">
               <cursor>
                  <Cursor fx:constant="DEFAULT" />
               </cursor>
            </TextArea>
         </children>
      </Pane>
      <TextField fx:id="userInput" layoutX="25.0" layoutY="549.0" onKeyPressed="#sendMsg" prefHeight="25.0" prefWidth="512.0" promptText="Skriv meldingen her...">
         <cursor>
            <Cursor fx:constant="TEXT" />
         </cursor>
      </TextField>
      <Button fx:id="sendBtn" layoutX="548.0" layoutY="549.0" mnemonicParsing="false" onClick="#sendMsg" text="Send">
         <cursor>
            <Cursor fx:constant="HAND" />
         </cursor>
      </Button>
   </children>
   <cursor>
      <Cursor fx:constant="DEFAULT" />
   </cursor>
</AnchorPane>

错误

javafx.fxml.LoadException:
/C:/Users/mariu/Desktop/Denkibot/samples/clients/text-client/target/classes/gui/ClientGui.fxml:54

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at gui.Main.start(Main.java:18)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsupportedOperationException: Cannot determine type for property.
    at com.sun.javafx.fxml.BeanAdapter.getSetterMethod(BeanAdapter.java:177)
    at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:250)
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:618)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    ... 17 more

1 个答案:

答案 0 :(得分:1)

您需要使用onActiononMouseClicked代替onClick

Button不包含onClick属性。

<Button fx:id="sendBtn" layoutX="548.0" layoutY="549.0" mnemonicParsing="false" onAction="#sendMsg" text="Send">
    <cursor>
        <Cursor fx:constant="HAND" />
    </cursor>
</Button>