[英]How to access a variable from one class controller file to another in JavaFX?
So I have an integer number called "size" saved to a controller class called SettingsStageController.java and I want that variable to be accessed through my other controller class file called GameStageController.java but I can't seem to find out how.所以我有一个名为“size”的整数保存到一个名为SettingsStageController.java的控制器类中,我希望通过我的另一个名为GameStageController.java的控制器类文件访问该变量,但我似乎无法找出如何。
SettingsStageController.java SettingsStageController.java
/* has the int size variable stored in this file */
int size = 5;
public void startGame(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("gameStage.fxml"));
root = loader.load();
stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); // ti ston poutso
scene = new Scene(root);
stage.setTitle("DnB: " + Integer.toString(size) + "x" + Integer.toString(size) + " Game");
stage.setScene(scene);
stage.show();
GameStageController gameStageController = loader.getController();
gameStageController.showPane();
}
GameStageController.java GameStageController.java
public class GameStageController implements Initializable {
@FXML
Text testText;
@FXML
AnchorPane twoXtwoPane;
@FXML
AnchorPane threeXthreePane;
@FXML
AnchorPane fourXfourPane;
@FXML
AnchorPane fiveXfivePane;
public void showPane() {
switch (/* I WANT TO PUT THE "SIZE" NUMBER HERE" */) {
case 2:
twoXtwoPane.setDisable(false);
twoXtwoPane.setVisible(true);
break;
case 3:
threeXthreePane.setDisable(false);
threeXthreePane.setVisible(true);
break;
case 4:
fourXfourPane.setDisable(false);
fourXfourPane.setVisible(true);
break;
case 5:
fiveXfivePane.setDisable(false);
fiveXfivePane.setVisible(true);
break;
default:
twoXtwoPane.setDisable(false);
twoXtwoPane.setVisible(true);
break;
}
}
}
If a method needs data to perform its functionality, then that data should be a parameter to the method.如果一个方法需要数据来执行其功能,那么该数据应该是该方法的参数。 You should do:
你应该做:
public class GameStageController implements Initializable {
// ...
public void showPane(int size) {
switch (size) {
// ...
}
}
}
and then of course然后当然
private int size = 5;
public void startGame(ActionEvent event) throws IOException {
// ...
GameStageController gameStageController = loader.getController();
gameStageController.showPane(size);
}
If your GameStageController
instance needs the size
variable later on, you can create an instance variable in that class, and set it in the showPane
method to the value passed as the parameter.如果您的
GameStageController
实例稍后需要size
变量,您可以在该类中创建一个实例变量,并在showPane
方法中将其设置为作为参数传递的值。
You just need to make it static so you can access it using the class name directly and if your controllers are in different packages you need to add public because by default the visibility is package.您只需将其设为静态,以便可以直接使用类名访问它,如果您的控制器位于不同的包中,则需要添加 public,因为默认情况下可见性是包。
So you have to declare size like this :所以你必须像这样声明大小:
public static int size = 5;
To access it you do :要访问它,您可以:
SettingsStageController.size
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.