我在尝试关闭当前场景并在选择菜单项时打开另一个场景时遇到问题。我的主舞台编码如下:
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
Pane myPane = (Pane)FXMLLoader.load(getClass().getResource
("createProduct.fxml"));
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
然后在createProduct.fxml中,当菜单项点击时,它将执行以下操作:
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
stage.show();
}
但是,前面的面板createProduct. fxml并没有关闭。我知道有一个叫做阶段关闭()的东西可以做到这一点,但是我不知道在哪里实现它,因为我从一开始就没有从main right传递场景。我想知道我应该如何解决这个问题。
提前谢谢。
你必须在开始方法上做一些改变,比如..
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("createProduct.fxml"));
Pane myPane = (Pane)myLoader.load();
CreateProductController controller = (CreateProductController) myLoader.getController();
controller.setPrevStage(primaryStage);
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
并且您的CreateProductController.java将是,
public class CreateProductController implements Initializable {
Stage prevStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show();
}
}
你有多种方法可以解决你的问题,我告诉你其中很少。我从您的问题中了解到,您要创建一个顶部包含导航(菜单栏)的应用程序,并且通过使用它可以,用户可以切换到任何屏幕。
最简单的方法是使导航栏全局可用,方法是将其设置为静态或将其传递到屏幕,或者通过任何其他方法。其次,制作一个屏幕(例如边框窗格)并在其上加载其他屏幕(其中心)。
您还可以在project中使用spring integration,以便为您提供更好的控制器注入。见Ref:http://www . Zen Java . com/2011/10/25/views-within-views-controllers-within-controllers/
您还可以创建自己的单屏幕控制器并使用它管理其他屏幕。参见参考: https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1
但是对于这个(上面),您必须更改或更新您当前的架构。
您可以从fx:id
获取当前打开的窗口/阶段。只需确保您的createProduct.fxml中有一个<code>fx:id</code>的项,如下所示
<TextField fx:id="username" labelFloat="true" layoutX="80.0" layoutY="300.0" prefHeight="32.0" prefWidth="260.0" promptText="Login Username" />
希望你看到fx: id
属性。
然后在启动控制器中,保持原样
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
Pane myPane = (Pane)FXMLLoader.load(getClass().getResource("createProduct.fxml"));
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
然后在你的CreateProductController.java里
//make sure you import the javafx item you have used, eg have used TextField
import javafx.scene.control.TextField;
public class CreateProductController implements Initializable {
// make sure the variable has the same type as the item in the fxml "TextField" and same name "username"
@FXML
private TextField username;
//we use the above variable since its referencing the window to which it belongs because of the fx:id attribute, then we close it
private void closeStage()
{
((Stage) username.getScene().getWindow()).close();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
closeStage();// we close the old stage
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
stage.show();
}
}