问题
How can I read and save the JavaFX GUI element hierarchy of my program inside a tree (including the main window, Stage
, Button
, TextBox
, etc.)?
回答1:
There's no automatism for what you want to achieve. The closest thing to loading is to use an fxml. But the problem is the saving. You can't generate an fxml from your scene nodes. Moreover the fxml is dynamic, it doesn't even have a schema.
For the saving you could
- use JAXB in order to generate a hierarchical XML (i. e. the fxml)
- implement the JAXB classes of the the node types and with the attributes which you need
- use an adapter class to simulate e. g. -INF to -Infinity that is required by fxml
- iterate through all the children in the scene recursively and create the same structure with JAXB classes
- save to fxml
and for the loading part you can use the standard fxml loader.
来源:https://stackoverflow.com/questions/35415240/javafx-read-and-save-gui-elements