所以我在第一次尝试编写一些东西时创建了一个小程序,我遇到了我的菜单栏的问题。 我用scenebuilder创建了所有东西,当我添加一个onAction事件时,我一直收到错误,我也找不到解决方案。
这是我的fxml代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<fx:root id="AnchorPane" prefHeight="400.0" prefWidth="600.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1">
<children>
<MenuBar fx:id="HoofdMenu" layoutY="2.0" prefHeight="25.0" prefWidth="600.0">
<menus>
<Menu mnemonicParsing="false" text="Maaltafels">
<items>
<MenuItem fx:id="maal10" mnemonicParsing="false" onAction="#maal10Selected" text="Tot maal 10" />
<MenuItem fx:id="maal100" mnemonicParsing="false" onAction="#maal100Clicked" text="Tot maal 100" />
<MenuItem mnemonicParsing="false" text="Tot maal 1000" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</fx:root>
这是我打开屏幕并执行操作的代码
package gui;
import domein.DomeinController;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class StartschermController extends AnchorPane{
private DomeinController dc;
@FXML
private MenuBar menuBar;
@FXML
private MenuItem maal10;
@FXML
private MenuItem maal100;
public StartschermController(DomeinController dc) throws MalformedURLException{
this.dc = dc;
String stringFxml = "file:///c:/Users/jelle/Documents/NetBeansProjects/ProjectenI/WiskundeOefFX/src/gui/StartScherm.fxml";
URL fxml = new URL(stringFxml);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(fxml);
loader.setRoot(this);
loader.setController(this);
try{
loader.load();
}catch(IOException ex){
throw new RuntimeException(ex);
}
}
@FXML
public void maal10Selected(javafx.event.ActionEvent event){
String stringoefeningen = "file:///c:/Users/jelle/Documents/NetBeansProjects/ProjectenI/WiskundeOefFX/src/gui/oefeningen.fxml";
try {
URL oefeningen = new URL(stringoefeningen);
FXMLLoader oefeningenLoader = new FXMLLoader();
OefeningenController controller = new OefeningenController(dc);
oefeningenLoader.setLocation(oefeningen);
oefeningenLoader.setController(controller);
Parent root = (Parent) oefeningenLoader.load();
Stage stage = new Stage();
stage.setTitle("Maaltafels");
stage.setScene(new Scene(root));
stage.show();
} catch (MalformedURLException ex) {
Logger.getLogger(StartschermController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(StartschermController.class.getName()).log(Level.SEVERE, null, ex);
}
dc.setMoeilijkheidsGraad(1);
}
这是我得到的错误列表
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:497)
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:497)
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$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: javafx.fxml.LoadException:
/c:/Users/jelle/Documents/NetBeansProjects/ProjectenI/WiskundeOefFX/src/gui/StartScherm.fxml:14
at gui.StartschermController.<init>(StartschermController.java:42)
at StartUp.StartUp.start(StartUp.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
... 1 more
Caused by: javafx.fxml.LoadException:
/c:/Users/jelle/Documents/NetBeansProjects/ProjectenI/WiskundeOefFX/src/gui/StartScherm.fxml:14
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.load(FXMLLoader.java:2409)
at gui.StartschermController.<init>(StartschermController.java:40)
... 10 more
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.MenuItem field gui.StartschermController.maal10 to javafx.scene.control.MenuItem
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)
... 13 more
Exception running application StartUp.StartUp
Java Result: 1
你们可以帮助我吗?