Can';不要在JavaFX中使用从ParameterController传递到GameController的初始化(GameController)方法中的对象
我在JavaFX上遇到了一个问题。我正在做一个游戏。 在GameController中,我正在实例化一个由5X5按钮组成的网格,我希望在此之前获得游戏的参数。例如,符号X或O和对手(如果他是人类或机器人)Can';不要在JavaFX中使用从ParameterController传递到GameController的初始化(GameController)方法中的对象,java,javafx,Java,Javafx,我在JavaFX上遇到了一个问题。我正在做一个游戏。 在GameController中,我正在实例化一个由5X5按钮组成的网格,我希望在此之前获得游戏的参数。例如,符号X或O和对手(如果他是人类或机器人) package Controlleur; import java.net.URL; import java.util.ResourceBundle; import Model.Parametre; import javafx.event.ActionEvent; import javafx
package Controlleur;
import java.net.URL;
import java.util.ResourceBundle;
import Model.Parametre;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import Model.Grille;
public class GameControlleur implements Initializable {
private Parametre gameParametre ;
private Grille g = Grille.getInstance() ; ;
@FXML
private GridPane GrilleV ;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//当我在这里使用GameParameter对象时,我得到了一个错误
initgrill()
}
提前感谢您,我非常感谢您在
fxmloader
的load
方法返回之前为解决此问题提供的任何帮助或建议。在此之前获取控制器的唯一方法是自己构造控制器,并在加载之前使用setController
或设置cotrollerfactory。但我不确定这是否能解决问题,因为您还没有提到代码中到底出了什么问题。您没有发布Model.grill
。顺便说一句:按照惯例,包名以小写字母开头;这使得告诉包结束点和类名开始点更容易识别…initialize
在fxmloader
的load
方法返回之前运行。在此之前获取控制器的唯一方法是自己构造控制器,并在加载之前使用setController
或设置cotrollerfactory。但我不确定这是否能解决问题,因为您还没有提到代码中到底出了什么问题。您没有发布Model.grill
。顺便说一句:按照惯例,包名以小写字母开头;这使得告诉包的结束点和类名的开始点更容易识别。。。
}
public void initParametre(Parametre g) {
gameParametre = new Parametre(g.getAdversaire(),g.getSymbole() ) ;
}
public void initGrille() {
for(int i = 0 ; i < 5 ; i++) {
for(int j = 0 ; j < 5 ; j++) {
if((i > 0 && i < 4) && (j > 0 && j < 4)) {
addButton(i,j,true);
}else{
addButton(i,j,false);
}
}
}
g.initGrille();
}
private void addButton(int x , int y , boolean disabled) {
final Button btn = new Button();
btn.setMinSize(68, 75);
btn.setText(" ");
if(disabled == true) {
btn.setDisable(true);
}
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
btn.setText("X");
}
});
GrilleV.add(btn, x, y);
}
}
package Controlleur;
import java.io.IOException;
import Model.Parametre;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
public class ParametreControlleur {
@FXML
private RadioButton RdBtnRobot ;
@FXML
private RadioButton RdBtnHumain ;
@FXML
private RadioButton SymboleX ;
@FXML
private RadioButton SymboleO ;
@FXML
private Button Quitter ;
//default choice
private String selectedAdversaire = "Robot";
private String selectedSymbole = "X" ;
//getting Adversaire
public void getSelectedAdversaire(ActionEvent e) {
if(RdBtnRobot.isSelected()) {
selectedAdversaire = RdBtnRobot.getText() ;
}
if (RdBtnHumain.isSelected()){
selectedAdversaire = RdBtnHumain.getText() ;
}
}
//getting Symbol
public void getSelectedSymbole(ActionEvent e) {
if(SymboleX.isSelected()) {
selectedSymbole = SymboleX.getText() ;
}
if(SymboleO.isSelected()){
selectedSymbole = SymboleO.getText() ;
}
}
@FXML
public void onCommencer(ActionEvent e) throws IOException {
//Les parametres de jeu => type d'adversaire choisi ainsi que le Symbole
Parametre p = new Parametre(selectedSymbole , selectedAdversaire) ;
FXMLLoader fxmlLoader = new FXMLLoader() ;
fxmlLoader.setLocation(getClass().getResource("../Vue/game.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
//this is the object that i pass to Gameontroller and i want to retrieve in initialize method
GameControlleur controller = fxmlLoader.getController() ;
controller.initParametre(p);
Stage stage = new Stage();
stage.setResizable(false);
stage.setTitle("Quixo");
stage.setScene(new Scene(root1));
stage.show();
((Node)(e.getSource())).getScene().getWindow().hide();
}