当前位置: 首页 > 知识库问答 >
问题:

如何在JavaFX中交换控制器?InvocationTargetException

宫铭
2023-03-14
 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
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:8413)
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:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
Caused by: 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 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:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: javafx.fxml.LoadException: 
/D:/Projekty/PartyAnimal/bin/view/Rules.fxml:14

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:934)
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 controller.MenuController.showRules(MenuController.java:39)
... 58 more
Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.ReflectUtil can not access a                                                   member of class controller.RulesController with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.Class.newInstance(Class.java:436)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 66 more
public class Main extends Application {

@Override
public void start(Stage primaryStage) {
    try {   
        AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("../view/Menu.fxml"));
        Scene scene = new Scene(root,1280,720); 
        primaryStage.setTitle("Party Animal");
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) throws IOException {
    launch(args);
}
package controller;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;

public class MenuController implements Initializable {


String menu_theme;

public MenuController() {
}
@FXML
public void showRules(ActionEvent event) throws IOException {

FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/Rules.fxml"));
     Parent root = loader.load();
     Scene scene = new Scene(root);
     Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
     stage.setTitle("PartyAnimal");
     stage.setScene(scene);
     stage.show();

}}
package controller;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.media.Media;

public class RulesController {

RulesController() {
System.out.println("blablabababa");
}

}

FXML1-menu开始:

<AnchorPane prefHeight="720.0" prefWidth="1280.0" styleClass="pane" stylesheets="@Background.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MenuController">
    <children>
<Pane prefHeight="720.0" prefWidth="1280.0" styleClass="pane" stylesheets="@Background.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <children>
        <Slider id="volumeSlider" layoutX="1147.0" layoutY="681.0" prefHeight="14.0" prefWidth="100.0">
           <effect>

fxml2-rules的开始(第14行的错误是这里的第一行,在此之前,这里只是预先导入):

<AnchorPane prefHeight="720.0" prefWidth="1280.0" styleClass="pane" stylesheets="@Background.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.RulesController">
   <children>
      <Pane prefHeight="720.0" prefWidth="1280.0" styleClass="pane" stylesheets="@Background.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Slider id="volumeSlider" layoutX="1147.0" layoutY="681.0" prefHeight="14.0" prefWidth="100.0">
               <effect>

共有1个答案

蔚和风
2023-03-14

您的RulesController需要一个公共无参数构造函数

FXMLLoader将(默认情况下)通过反射调用指定类上的无参数构造函数来创建控制器。反射代码假定此构造函数是公共的。由于您从构造函数中省略了public关键字,所以FXMLLoader在尝试创建控制器时失败。

只是替换

RulesController() {
    System.out.println("blablabababa");
}
public RulesController() {
    System.out.println("blablabababa");
}
 类似资料:
  • 如果JavaFX项目中有3个文件;FXML文件、FXML控制器和应用程序类;控制器如何通过改变点击时的屏幕(通常使用)来响应按钮点击(工作非常好)?我没有对stage的引用(它被传递到应用程序类的)。 应用程序示例: FXML示例: 控制器示例:

  • 交换机与控制器交互 我们可以启动一个简单的控制器,默认没有任何流表项,仅仅作为一台带学习功能的交换机。控制器默认监听端口是 6633。 以下控制器与交换机之间的消息交互过程,可以通过 wireshark,配置 of 过滤器观察到交换机跟控制器之间的交互消息。 参见下面的表格。 消息 类型 描述 Hello Controller->Switch 跟着 TCP 握手,控制器发送它的版本号到交换机。 H

  • 问题内容: 使用SceneBuilder。我有2个阶段,每个阶段都有一个控制器: , 。 Stage1Controller: Stage2Controller: 这是使用2种方法 (称为in 方法)将这两个fxml文件加载到Main.java类中的方式: 该方法在第一阶段中用作方法,它在两个阶段都转换视图。 如何输入方法?谢谢 问题答案: “快速又脏”的方法是给的引用: 现在在您的主应用程序中:

  • 在我的javafx应用程序中,我们有两个FXML文件,第一个。FXML和second.FXML,相同的第一个Controller.java和secondController.java现在主要的问题是第一个。FXML包含TextField name和on Button当用户单击那个按钮时second.FXML显示在second.FXML中我有一个组合框和一个按钮当用户单击second.FXML按钮时

  • 我正在学习使用SceneBuilder创建一个新的JavaFX应用程序的教程,并尝试向.fxml文件添加一个controller类。 我正在使用Eclipse和Gluon的Scene Builder V8.1.1版本。 在哪里可以将我的.fxml文件连接到我的控制器Java类?

  • null 我在TopMenuButtons中有一个按钮,它具有openAssisstantStage()方法,打开新的小阶段: 对于这个阶段,我还有FXML文件和控制器(AssisstantController)。只有2个按钮-第一个按钮应该打开前一个阶段和关闭电流,但第二个按钮也应该打开普雷沃阶段,关闭电流,也打开一个特定的标签在我的标签视图和做一些其他逻辑例如。获取实际localDate: 我