[英]JAVAFX Scene builder multiple controllers
我用多个问题折磨了谷歌,但仍然没能获得一点。
我想用SceneBuilder填充的2个FXML场景来构建应用程序。 每个都有自己的控制器。 我使用ControlledScreen交换场景,这可行。 但是后来我无法改变任何场景。 例如:我在场景控制器中有标签:
public class ControllerForm implements ControlledScreen, Initializable {
ScreensController myController;
GraphicsContext GC;
@FXML
private Label fitnessLabel;
public void updateFitnessLabel(double data) {
fitnessLabel.setText(String.valueOf(data));
}
public void initialize(URL location, ResourceBundle resources) {
this.GC = neuroCanvas.getGraphicsContext2D();
}
在FXML文件中将其定义为:
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="684.0" prefWidth="918.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="general.ControllerForm">
<left>
<VBox prefHeight="400.0" prefWidth="189.0" BorderPane.alignment="CENTER">
<children>
<Label fx:id="fitnessLabel" text="Label">
<graphic>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Fitness:" />
</graphic>
</Label>
更新标签的功能是updateFitnessLabel。
在初始化方法中,我使用GC cuz,在此场景上有画布,想在其上绘制。
我有要求此功能的方法为:
public class Net {
private ControllerForm controller;
public void drawStart() throws IOException {
FXMLLoader loader = new FXMLLoader(
getClass().getResource(
Main.ScreenFile
)
);
BorderPane cv = loader.load();
// load actual screen
ControllerForm controller =
loader.<ControllerForm>getController();
controller.updateFitnessLabel(12);}
但是没有效果。 标签不会被更新。
我想念什么?
我尝试使用时间轴,但仍然没有效果。 如何在这个场景上实际画些东西?
好的,我找到了解决方案。
loader.load返回Object。 但是我需要初始对象而不是新对象。 使用此答案-FXMLLoader getController返回NULL吗?
这不是最佳选择,但是我不知道如何获得初始对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.