我需要创建许多不同的FXML文件,并且每个文件都具有一致的布局。每个都有一个AnchorPane,它将保持单独的内容。
有没有办法加载'基地' FXML文件,然后加载第二个FXML文件,并将该数据路由到第一个?
例如,FXML#1有一个BorderPane。 FXML#2有一个按钮,texfield,标签等。如何加载#1,然后加载#2作为#1的孩子?
答案 0 :(得分:2)
您可以使用<fx:root>
元素来添加现有元素。
您需要一种方法来获取应该用作根元素的节点的引用,并在加载第二个fxml时将其传递给FXMLLoader
。你可以,例如使用命名空间使用fx:id
属性获取该元素:
@Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader outerLoader = new FXMLLoader(getClass().getResource("outer.fxml"));
Scene scene = new Scene(outerLoader.load());
URL inner = getClass().getResource("inner1.fxml");
// URL inner = getClass().getResource("inner2.fxml");
FXMLLoader innerLoader = new FXMLLoader(inner);
// get insertion point from outer fxml
innerLoader.setRoot(outerLoader.getNamespace().get("insertionPoint"));
innerLoader.load();
primaryStage.setScene(scene);
primaryStage.show();
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1">
<children>
<BorderPane AnchorPane.topAnchor="10" fx:id="insertionPoint"/>
</children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root type="javafx.scene.layout.BorderPane" xmlns:fx="http://javafx.com/fxml/1">
<center>
<Label text="Hello from inner 1."/>
</center>
</fx:root>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root type="javafx.scene.layout.BorderPane" xmlns:fx="http://javafx.com/fxml/1">
<center>
<Label text="Greetings from inner 2."/>
</center>
</fx:root>
答案 1 :(得分:0)
或者您实际上可以包括这样的模板文件
Traceback (most recent call last):
File "/Users/edamame/my_project/extract_appointments.py", line 16, in <module>
db_cfg = yaml.safe_load(infile)
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/__init__.py", line 94, in safe_load
return load(stream, SafeLoader)
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/__init__.py", line 72, in load
return loader.get_single_data()
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/constructor.py", line 35, in get_single_data
node = self.get_single_node()
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/composer.py", line 36, in get_single_node
document = self.compose_document()
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/composer.py", line 55, in compose_document
node = self.compose_node(None, None)
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/composer.py", line 84, in compose_node
node = self.compose_mapping_node(anchor)
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/composer.py", line 127, in compose_mapping_node
while not self.check_event(MappingEndEvent):
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/parser.py", line 98, in check_event
self.current_event = self.state()
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/parser.py", line 428, in parse_block_mapping_key
if self.check_token(KeyToken):
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/scanner.py", line 116, in check_token
self.fetch_more_tokens()
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/scanner.py", line 220, in fetch_more_tokens
return self.fetch_value()
File "/Users/edamame/my_project/venv/lib/python3.6/site-packages/yaml/scanner.py", line 576, in fetch_value
self.get_mark())
yaml.scanner.ScannerError: mapping values are not allowed here
in "/Users/edamame/my_logins/my_config.yaml", line 5, column 9