未找到FXMLLoader方法加载的类异常

问题描述 投票:0回答:1

给出以下代码:

public class Main extends Application {

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

private Logger LOG = Logger.getLogger(Main.class.getName());

@Override
public void start(Stage primaryStage) throws Exception {
    URL url = Main.class.getResource("session.fxml");
    System.out.println("Value URL = " + url); 
    FXMLLoader.load(Main.class.getResource("session.fxml"));//errors here

    //Scene scene = new Scene(FXMLLoader.load(Main.class.getResource("session.fxml")));

}

FXMLLoader.load方法似乎出错,尽管getResource正确生成了预期的URL。

堆栈跟踪很长,但是源错误是:

Caused by: java.lang.ClassNotFoundException: SessionController
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more

此错误是什么意思?导致错误的代码出了什么问题?

BTW:session.fxml与此代码位于同一文件夹中,因此getResource()方法没有问题。

谢谢

编辑:为了回应Kleopatra的评论,这是fxml文件的完整源代码和调用它的代码:

FXML:

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

<!--
  ~ Open Source Software published under the Apache Licence, Version 2.0.
  -->

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SessionController">
    <center>
        <AnchorPane>
            <children>
                <TabPane tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                   <tabs>

       <Tab text="N-Queens Puzzle">
                       <content>
                           <SplitPane fx:id="splitWordList" dividerPositions="0.65">
                               <items>
                                   <AnchorPane>
                                       <children>
                                           <BorderPane fx:id="mainWordPane" styleClass="mainWordPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                               <center>

                                            </center>
                                               <bottom>
                                            </bottom>
                                               <top>
                                                   <Label fx:id="noSolutionLabel" style="-fx-font: 24 arial; -fx-label-padding: 5px" text="No solution found!" visible="false" />

