JavaFX-需要帮助设置初始化阶段吗
当我试图在初始化时将stage设置为变量时,我不断得到一个JavaFX-需要帮助设置初始化阶段吗,java,javafx,stage,Java,Javafx,Stage,当我试图在初始化时将stage设置为变量时,我不断得到一个null引用异常 我的密码 public class ListController implements Initializable { private Stage thisStage; @Override public void initialize(URL location, ResourceBundle resources) { lblCount.setTex
null
引用异常
我的密码
public class ListController implements Initializable {
private Stage thisStage;
@Override
public void initialize(URL location, ResourceBundle resources) {
lblCount.setText("Hey"); //This works
thisStage = (Stage) lblCount.getScene().getWindow(); //this doesn't
}
public ListController()
{
thisStage = (Stage) lblCount.getScene().getWindow(); //this doesn't
}
}
我尝试使用构造函数,但这也不起作用。如果我们在Stage类中找到数据类型并相应地使用此类初始化,则可以解决此空引用 比如说,
Stage myStage = null ; // this will raise a null pointer reference exception the possible solution can be to say:
Stage myStage = {" ", 0, ' ', etc } ; // as long as the class Stage has data members like String, int, char, etc
获取阶段引用的一种方法是在控制器上创建一个方法并进行设置:
FXMLLoader loader = new FXMLLoader();
Parent node = loader.load(getClass().getResource("view.fxml").openStream());
Scene scene = new Scene(node);
Stage stage = new Stage();
Controller controller = (Controller)loader.getController();
controller.setPrimaryStage(stage);
stage.show();
然后在您的方法中有一个有效的引用。当您调用stage时,您可以通过如下方法传递stage引用
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(YourClass.class.getResource("yourpaht.fxml"));
//here you change AnchorPane for your root Node
rootLayout = (AnchorPane) loader.load();
// Show scene.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
// Da acceso al programa principal.
ListController controller = loader.getController();
//method for passing the stage to the controller
controller.controlerPassing(this, primaryStage);
// Show stage,
primaryStage.show();
public class ListController implements Initializable {
private Stage stage;
pivate YourClass classs;
public controlerPassing(YourClass classs, Stage stage){
this.stage = stage;
this.classs = classs;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
lblCount.setText("Hey"); //This works
//now you can do whatever you want whit the stage
stage. do stuff........
}
}
现在controller类看起来可以像这样
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(YourClass.class.getResource("yourpaht.fxml"));
//here you change AnchorPane for your root Node
rootLayout = (AnchorPane) loader.load();
// Show scene.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
// Da acceso al programa principal.
ListController controller = loader.getController();
//method for passing the stage to the controller
controller.controlerPassing(this, primaryStage);
// Show stage,
primaryStage.show();
public class ListController implements Initializable {
private Stage stage;
pivate YourClass classs;
public controlerPassing(YourClass classs, Stage stage){
this.stage = stage;
this.classs = classs;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
lblCount.setText("Hey"); //This works
//now you can do whatever you want whit the stage
stage. do stuff........
}
}
lblCount在哪里初始化?@BenWin要解决的第一个问题是空引用问题。在调用控制器的
initialize()
方法时,FXML文件表示的UI尚未添加到场景中。因此,lblCount().getScene()
在这里计算为null
,得到一个NullPointerException
。你到底想对窗口做什么?我只是想对实际的舞台/窗口做一个参考。因此,如果我想操纵舞台,我可以这样做。thistage.hide()或thistage.bringtofront()之类的。我不想每次想与舞台互动时都必须执行lblCont.getScene().getWindow().hide()。有什么建议吗?