我只是面临一个问题,用 JavaFX 实现一个完整的 MVC 模型。我在带有拆分窗格的 fxml 文件中创建了一个简单的结构。
这个想法是在左侧有一个树形菜单,在左侧有一个 GUI 元素来配置我的 GUI 设置。为了填充右侧,我想在窗格中加载相应的 fxml 文件。通过在树项中实现的 mouseclickevents 触发。
问题是,我不知道必须在 controller.java 文件中使用哪些函数来加载 fxml 文件并使其在拆分窗格的右侧可见。
编辑:
这是我的代码:
这就是 FXML MainFrame:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?language javascript?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MenuItems.Menu_Item_Controller">
<children>
<SplitPane dividerPositions="0.2593984962406015" layoutY="-7.0" prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="7.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="-7.0">
<items>
<AnchorPane fx:id="Preferences_Left" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TreeView fx:id="Preference_Tree" layoutY="23.0" prefHeight="575.0" prefWidth="204.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="23.0">
<root>
<TreeItem value="Case Full Name">
<children>
<TreeItem value="Test1">
<children>
<TreeItem>
<value>
<Label fx:id="XML_Center_Fields" onMouseClicked="#XML_Center_Fields" prefWidth="180" text="Test" />
</value>
</TreeItem>
<TreeItem>
<value>
<CheckBox id="checkBox2" prefWidth="180.0" text="TWO" />
</value>
</TreeItem>
<TreeItem>
<value>
<CheckBox id="checkBox3" prefWidth="180.0" text="THREE" />
</value>
</TreeItem>
</children>
</TreeItem>
</children>
</TreeItem>
</root>
</TreeView>
</children>
</AnchorPane>
<AnchorPane fx:id="Preferences_Right" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<AnchorPane fx:id="XML_Center_Field" layoutX="64.0" layoutY="23.0" prefHeight="575.0" prefWidth="588.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="23.0">
</AnchorPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
这就是我要调用的 FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="218.0" prefWidth="239.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button layoutX="130.0" layoutY="122.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
这就是控制器文件,它使用来自第一个 FXML 的操作来调用另一个,目标是在拆分窗格的右侧将其可视化。
package MenuItems;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* Created by tkeller2 on 25.11.2014.
*/
public class Menu_Item_Controller extends Pane {
public AnchorPane Preferences_Left;
public TreeView Preference_Tree;
public AnchorPane Preferences_Right;
public AnchorPane XML_Center_Field;
TitledPane titledPane = new TitledPane();
public void XML_Center_Fields(Event event) throws Exception {
final FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "Test.fxml"));
fxmlLoader.setRoot(XML_Center_Field);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch( IOException exception) {
throw new RuntimeException( exception);
}
getChildren().add(XML_Center_Field
);
}
}
我知道它不起作用。错误信息很清楚:
原因:javafx.fxml.LoadException:已指定根值。
但我没有任何线索。建议?
感谢你的回答。我确信它指向正确的方向,但我无法让它运行。
请看一下我的代码。
这就是 FXML MainFrame:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?language javascript?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MenuItems.Menu_Item_Controller">
<children>
<SplitPane dividerPositions="0.2593984962406015" layoutY="-7.0" prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="7.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="-7.0">
<items>
<AnchorPane fx:id="Preferences_Left" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TreeView fx:id="Preference_Tree" layoutY="23.0" prefHeight="575.0" prefWidth="204.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="23.0">
<root>
<TreeItem value="Case Full Name">
<children>
<TreeItem value="Test1">
<children>
<TreeItem>
<value>
<Label fx:id="XML_Center_Fields" onMouseClicked="#XML_Center_Fields" prefWidth="180" text="Test" />
</value>
</TreeItem>
<TreeItem>
<value>
<CheckBox id="checkBox2" prefWidth="180.0" text="TWO" />
</value>
</TreeItem>
<TreeItem>
<value>
<CheckBox id="checkBox3" prefWidth="180.0" text="THREE" />
</value>
</TreeItem>
</children>
</TreeItem>
</children>
</TreeItem>
</root>
</TreeView>
</children>
</AnchorPane>
<AnchorPane fx:id="Preferences_Right" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<AnchorPane fx:id="XML_Center_Field" layoutX="64.0" layoutY="23.0" prefHeight="575.0" prefWidth="588.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="23.0">
</AnchorPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
这就是我要调用的 FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="218.0" prefWidth="239.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button layoutX="130.0" layoutY="122.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
这就是控制器文件,它使用来自第一个 FXML 的操作来调用另一个,目标是在拆分窗格的右侧将其可视化。
package MenuItems;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* Created by tkeller2 on 25.11.2014.
*/
public class Menu_Item_Controller extends Pane {
public AnchorPane Preferences_Left;
public TreeView Preference_Tree;
public AnchorPane Preferences_Right;
public AnchorPane XML_Center_Field;
TitledPane titledPane = new TitledPane();
public void XML_Center_Fields(Event event) throws Exception {
final FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "Test.fxml"));
fxmlLoader.setRoot(XML_Center_Field);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch( IOException exception) {
throw new RuntimeException( exception);
}
getChildren().add(XML_Center_Field
);
}
}
我知道它不起作用。错误信息很清楚:
原因:javafx.fxml.LoadException:已指定根值。
但我没有任何线索。建议?