我今年开始使用javafx,我正在尝试运行此代码来打开一个新窗口,但是当我运行代码时会遇到LoadException。
这是我的代码
package gui;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Locale;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import domein.DomeinController;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class TitelbeeldschermController extends AnchorPane {
@FXML
private Button btnFrans;
@FXML
private Button btnNederlands;
@FXML
private Button btnEngels;
@FXML
private Button btnMaaknieuweSpeler;
@FXML
private Button btnMaakNieuweWedstrijd;
@FXML
private Button btnBestaandeWedstrijdLaden;
@FXML
private AnchorPane anchorPane;
private DomeinController dc;
public TitelbeeldschermController(DomeinController dc){
this.dc = dc;
FXMLLoader loader = new FXMLLoader(getClass().getResource("Titelbeeldscherm.fxml"));
loader.setRoot(this);
loader.setController(this);
try{
loader.load();
}catch(IOException ex){
throw new RuntimeException(ex);
}
}
@FXML
public void btnMaakNieuweSpelerOnAction(javafx.event.ActionEvent event) throws IOException{
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MaakNieuweSpeler.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle(dc.getTaal().getText("nieuweSpeler"));
stage.setScene(new Scene(root1));
stage.show();
}catch(IOException ex){
ex.printStackTrace();
}
}
@FXML
private void btnMaakNieuweWedstrijdOnAction(javafx.event.ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MaakNieuweSpeler.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("ABC");
stage.setScene(new Scene(root1));
stage.show();
}catch(IOException ex){
ex.printStackTrace();
}
}
@FXML
private void btnBestaandeWedstrijdLadenOnAction(javafx.event.ActionEvent event) {
}
@FXML
private void btnMenuTaalNederlandsAction(javafx.event.ActionEvent event) {
dc.setTaal("nl");
btnFrans.setVisible(false);
btnEngels.setVisible(false);
btnNederlands.setVisible(false);
btnMaaknieuweSpeler.setText(dc.getTaal().getText("accountMaken"));
btnMaaknieuweSpeler.setVisible(true);
btnMaakNieuweWedstrijd.setText(dc.getTaal().getText("startNieuwSpel"));
btnMaakNieuweWedstrijd.setVisible(true);
btnBestaandeWedstrijdLaden.setText(dc.getTaal().getText("laadWedstrijd"));
btnBestaandeWedstrijdLaden.setVisible(true);
}
@FXML
private void btnMenuTaalEngelsAction(javafx.event.ActionEvent event) {
dc.setTaal("en");
btnFrans.setVisible(false);
btnEngels.setVisible(false);
btnNederlands.setVisible(false);
btnMaaknieuweSpeler.setText(dc.getTaal().getText("accountMaken"));
btnMaaknieuweSpeler.setVisible(true);
btnMaakNieuweWedstrijd.setText(dc.getTaal().getText("startNieuwSpel"));
btnMaakNieuweWedstrijd.setVisible(true);
btnBestaandeWedstrijdLaden.setText(dc.getTaal().getText("laadWedstrijd"));
btnBestaandeWedstrijdLaden.setVisible(true);
}
@FXML
private void btnMenuLanguageFransAction(javafx.event.ActionEvent event) {
System.out.println("Clicked");
dc.setTaal("fr");
btnFrans.setVisible(false);
btnEngels.setVisible(false);
btnNederlands.setVisible(false);
btnMaaknieuweSpeler.setText(dc.getTaal().getText("accountMaken"));
btnMaaknieuweSpeler.setVisible(true);
btnMaakNieuweWedstrijd.setText(dc.getTaal().getText("startNieuwSpel"));
btnMaakNieuweWedstrijd.setVisible(true);
btnBestaandeWedstrijdLaden.setText(dc.getTaal().getText("laadWedstrijd"));
btnBestaandeWedstrijdLaden.setVisible(true);
}
}
这是我的fxml文件中的代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="406.0" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.MaakNieuweSpelerController">
<children>
<TextField fx:id="txtGebruikersnaam" layoutX="226.0" layoutY="62.0" />
<TextField fx:id="txtGeboortedatum" layoutX="226.0" layoutY="144.0" />
<Label fx:id="lblGebruikersnaam" layoutX="57.0" layoutY="66.0" text="Gebruikersnaam: " />
<Label fx:id="lblGeboortedatum" layoutX="57.0" layoutY="148.0" text="GeboorteDatum: " />
<Button fx:id="btnMaakNieuweSpeler" layoutX="57.0" layoutY="234.0" mnemonicParsing="false" onAction="#maakNieuweSpeler" text="Aanmaken" />
<Button fx:id="btnAnnuleren" layoutX="304.0" layoutY="234.0" mnemonicParsing="false" onAction="#Annuleren" text="Annuleren" />
</children>
</AnchorPane>
这就是我得到的错误
javafx.fxml.LoadException:
file:/C:/Users/jelle/Documents/NetBeansProjects/ProjectenI/pazaak-g16/dist/run1805194826/pazaak-g16.jar!/gui/MaakNieuweSpeler.fxml:8
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
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.load(FXMLLoader.java:2409)
at gui.TitelbeeldschermController.btnMaakNieuweSpelerOnAction(TitelbeeldschermController.java:65)
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:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException: gui.MaakNieuweSpelerController
at java.lang.Class.newInstance(Class.java:427)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 66 more
Caused by: java.lang.NoSuchMethodException: gui.MaakNieuweSpelerController.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 68 more
答案 0 :(得分:0)
FXMLLoader
的默认行为是通过调用该类的无参数构造函数,从fx:controller
属性中指定的类创建控制器。换句话说,由于您的FXML文件中有fx:controller="gui.MaakNieuweSpelerController"
,FXMLLoader
将有效地尝试
Object controller = new MaakNieuweSpelerController();
由于您的MaakNieuweSpelerController
类没有无参数构造函数,因此在尝试加载FXML文件时会出现错误。
最简单的解决方法是从fx:controller="gui.MaakNieuweSpelerController"
中删除MaakNieuweSpeler.fxml
属性,并使用Java代码设置控制器:
@FXML
public void btnMaakNieuweSpelerOnAction(javafx.event.ActionEvent event) throws IOException{
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MaakNieuweSpeler.fxml"));
// You didn't post the MaakNieuweSpelerController class, but I am guessing
// this is how you need to construct it. Just modify this to call
// a valid constructor if necessary:
MaakNieuweSpelerController controller = new MaakNieuweSpelerController(dc);
fxmlLoader.setController(controller);
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle(dc.getTaal().getText("nieuweSpeler"));
stage.setScene(new Scene(root1));
stage.show();
}catch(IOException ex){
ex.printStackTrace();
}
}