将FXML加载到边框中心

时间:2015-02-01 22:44:39

标签: javafx center fxml borderpane

晚上好人,

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

请查看我的简单代码如下:

Main.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 {

}

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:4)

好的,

我终于做到了; - )

以下代码是我的一天:

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。

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

这将帮助其他人。 如果您想在边框窗格的中央加载fxm

$ awk -F, -v OFS=, '{a[$2]+=$3}END{asorti(a,b);for(i in b)print b[i], a[b[i]]}' file
Birmingham,300
London,700
Mumbai,150
Seoul,450
Tokyo,1230