我正在开发一个javaFX Programm(由scenebuilder构建)并且由于是javafx的新手而遇到了一些大问题。
我有一个带有菜单栏的主窗口。在这个菜单栏我可以选择" NewSystems"添加新系统。选择它,会出现一个新窗口(带有单独控制器的独立fxml文件),其中包含一些Textfields和Textareas,以及一些按钮。
我希望它的方式是这样的: 用户在特定的文本字段中输入名称,ID等。然后单击按钮"添加"我想将每个Textfield保存在不同的String中。
这是mainController。点击" NewSystems"使用下面的handleNewSystem方法。窗户开得很好。
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
public class MainWindowController {
@FXML
private void handleNewSystem(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("/application/SystemData.fxml"));
Parent root = fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setOpacity(1);
stage.setTitle("New System");
stage.setScene(new Scene(root, 300, 600));
stage.showAndWait();
}
}
SystemDataController:
@FXML
public AnchorPane SystemData;
public TextField ID;
public TextField SysName;
public TextField SysCustomer;
public TextField SysProjectlabel;
public TextArea SysDescription;
public TextArea SysComment;
public TextField SysTechnology;
public Button addSystem;
@FXML
private void handleAddSystem(ActionEvent event) throws IOException {
String string = ID.getText();
System.out.println(string);
}
使用以下行:
<TextField fx:id="ID" onAction="#handleAddSystem"
GridPane.columnIndex="1"/>
我设法打印出ID Textfield中写的没有问题。
现在,当按钮&#34;添加&#34;时,我试图获取所有文本字段值。被压了。因此我删除了&#34; onAction =&#34;#handleAddSystem&#34;在上面的代码上,并将其添加到按钮:
<Button fx:id="addSystem" onAction="#handleAddSystem" layoutX="14.0"
layoutY="561.0" mnemonicParsing="false" onAction="#handleAddSystem"
prefHeight="25.0" prefWidth="52.0" text="Add " />
导致很多错误我无法解决。甚至不包含按钮和文本字段的窗口(单击&#34; NewSystem&#34;在主窗口通常会选择它)
我收到了以下错误代码:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException:
java.lang.reflect.InvocationTargetException
Caused by: java.lang.reflect.InvocationTargetException
Caused by: javafx.fxml.LoadException:
/C:/Users/UserName/git/Team/GUI/bin/application/SystemData.fxml:63
SystemData.fxml中的第63行是我在上面发布的那个:
<Button fx:id="addSystem" onAction="#handleAddSystem" layoutX="14.0"
layoutY="561.0" mnemonicParsing="false" onAction="#handleAddSystem"
prefHeight="25.0" prefWidth="52.0" text="Add " />
我很期待你的帮助,如果问题很长,我很抱歉。
完整的堆栈跟踪:
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.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:58)
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.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer
.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer
.lambda$createChildren$343(ContextMenuContent.java:1358)
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.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(GlassView
EventHandler.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(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 43 more
Caused by: javafx.fxml.LoadException:
/C:/Users/Mahir/git/Team23/GUI/bin/application/SystemData.fxml:63
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at
application.MainWindowController.handleNewSystem
(MainWindowController.java:17)
... 53 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:
[63,194]
Message: http://www.w3.org/TR/1999/REC-xml-names-
19990114#AttributeNotUnique?Button&onAction
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown
Source)
at javax.xml.stream.util.StreamReaderDelegate.next(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
... 56 more