将 FXML 加载到边框中心



晚上好伙计们,

我正在玩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>

第1页GuiController.java:

package application;
public class Page1GUIController {
}

非常感谢您的帮助!

fx:controller 属性需要完全限定的类名。 即在Page1GUI.fxml中,您需要fx:controller="application.Page1GUIController"

(注意:我没有阅读您的所有代码:可能还有其他错误。但这是您发布错误的原因。

好的伙计们,

我终于成功了;-)

以下代码让我很开心:

public void LoadDashboardFXML(ActionEvent event) throws IOException
{
    StackPaneMain.getChildren().clear();
    StackPaneMain.getChildren().add(FXMLLoader.load(getClass().getResource("Dashboard.fxml")));
    StackPaneMain.setLayoutX(0);
    StackPaneMain.setLayoutY(0);
}

我只是添加了

@FXML
StackPane StackPaneMain;

在 Main .java (fx:id) 的控制器类中。

现在,首先清除StackPane,然后加载fxml。

这将帮助其他人。如果要在边框窗格的中心加载 FXM

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("prepared.fxml"));
        borderPane.getChildren().remove(borderPane.getCenter()); //remove existing fxml from center.
        try {
            borderPane.setCenter(fxmlLoader.load());
        } catch (IOException e) {
            e.printStackTrace();
        }

最新更新