应用程序启动方法中未指定控制器的Javafx错误异常
Javafx error exception in Application start method no controller specified
当我在 运行 intellij 中使用 java 8 编写以下代码时,出现错误。请帮忙。 fxml 是使用场景生成器生成的,我认为按钮 Dagrooster 未与该方法链接。
package Weert;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Weert.fxml"));
primaryStage.setTitle("test");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Weert.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.web.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<top>
<GridPane alignment="TOP_CENTER" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="btnDagrooster" alignment="TOP_CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#Dagrooster" text="Dagrooster" textAlignment="CENTER" GridPane.columnIndex="0" />
<Button fx:id="btnHuidigRooster" alignment="TOP_CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Huidig Rooster" textAlignment="CENTER" GridPane.columnIndex="1" />
<Button fx:id="btnNieuwRooster" alignment="TOP_CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Nieuw Rooster" textAlignment="CENTER" GridPane.columnIndex="2" />
</children>
</GridPane>
</top>
<bottom>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button alignment="TOP_CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Instellingen" textAlignment="CENTER" />
<Button alignment="TOP_CENTER" contentDisplay="CENTER" layoutX="10.0" layoutY="13.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Laden..." textAlignment="CENTER" GridPane.columnIndex="1" />
</children>
</GridPane>
</bottom>
<center>
<WebView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Controller.java:
package Weert;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable
{
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
}
@FXML
public void Dagrooster(ActionEvent event)
{
System.out.println("lollolol");
}
}
错误:
Exception in Application start method
Exception in thread "main" 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$launchApplication2(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda/381259350.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: No controller specified.
/F:/foo/Weert.fxml:24
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:553)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:595)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:766)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2827)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2536)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at Weert.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication19(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda/826215799.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda/1784984253.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda/965429099.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater1(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda/1334999168.run(Unknown Source)
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$null5(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda/184875764.run(Unknown Source)
... 1 more
错误表明,FXML 缺少 fx:controller
声明。将控制器声明添加到 BorderPane
声明,如图所示:
...
<BorderPane maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Weert.Controller">
...
要了解如何通过 SceneBuilder 设置控制器 class,您可以查看 this question。
正如错误所暗示的那样,问题出在 "fxml",其中它指的是不存在的 class。要解决此搜索,请搜索一个名为 "fx:controller" 的属性并为其分配合格的 class 名称。例如 "fx:controller="packagename.className".
您确实没有告诉 fxml 文件控制器 class 在哪里...
这个...
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
这样...
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Weert.Controller">
甚至这个...
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
.. 然后将两个文件放在同一个文件夹中
当我在 运行 intellij 中使用 java 8 编写以下代码时,出现错误。请帮忙。 fxml 是使用场景生成器生成的,我认为按钮 Dagrooster 未与该方法链接。
package Weert;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Weert.fxml"));
primaryStage.setTitle("test");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Weert.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.web.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<top>
<GridPane alignment="TOP_CENTER" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="btnDagrooster" alignment="TOP_CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#Dagrooster" text="Dagrooster" textAlignment="CENTER" GridPane.columnIndex="0" />
<Button fx:id="btnHuidigRooster" alignment="TOP_CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Huidig Rooster" textAlignment="CENTER" GridPane.columnIndex="1" />
<Button fx:id="btnNieuwRooster" alignment="TOP_CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Nieuw Rooster" textAlignment="CENTER" GridPane.columnIndex="2" />
</children>
</GridPane>
</top>
<bottom>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button alignment="TOP_CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Instellingen" textAlignment="CENTER" />
<Button alignment="TOP_CENTER" contentDisplay="CENTER" layoutX="10.0" layoutY="13.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Laden..." textAlignment="CENTER" GridPane.columnIndex="1" />
</children>
</GridPane>
</bottom>
<center>
<WebView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Controller.java:
package Weert;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable
{
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
}
@FXML
public void Dagrooster(ActionEvent event)
{
System.out.println("lollolol");
}
}
错误:
Exception in Application start method
Exception in thread "main" 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$launchApplication2(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda/381259350.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: No controller specified.
/F:/foo/Weert.fxml:24
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:553)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:595)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:766)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2827)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2536)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at Weert.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication19(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda/826215799.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda/1784984253.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda/965429099.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater1(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda/1334999168.run(Unknown Source)
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$null5(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda/184875764.run(Unknown Source)
... 1 more
错误表明,FXML 缺少 fx:controller
声明。将控制器声明添加到 BorderPane
声明,如图所示:
...
<BorderPane maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Weert.Controller">
...
要了解如何通过 SceneBuilder 设置控制器 class,您可以查看 this question。
正如错误所暗示的那样,问题出在 "fxml",其中它指的是不存在的 class。要解决此搜索,请搜索一个名为 "fx:controller" 的属性并为其分配合格的 class 名称。例如 "fx:controller="packagename.className".
您确实没有告诉 fxml 文件控制器 class 在哪里...
这个...
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
这样...
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Weert.Controller">
甚至这个...
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
.. 然后将两个文件放在同一个文件夹中