我只是面临一个问题,即使用JavaFX实现完整的MVC模型。 我在带有拆分窗格的fxml文件中创建了一个简单的结构。
这个想法是在左侧有一个树状菜单,在左侧有GUI元素来配置我的GUI设置。 为了填满右侧,我想在窗格中加载相应的fxml文件。 通过在Tree Items中实现的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>
&#13;
我想要调用的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>
&#13;
这是控制器文件,它使用第一个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
);
}
}
&#13;
我知道它不起作用。错误信息很明确:
引起: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>
&#13;
我想要调用的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>
&#13;
这是控制器文件,它使用第一个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
);
}
}
&#13;
我知道它不起作用。错误信息很明确:
引起:javafx.fxml.LoadException:已指定根值。
但我没有任何线索。建议?
答案 0 :(得分:0)
我这样做:
public class Task extends Pane {
TitledPane titledPane;
public Task() {
final FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "Task.fxml"));
titledPane = new TitledPane();
fxmlLoader.setRoot( titledPane);
fxmlLoader.setController( this);
try {
fxmlLoader.load();
} catch( IOException exception) {
throw new RuntimeException( exception);
}
getChildren().add( titledPane);
}
...
}
任务只是通过fxml创建的自定义TitledPane对象。我把几个这些Task对象放在一个场景上。
我不确定这是否是适用于JavaFX 8的正确方法,但它确实有效。