嗨,我有问题打开新窗口fxml视图后单击按钮。主窗口显示,在主视图中我有六个按钮。我想我点击按钮打开新窗口。但是,当我单击按钮时,显示异常:
java.lang.Reflect.InvocationTargetException
我尝试在internet上找到的不同解决方案,但显示此异常。主要是尝试在切换函数中修改代码,负责显示新视图。我认为问题出在这里或者是在主类中的代码。
Main class:
public class Main extends Application {
public void start(Stage stage) throws Exception {
AnchorPane pane = FXMLLoader.load(getClass().getResource("/mainView.fxml"));
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
@Override
public void init() {
System.out.println("Init");
}
@Override
public void stop() {
System.out.println("Stop");
}
public static void main(String[] args) {
launch(args);
}
}
MainController.java
public class MainController {
@FXML
private Button addChangeMainButton;
@FXML
private Button viewChangesButton;
@FXML
private Button addChangeBoughtButton;
@FXML
private Button viewContractsButton;
@FXML
private Button addContractButton;
@FXML
private Button viewStatisticsButton;
@FXML
void chooseViewAction(ActionEvent event) throws IOException {
Button button = (Button) event.getSource();
String buttonId = button.getId();
MainLogic mainl = new MainLogic();
mainl.chooseView(buttonId);
}
}
public class MainLogic {
private String chooseView;
public void chooseView(String chooseView) throws IOException {
switch (chooseView) {
case "addChangeMainButton":
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("addChangeVIEW.fxml"));
Parent root1 = fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
break;
case "viewChangesButton":
System.out.println("viewChangesButton");
break;
case "addChangeBoughtButton":
System.out.println("addChangeBoughtButton");
break;
case "viewContractsButton":
System.out.println("viewContractsButton");
break;
case "addContractButton":
System.out.println("addContractButton");
break;
case "viewStatisticsButton":
System.out.println("viewStatisticsButton");
break;
default:
System.out.println("default");
break;
}
}
}
mainView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.controller.MainController">
<children>
<Button fx:id="addChangeMainButton" layoutX="31.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Add Change" />
<Button fx:id="viewChangesButton" layoutX="31.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Changes" />
<Button fx:id="addChangeBoughtButton" layoutX="217.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Change Bought/Sold" />
<Button fx:id="viewContractsButton" layoutX="225.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Contracts" />
<Button fx:id="addContractButton" layoutX="403.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Add Contract" />
<Button fx:id="viewStatisticsButton" layoutX="403.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Statistics" />
</children>
</AnchorPane>
addChangeVIEW.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.controller.addChangeController">
<children>
<Button fx:id="button" layoutX="263.0" layoutY="210.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
addChangeController.java
package com.controller;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class addChangeController {
@FXML
private Button button;
}
module-info.java
module ProjectCPApril {
requires javafx.graphics;
requires javafx.fxml;
requires javafx.controls;
exports com.controller to javafx.graphics;
opens com.controller to javafx.fxml;
exports com.logic to javafx.graphics;
opens com.logic to javafx.fxml;
}
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
... 47 more
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at ProjectCPApril/com.logic.MainLogic.chooseView(MainLogic.java:27)
at ProjectCPApril/com.controller.MainController.chooseViewAction(MainController.java:43)
... 58 more
Stop
Process finished with exit code 0
将注释中的信息复制到答案中
堆栈跟踪的重要部分是:
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at ProjectCPApril/com.logic.MainLogic.chooseView(MainLogic.java:27)
at ProjectCPApril/com.controller.MainController.chooseViewAction(MainController.java:43)
... 58 more
这将告诉您,当您调用load
时,FXMLLoader
的位置为null
。通过class.getResource
进行设置时,这意味着所述方法返回null
。当getResource
返回null
时,这意味着无法找到具有给定路径的资源。
您当前使用“AddChangeView”
作为路径,但您说:
addchangeview.fxml
位于文件夹resources
中,mainlogic.java
位于包com.logic
中
由于您的路径没有前导的/
,所以它是相对于您调用GetResource
的类
的。由于您使用了mainlogic.class
,因此路径将解析为com/logic/addchangeview.fxml
-这不是您拥有资源的地方。您已经将资源放在类路径的根目录中,可以使用绝对路径“/addchangeview.fxml”
检索该路径。
有关详细信息,请参阅class.getResource(String)
。
我目前正在使用JavaFX和JavaFX Scene Builder开发一些GUI。我遇到了一个问题,要为它找到解决方案并不容易(至少对我来说)。我想得到这样的东西: http://i.imgur.com/oN4mIVl.png 关键是单击TreeItem“Sample”后,只有窗口的右侧会重新加载。我也想使用fxml文件来完成它。我知道我可以在另一个fxml中包含一个fxml,但这不是解决方案。
问题内容: 我遇到的情况是,单击按钮打开带有搜索结果的新浏览器窗口。 有什么方法可以连接并聚焦到新打开的浏览器窗口吗? 并使用它,然后返回到原始(第一个)窗口。 问题答案: 您可以在以下窗口之间切换:
问题内容: 我遇到的情况是,单击按钮打开带有搜索结果的新浏览器窗口。 有什么方法可以连接并聚焦到新打开的浏览器窗口吗? 并使用它,然后返回到原始(第一个)窗口。 问题答案: 你可以在以下窗口之间切换:
在我的应用程序中,点击提交按钮(Struts),执行一个操作类方法,该方法设置bean的“更新网址”属性。我想要的是我需要在设置bean完成后在新浏览器中打开的网址,而用户不必做任何其他事情,比如单击链接。我找不到做这件事的正确方法。 下面是一些代码: 我的JSP:
作为我程序的一部分,我需要有一个按钮,当用户单击它时,它会打开一个新窗口。 嗯,我想我应该有一个类来创建框架并按按钮调用它。但我不知道该怎么开始。我刚刚在程序中找到了我的按钮,但它不起作用。有人能告诉我怎么做吗?或对其进行编码。