我正在尝试在根窗格上添加节点。如果我尝试在类外部调用方法,则根窗格上不会显示节点。但是,如果我在类中调用该方法,它就可以工作。
public class MainLayout extends AnchorPane{
@FXML public Pane root_pane;
public MainLayout(){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/resources/MainLayout.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try{
fxmlLoader.load();
}catch(IOException exception){
throw new RuntimeException(exception);
}
}
@FXML
private void initialize(){
ShapeEllipse ellipse = new ShapeEllipse();
ellipse.setText("Main");
Arrows arrow = new Arrows();
arrow.relocate(10,20);
root_pane.getChildren().addAll(ellipse,arrow);
}
public void insertShape(){
ShapeInputOutput inputOutput = new ShapeInputOutput();
inputOutput.relocate(10,50);
root_pane.getChildren().add(inputOutput);
}
这是我的第二个控制器类
public class AddShapes extends AnchorPane{
MainLayout main = new MainLayout();
ShapeInputOutput inputOutput1 = new ShapeInputOutput();
public AddShapes(){
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/resources/AddShapes.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try{
fxmlLoader.load();
}catch(Exception e){
e.printStackTrace();
}
addActions();
}
public void addActions(){
inputOutput1.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
main.insertShape();
}