我正在使用javaFXML。我试图打开一个选项窗口点击登录按钮,但我得到javafx.fxml.LoadException。我给了整个代码,请帮助我。
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="345.0" prefWidth="361.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="qwertyy.FXMLDocumentController">
<stylesheets>
<URL value="@login.css" />
</stylesheets>
<children>
<AnchorPane layoutX="4.0" layoutY="-14.0" prefHeight="345.0" prefWidth="426.0">
<children>
<Label cache="true" cacheHint="QUALITY" depthTest="ENABLE" layoutX="66.0" layoutY="100.0" mnemonicParsing="true" prefHeight="32.0" prefWidth="111.0" text="Username">
<font>
<Font size="14.0" />
</font></Label>
<Label cache="true" cacheHint="QUALITY" depthTest="ENABLE" layoutX="67.0" layoutY="164.0" mnemonicParsing="true" prefHeight="32.0" prefWidth="73.0" text="Password">
<font>
<Font size="14.0" />
</font></Label>
<TextField id="usernm" fx:id="usernm" layoutX="140.0" layoutY="104.0" prefHeight="25.0" prefWidth="163.0" />
<PasswordField fx:id="pwwd" layoutX="140.0" layoutY="168.0" prefHeight="25.0" prefWidth="163.0" />
<Button layoutX="141.0" layoutY="231.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="25.0" prefWidth="73.0" text="Login" />
</children>
</AnchorPane>
</children>
</AnchorPane>
FXMLDocumentController.java
package qwertyy;
import java.awt.Dialog;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class FXMLDocumentController implements Initializable {
private Stage stage;
private Qwertyy appln;
public void setApp(Qwertyy qwer)
{
appln=qwer;
}
@FXML
private TextField usernm,pwwd;
@FXML
private void handleButtonAction(ActionEvent event) {
URL u;
URLConnection ucn;
OutputStream os;
byte[] sss;
String msg;
String usrnm,pwd,eusrnm,epwd,ousrnm;
ousrnm=usernm.getText();
usrnm=usernm.getText();
pwd=pwwd.getText();
msg="&usrnm="+usrnm+"&pwd="+pwd;
sss=msg.getBytes();
try{
u=new URL("http://localhost:8088/profutil/CheckLogin");
ucn=u.openConnection();
ucn.setDoOutput(true);
ucn.setDoInput(true);
ucn.setRequestProperty("Content=length", String.valueOf(sss.length));
os=ucn.getOutputStream();
os.write(sss);
os.flush();
String inp;
BufferedReader in=new BufferedReader(new InputStreamReader(ucn.getInputStream()));
inp=in.readLine();
System.out.println("input"+inp);
while(inp!=null)
{
if(inp.equals("3"))
{
System.out.println("in lab");
appln.swa();
}
if(inp.equals("1"))
{
}
else
{
}
inp=in.readLine();
}
os.close();
}catch(Exception ee)
{
// new Error(null, true, "Internet Connection Required");
ee.printStackTrace();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Labopt.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="353.0" prefWidth="430.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="qwertyy.LaboptController">
<children>
<ScrollPane layoutX="-1.0" layoutY="1.0" prefHeight="353.0" prefWidth="491.0">
<content>
<MenuBar prefHeight="25.0" prefWidth="492.0">
<menus>
<Menu mnemonicParsing="false" text="ViewAllOrders" />
<Menu mnemonicParsing="false" text="Preferences">
<items>
<MenuItem mnemonicParsing="false" text="Add" />
<MenuItem mnemonicParsing="false" text="Edit" />
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="ViewAllClients" />
<Menu mnemonicParsing="false" text="Messages">
<items>
<MenuItem fx:id="msg1" mnemonicParsing="false" onAction="#CompmsgAction" text="Compose" />
<MenuItem fx:id="msg2" mnemonicParsing="false" onAction="#InboxAction" text="Inbox" />
<MenuItem fx:id="msg3" mnemonicParsing="false" onAction="#SentAction" text="Sent" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Notes">
<items>
<MenuItem mnemonicParsing="false" text="Add Note" />
<MenuItem mnemonicParsing="false" text="View Note" />
</items>
</Menu>
</menus>
</MenuBar>
</content>
</ScrollPane>
LabOptController.java
package qwertyy;
import java.awt.MenuItem;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class LaboptController implements Initializable {
@FXML
private MenuItem msg1,msg2,msg3;
private Qwertyy application;
public void setApp(Qwertyy application){
this.application = application;
}
@FXML
private void CompmsgAction(ActionEvent event) throws IOException {
}
@FXML
private void InboxAction(ActionEvent event) {
}
@FXML
private void SentAction(ActionEvent event) {
}
@FXML
private void handleButtonAction(ActionEvent event) {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Qwertyy.java
package qwertyy;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Qwertyy extends Application {
private Stage stage;
private final double MINIMUM_WINDOW_WIDTH = 390.0;
private final double MINIMUM_WINDOW_HEIGHT = 500.0;
@Override
public void start(Stage primaryStage) throws Exception {
try {
stage = primaryStage;
stage.setTitle("FXML Login Sample");
stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
gotoLogin();
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(Qwertyy.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void gotoLogin() {
try {
FXMLDocumentController login = (FXMLDocumentController) replaceSceneContent("FXMLDocument.fxml");
login.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Qwertyy.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void swa()
{
gotoProfile();
}
private void gotoProfile() {
try {
LaboptController profile = (LaboptController) replaceSceneContent("Labopt.fxml");
profile.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Qwertyy.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Initializable replaceSceneContent(String fxml) throws Exception {
FXMLLoader loader = new FXMLLoader();
InputStream in = Qwertyy.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(Qwertyy.class.getResource(fxml));
AnchorPane page;
try {
page = (AnchorPane) loader.load(in);
} finally {
in.close();
}
Scene scene = new Scene(page, 800, 600);
stage.setScene(scene);
stage.sizeToScene();
return (Initializable) loader.getController();
}
}
生成异常
Aug 12, 2014 5:05:27 PM qwertyy.Qwertyy gotoProfile
SEVERE: null
javafx.fxml.LoadException:
file:/C:/Users/Administrator/Documents/NetBeansProjects/Qwertyy/dist/run779804382/Qwertyy.jar! /qwertyy/Labopt.fxml:26
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at qwertyy.Qwertyy.replaceSceneContent(Qwertyy.java:79)
at qwertyy.Qwertyy.gotoProfile(Qwertyy.java:65)
at qwertyy.Qwertyy.swa(Qwertyy.java:58)
at qwertyy.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
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:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1763)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651)
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( at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)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:204)
at javafx.scene.Node.fireEvent(Node.java:8175)
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:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.MenuItem field qwertyy.LaboptController.msg1 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:758)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:745)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2723)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 62 more
答案 0 :(得分:2)
正如异常中明确指出的那样,问题是班级LaboptController
包含msg1
类型的字段msg2
,msg3
,java.awt.MenuItem
。它应该是javafx.scene.control.MenuItem
。
只需更正导入。