在javaFx中,如何从场景构建器连接.fxml文件?

时间:2019-08-05 08:09:23

标签: javafx

我是JavaFX的初学者。JavaFX存在此问题。我想显示场景构建器中的.fxml文件,并使用Eclipse运行。但是我发现了这个问题。请帮助我。

这是我运行应用程序的主文件。在主文件中,我叫Register_Form.fxml文件名。

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TitledPane;


public class Main extends Application {
public void start(Stage primaryStage) {
    try {
    TitledPane tp=(TitledPane) FXMLLoader.load(Main.class.getResource("Register_Form.fxml"));
        primaryStage.setScene(new Scene(tp));
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}
}

这是我来自Scene Builder的Register_Form.fxml。

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="Registeration Form" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Register">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
     <children>
        <Label layoutX="208.0" layoutY="14.0" text="Registeration_Form" textFill="#2237a1" textOverrun="CENTER_ELLIPSIS">
           <font>
              <Font size="18.0" />
           </font>
        </Label>
        <TextField fx:id="userid" layoutX="211.0" layoutY="79.0" />
        <TextField fx:id="colorid" layoutX="211.0" layoutY="199.0" />
        <Text layoutX="152.0" layoutY="96.0" strokeType="OUTSIDE" strokeWidth="0.0" text="UserName" />
        <PasswordField fx:id="pwdid" layoutX="211.0" layoutY="137.0" />
        <Text layoutX="152.0" layoutY="154.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password" />
        <Text layoutX="155.0" layoutY="216.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Fav Color" />
        <Button layoutX="325.0" layoutY="276.0" mnemonicParsing="false" text="Create" />
        <Button layoutX="185.0" layoutY="276.0" mnemonicParsing="false" text="Back" />
     </children></AnchorPane>
</content>
</TitledPane>

这是Register.java

package application;
import javafx.scene.control.TextField;
import javafx.fxml.FXML;
import javafx.scene.control.PasswordField;

public class Register {
private TextField userid;
private TextField colorid;
private PasswordField pwdid;

public Register() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    new Register();

}
}

这是我在跑步时发现的错误。

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
 Caused by: java.lang.Error: Unresolved compilation problem: 
Syntax error on token "}", delete this token

at application.Register.<init>(Register.java:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
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 application.Main.start(Main.java:17)
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)
... 1 more
Exception running application.Main

1 个答案:

答案 0 :(得分:0)

如@fabian所述,您的控制器应为这样,

const returnAfterReading = async () => {
    let myEntityJSONConst = {
        myEntity : {}
    }
    myEntityJSONConst.myEntity = await dbQueryPromise();

    return myEntityJSONConst;
}

returnAfterReading().then(val => {
     console.log(val);
}).catch(err => {
     console.log(err);
});