各位晚安,
我正在玩 JavaFX。目的是在单击按钮时将准备好的 fxml 加载到边框的中心元素中。不幸的是,我收到以下错误:
Caused by: javafx.fxml.LoadException:
/D:/Coden/Eclipse%20Workspace/Project%20Tango001/bin/application/Page1Gui.fxml:8
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at application.MainGUIController.LadeCenterNeu(MainGUIController.java:19)
... 57 more
Caused by: java.lang.ClassNotFoundException: Page1GUIController.java
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 71 more
请找到我的简单代码如下:
主.java:
//*** Main.java
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("MainGUI.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainGui.fxml:
//*** MainGui.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane id="maincontent" fx:id="maincontent" maxHeight="344.0" maxWidth="600.0" minHeight="344.0" minWidth="600.0" prefHeight="344.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainGUIController">
<top>
<Pane prefHeight="72.0" prefWidth="600.0" style="-fx-background-color: #00567F;" BorderPane.alignment="CENTER">
<children>
<Button layoutX="274.0" layoutY="24.0" mnemonicParsing="false" onAction="#LadeCenterNeu" text="Button" />
</children></Pane>
</top>
<left>
<Pane prefHeight="200.0" prefWidth="98.0" style="-fx-background-color: #33677F;" BorderPane.alignment="CENTER" />
</left>
<center>
<Pane id="MainGUICenter" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #4CC5FF;" BorderPane.alignment="CENTER">
<children>
<Label layoutX="169.0" layoutY="92.0" text="Initialer Inhalt" textAlignment="CENTER" />
</children>
</Pane>
</center>
<right>
<Pane prefHeight="200.0" prefWidth="92.0" style="-fx-background-color: #33677F;" BorderPane.alignment="CENTER" />
</right>
<bottom>
<Pane prefHeight="72.0" prefWidth="600.0" style="-fx-background-color: #00567F;" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
MainGuiController.java:
//*** MainGuiController.java
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;
public class MainGUIController {
@FXML
Pane maincontent;
@FXML
private void LadeCenterNeu(ActionEvent event) throws IOException {
maincontent.getChildren().clear();
maincontent.getChildren().add(FXMLLoader.load(getClass().getResource("Page1Gui.fxml")));
System.out.println("Load!");
}
}
Page1Gui.fxml:
//*** Page1Gui.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="425.0" prefWidth="545.0" style="-fx-background-color: #111111;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Page1GUIController.java">
<children>
<Pane layoutX="-169.0" layoutY="-176.0" prefHeight="200.0" prefWidth="200.0" />
<Label layoutX="259.0" layoutY="204.0" text="Inhalt 2" />
</children>
</AnchorPane>
Page1GuiController.java:
package application;
public class Page1GUIController {
}
非常感谢您的帮助!