我遵循了教程并创建了自己的自定义元素。这是一个AnchorPane,其中包含6个重叠的画布以创建图形框架。
下面是itudesGraph.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.AnchorPane?>
<fx:root type="javafx.scene.layout.AnchorPane" minHeight="200.0" minWidth="700.0" prefHeight="300.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Canvas fx:id="backgroundCanvas" height="300.0" width="900.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Canvas fx:id="series1Canvas" height="300.0" width="900.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Canvas fx:id="series2Canvas" height="300.0" width="900.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Canvas fx:id="series3Canvas" height="300.0" layoutX="7.0" width="900.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Canvas fx:id="series4Canvas" height="300.0" width="900.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Canvas fx:id="savedSeriesCanvas" height="300.0" layoutY="-1.0" width="900.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</fx:root>
相当简单。然后,我有一个加载此框架的自定义控制器,称为幅值图形控制器
public class magnitudeGraphController extends AnchorPane implements Initializable {
@FXML
public Canvas backgroundCanvas;
@FXML
public Canvas series1Canvas;
@FXML
public Canvas series2Canvas;
@FXML
public Canvas series3Canvas;
@FXML
public Canvas series4Canvas;
@FXML
public Canvas savedSeriesCanvas;
public magnitudeGraphController(){
System.out.println("Graph Initialised");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("magnitudeGraph.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
又好又容易。因此,在我的主应用程序控制器上,我可以调用一个graphs实例,控制器将加载fxml文件,其根元素为anchorPane。
这在主应用程序中通过以下方法调用:
public magnitudeGraphController spectrumgraph = new magnitudeGraphController();
现在,为我的主应用程序编写fxml时会遇到困难。我为控制器编写了导入文件:
<?import myApp.magnitudeGraphController?>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<magnitudeGraphController fx:id="spectrumgraph" layoutX="38.0" layoutY="33.0" minHeight="200.0" minWidth="700.0" prefHeight="300.0" prefWidth="944.0">
</AnchorPane>
我得到的是fxml编辑器上的错误,指出“ Class javafx.scene.layout.AnchorPane”不支持属性MagnitudeGraphController。
让我感到困惑的是,它运行得很好。我所做的只是将fxml文件和控制器的名称更改为一个更可识别的名称,并重构了该文件以反映这一点。我已经查看了备份备份的工作时间,除了文件名之外,找不到任何区别。如果尝试编译该错误,则在尝试加载幅值GraphController时会抛出classnotfoundexception。 我已经把头发扯了好多年了,无法找到使整个事情崩溃的原因。