[英]How to handle a ActionEvent from another class
I'm using JavaFX and working on a Schoolproject right now. 我正在使用JavaFX并正在进行Schoolproject。
About my program, i have a login screen and if i press on login, i come to the mainframe. 关于我的程序,我有一个登录屏幕,如果我按登录,我就会进入大型机。 The content area of the mainframe is empty, because i load the content for the AnchorPane that builds the mainframe when i use the buttons in the menubar. 大型机的内容区域为空,因为当我使用菜单栏中的按钮时,我会加载用于构建大型机的AnchorPane的内容。
When i press the login button, i want to laod the mainframe and the home pane in it. 当我按下登录按钮时,我想在其中装载大型机和主页窗格。 But i don't know how to load the pane into the mainframe from another class. 但是我不知道如何从另一个类将窗格加载到大型机中。
Here my code: 这是我的代码:
Code to load the mainframe: 加载大型机的代码:
@FXML
protected void login(ActionEvent event) throws IOException, URISyntaxException {
Stage stage;
Parent root = null;
stage = (Stage) btnLogin.getScene().getWindow();
try{
root = FXMLLoader.load(getClass().getResource("view/mainframe.fxml"));
} catch (IOException e){
e.printStackTrace();
}
Scene scene = new Scene(root, 1280, 900);
stage.setScene(scene);
stage.show();
}
Code to laod the home pane into the mainframe: 将主页窗格放入大型机的代码:
@FXML
protected void mHome(ActionEvent event) throws IOException, URISyntaxException {
try {
URL url = getClass().getResource("view/home.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(url);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream());
aContent.getChildren().clear();
aContent.getChildren().add(page);
}
catch (IOException e) {
e.printStackTrace();
}
}
These two methods are in different classes. 这两种方法位于不同的类中。
If you need some more information, please let me know :) 如果您需要更多信息,请告诉我:)
Thank you and greets, Timo 谢谢你,蒂莫
Just invoke the mHome()
method. 只需调用mHome()
方法。 Note that, since you never use the ActionEvent
, you can omit it from the method signature (the FXMLLoader
will still be able to map it as an event handler). 请注意,由于您从不使用ActionEvent
,因此可以从方法签名中将其忽略( FXMLLoader
仍然可以将其映射为事件处理程序)。 So change it to 因此将其更改为
@FXML
protected void mHome() throws IOException, URISyntaxException {
try {
URL url = getClass().getResource("view/home.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(url);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream());
aContent.getChildren().clear();
aContent.getChildren().add(page);
}
catch (IOException e) {
e.printStackTrace();
}
}
If you always want to show the "home pane" when the main pane is first shown, just add a call to mHome
to the initialize()
method in the same controller class: 如果始终要在首次显示主窗格时显示“主窗格”,只需mHome
的调用添加到同一控制器类中的initialize()
方法中:
public void initialize() {
// existing code...
mHome();
}
Alternatively, if you specifically only want to show the "home pane" when you load it from the login pane, add a call to mHome
in your login handler: 另外,如果您特别想在从登录窗格中加载“主页”时显示它,请在登录处理程序中添加对mHome
的调用:
@FXML
protected void login(ActionEvent event) throws IOException, URISyntaxException {
Stage stage;
Parent root = null;
stage = (Stage) btnLogin.getScene().getWindow();
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("view/mainframe.fxml"));
root = loader.load();
MainController controller = loader.getController();
controller.mHome();
} catch (Exception e){
e.printStackTrace();
}
Scene scene = new Scene(root, 1280, 900);
stage.setScene(scene);
stage.show();
}
(I'm assuming MainController
is the name of the controller class for mainframe.fxml
; obviously just adjust as needed.) (我假设MainController
是mainframe.fxml
的控制器类的名称;显然只是根据需要进行调整。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.