我试图从一个单独的控制器内的一个控制器类访问一个方法。我正在尝试访问的方法setPiece()
用于将图像绘制到窗格上,并位于控制器GoBoardController
中。我试图从Tutorial_selectController
访问此方法的控制器。
在Tutorial_selectController中,我在StonePlaceTutorial()方法中调用setPiece()
方法。我认为通过GoBoardController对象调用setPiece()
方法会起作用,但只要在控制器内调用RuntimeException
方法,我就会得到InvocationTargetException
和setPiece()
。
GoBoardController.java
public class GoBoardController implements Initializable {
@FXML
private ImageView goBoardImageView;
@FXML
private Button boardBackButton;
@FXML
private Button boardBackTutorial;
@FXML
private AnchorPane boardPane;
private GoBoard goBoard;
FXMLLoader loader;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
//Creating a new GoBoard object on initilization of the controller
goBoard = new GoBoard(boardPane);
}
//Method I'm trying to access
public void setPiece(double x, double y, String p){
if(p == "b"){
goBoard.setStoneBlack(x, y);
}
else if(p == "w"){
goBoard.setStoneWhite(x, y);
}
}
}
Tutorial_selectController.java
public class Tutorial_selectController implements Initializable {
@FXML
private Button quitButton;
@FXML
private Button mainMenuButton;
@FXML
private Button stonePlaceButton;
private Go_Tutorial_Lessons lessons;
private Stage lessonStage;
private Parent root;
private Scene lessonScene;
private GoBoardController goBoardController;
//x and y values will be used to pre-set the board for specific tutorials
private final int x1 = 349;
private final int y1 = 99;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
lessons = new Go_Tutorial_Lessons();
try{
root = FXMLLoader.load(Go.class.getResource("/GoFXML/GoBoard.fxml"));
}catch(Exception e){
e.printStackTrace();
}
lessonScene = new Scene(root);
}
//Where I'm attempting to access the setPiece() method
@FXML
private void StonePlaceTutorial(ActionEvent event) throws IOException {
lessonStage = (Stage)mainMenuButton.getScene().getWindow();
lessonStage.setScene(lessonScene);
goBoardController.setPiece(x1, y1, "b"); //doesn't work, why.
lessons.L1_Stone_Placement();
}
}
如何正确访问setPiece()
方法,以便在屏幕上绘制所需内容?