javafx错误访问控制器

时间:2017-05-09 22:58:12

标签: java javafx

我试图在javafx的舞台上设置两个场景,并使用控制器访问多个场景。所以我创建了MainFxml.fxml,这是一个Splitpane。里面注入了WorkArea.fxml和ListMenu.fxml。一切正常,直到我用它的控制器MainFxml.java连接MainFxml.fxml。我在下面粘贴代码。慢慢地放松了我的思绪。希望代码格式正确。

//MainFxml.fxml
<SplitPane dividerPositions="0.5" maxHeight="-Infinity" maxWidth="-Infinity" 
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" 
prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="com.test.pizzeriaFX.MainFxml">
   <items>
       <fx:include fx:id="workArea" source="WorkArea.fxml"/>
       <fx:include fx:id="listMenu"  source="ListMenu.fxml"/>
   </items>
</SplitPane>

public class MainFxml {

    @FXML private ListMenu listMenu;
    @FXML private WorkArea workArea;

    @FXML private void initialize() {
        System.out.println("iii");
        listMenu.init(this);
        workArea.init(this);
    }
}

//ListMenu.fxml
<AnchorPane prefHeight="400.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/8.0.111"
            xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="com.test.pizzeriaFX.ListMenu" >
      <ListView fx:id="menuListView" onMouseClicked="#handleMenuClick" 
prefHeight="600.0" prefWidth="232.0"  AnchorPane.bottomAnchor="0.0" 
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" 
AnchorPane.topAnchor="0.0"   />

public class ListMenu {
    private List<ListViewPosition> menuItems;

    private MainFxml main;

    public void init(MainFxml mainFxml) {
        this.main=mainFxml;

    }

    @FXML
    private TextArea mainTextArea;

    @FXML
    private ListView<ListViewPosition> menuListView;

    public void initialize(){

    }

    @FXML
    public void handleMenuClick() throws IOException {
    }
}

//WorkArea.fxml
<AnchorPane xmlns="http://javafx.com/javafx/8.0.111" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="com.test.pizzeriaFX.WorkArea"  
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" 
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" >
    <TextArea fx:id="mainTextArea" editable="false"  
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" 
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</AnchorPane>

public class WorkArea {

    private MainFxml main;

    @FXML private TextArea mainTextArea;

    public void initialize(){
        Datasource datasource = Datasource.getInstance();
    }

    public void init(MainFxml mainFxml) {
        this.main=mainFxml;
    }
}


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = 
FXMLLoader.load(getClass().getResource("MainFxml.fxml"));
        primaryStage.setTitle("Hello Pizza");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    @Override
    public void init() throws Exception {
        super.init();
    }

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

...... /

 "C:\Program Files\Java\jdk1.8.0_121\bin\java" "-
    javaagent:C:\Users\Marta\AppData\Roaming\JetBrains\IntelliJ IDEA 

 2017.1\lib\idea_rt.jar=58533:C:\Users\Marta\AppData\Roaming\JetBrains\IntelliJ 
    IDEA 2017.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program 
    Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program 

    Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;C:\Users\Marta\IdeaProjects\pizzeriaGUI\out\production\pizzeriaGUI;C:\Users\Marta\Desktop\sqlite-jdbc-3.16.1.jar" com.test.pizzeriaFX.Main
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        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(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
    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$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: javafx.fxml.LoadException: 
    /C:/Users/Marta/IdeaProjects/pizzeriaGUI/out/production/pizzeriaGUI/com/test/pizzeriaFX/MainFxml.fxml:7

        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 com.test.pizzeriaFX.Main.start(Main.java:15)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
        ... 1 more
    Caused by: java.lang.IllegalArgumentException: Can not set com.test.pizzeriaFX.WorkArea field com.test.pizzeriaFX.MainFxml.workArea to javafx.scene.layout.AnchorPane
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
        at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
        at java.lang.reflect.Field.set(Field.java:764)
        at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
        at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
        at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
        at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
        at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
        ... 17 more
    Exception running application com.test.pizzeriaFX.Main

    Process finished with exit code 1

0 个答案:

没有答案