                                               </top>
                                   <bottom>
                                      <HBox alignment="CENTER" prefHeight="45.0" prefWidth="448.0" spacing="15.0" BorderPane.alignment="CENTER">
                                         <children>
                                            <Label text="Number of Queens" />
                                            <TextField fx:id="queens" prefHeight="39.0" prefWidth="90.0" text="8" />
                                         </children>
                                         <BorderPane.margin>
                                            <Insets />
                                         </BorderPane.margin>
                                      </HBox>
                                   </bottom>
                                           </BorderPane>
                                       </children>
                                   </AnchorPane>
                                   <AnchorPane>
                                       <children>
                                <Accordion layoutX="-8.0" prefHeight="450.0" prefWidth="250.0">
                                  <panes>
                                    <TitledPane animated="false" text="Hill Climbing">
                                      <content>
                                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                               <children>
                                                  <Button id="hillClimbingBtn" layoutX="40.0" layoutY="108.0" mnemonicParsing="false" onAction="#runHillClimbing" prefWidth="162.0" text="Run Hill Climbing" />
                                                  <TextField id="hcIterations" fx:id="hcIterations" layoutX="15.0" layoutY="41.0" prefHeight="39.0" prefWidth="218.0" text="50000" />
                                                  <Label layoutX="14.0" layoutY="14.0" text="Max Number of Iterations" />
                                               </children>
                                            </AnchorPane>
                                      </content>
                                    </TitledPane>
                                    <TitledPane animated="false" text="Simulated Annealing">
                                      <content>
                                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                               <children>
                                                  <Button id="simulatedAnnealingBtn" layoutX="12.0" layoutY="227.0" mnemonicParsing="false" onAction="#runSimulatedAnnealing" prefWidth="225.0" text="Run Simulated Annealing" />
                                                  <Label layoutX="23.0" layoutY="1.0" text="Max Number of Iterations" />
                                                  <Label layoutX="23.0" layoutY="67.0" text="Temperature" />
                                                  <Label layoutX="23.0" layoutY="133.0" text="Cooling Factor" />
                                                  <TextField id="saIterations" fx:id="saIterations" layoutX="12.0" layoutY="28.0" text="50000" />
                                                  <TextField id="saTemperature" fx:id="saTemperature" layoutX="14.0" layoutY="94.0" text="120" />
                                                  <TextField id="saCoolingFactor" fx:id="saCoolingFactor" layoutX="12.0" layoutY="160.0" text="0.95" />
                                               </children>
                                            </AnchorPane>
                                      </content>
                                    </TitledPane>
                                    <TitledPane animated="false" text="Local Beam Search">
                                      <content>
                                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                               <children>
                                                  <Button id="localBeamSearchBtn" layoutX="20.0" layoutY="167.0" mnemonicParsing="false" onAction="#runLocalBeamSearch" text="Run Local Beam Search" />
                                                  <TextField id="lbIterations" fx:id="lbIterations" layoutX="12.0" layoutY="38.0" text="50000" />
                                                  <TextField id="lbStates" fx:id="lbStates" layoutX="12.0" layoutY="119.0" text="5" />
                                                  <Label layoutX="14.0" layoutY="14.0" text="Max Number of Iterations" />
                                                  <Label layoutX="12.0" layoutY="84.0" text="Number of States" />
                                               </children>
                                            </AnchorPane>
                                      </content>
                                    </TitledPane>
                                      <TitledPane animated="false" layoutX="10.0" layoutY="88.0" text="Genetic Algorithm">
                                         <content>
                                            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                               <children>
                                                  <Button id="geneticAlgorithmBtn" layoutX="24.0" layoutY="219.0" mnemonicParsing="false" onAction="#runGeneticAlgorithm" text="Run Genetic Algorithm" />
                                                  <Label layoutX="24.0" layoutY="77.0" text="Mutation Probability" />
                                                  <Label layoutX="24.0" layoutY="145.0" text="NumberOfGenerations" />
                                                  <TextField id="gaGenerations" fx:id="gaGenerations" layoutX="15.0" layoutY="172.0" text="50000" />
                                                  <TextField id="gaMutationProbability" fx:id="gaMutationProbability" layoutX="14.0" layoutY="106.0" text="0.20" />
                                                  <Label layoutX="22.0" layoutY="6.0" text="Initial Population Size" />
                                                  <TextField id="gaInitialPopulationSize" fx:id="gaInitialPopulationSize" layoutX="14.0" layoutY="36.0" text="10" />
                                               </children>
                                            </AnchorPane>
                                         </content>
                                      </TitledPane>
                                  </panes>
                                </Accordion>
                                       </children>
                                   </AnchorPane>
                               </items>
                           </SplitPane>
                       </content>
                   </Tab>
               </tabs>
            </TabPane>
        </children>
    </AnchorPane>
</center>

这里是旨在调用它的代码(fxml和同一目录中的代码):

 package algo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.net.URL;
import java.util.logging.*;


/**
 * Created by mrsfy on 27-Mar-17.
 */
public class Main extends Application {

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

    private Logger LOG = Logger.getLogger(Main.class.getName());

    @Override
    public void start(Stage primaryStage) throws Exception {
        //Main.class.getResource("session.fxml");
        URL url = Main.class.getResource("session.fxml");
        System.out.println("Value URL = " + url); 
        try {
        FXMLLoader.load(Main.class.getResource("session.fxml"));
        } catch (Exception e) {
            System.out.println(e.toString());
        }

        //Scene scene = new Scene(FXMLLoader.load(Main.class.getResource("session.fxml")));

        //primaryStage.setScene(scene);
        //primaryStage.setHeight(675);
        //primaryStage.setWidth(753);
        //primaryStage.setResizable(false);
        //primaryStage.show();


    }

}

java javafx fxml
1个回答
0
投票

[fxml文件中的fx:controller标记可能有问题。它应该提供目标控制器的路径和类名。

示例:

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

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

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="630.0" prefWidth="550.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.chess.application.GameController">
    <top>
        <MenuBar BorderPane.alignment="CENTER">

这里目标类是路径为com.chess.application.GameController的“ GameController”

© www.soinside.com 2019 - 2024. All rights reserved.