简体   繁体   English

如何将按钮添加到另一个类

[英]How to add a button to another class

I have a class called Restaurant, that has an FXML file, in that class, I have a button that when pressed opens another window called tables, that also has an FXML file, I have a minimize button in tables window. 我有一个名为Restaurant的类,它具有一个FXML文件,在该类中,我有一个按钮,当按下该按钮时会打开另一个称为表的窗口,该窗口也有一个FXML文件,在表窗口中有一个最小化按钮。

What I want is when I press the minimize button in table, a new button will be added to the Restaurant window. 我想要的是,当我按表中的“最小化”按钮时,新按钮将添加到“餐厅”窗口中。

But I'm getting a null exception. 但是我得到了一个空异常。 Can someone help me solve this. 有人可以帮我解决这个问题吗?

This is the code for my minimize button: 这是我的最小化按钮的代码:

@FXML
public void minimiza(ActionEvent event) {
     Button Tables = new Button("Mesas");
    try {
        Stage mesa = (Stage) ((Button) event.getSource()).getScene().getWindow();
        mesa.setIconified(true);
        FXMLLoader loader = new FXMLLoader();
        RestauranteController controle = loader.getController();
        controle.adicionaBotao(Tables);
    } catch (Exception e) {
        System.out.println("Not working " + e.getMessage());
    }
}

It's probably better to just listen to the iconified property of the Stage from the Restaurant class. 最好只听从Restaurant类中Stageiconified属性。 If the minimized state does not match the iconified state, you could create this property in the controller for the tables window yourself. 如果最小化状态iconified 化状态不匹配,则可以在控制器中为tables窗口创建此属性。

private int windowCount;

@Override
public void start(Stage primaryStage) {

    Button btn = new Button("Show new Window");
    VBox buttonContainer = new VBox(btn);

    btn.setOnAction((ActionEvent event) -> {
        // create new window
        windowCount++;
        Label label = new Label("Window No " + windowCount);
        Stage stage = new Stage();
        Scene scene = new Scene(label);
        stage.setScene(scene);

        // button for restoring from main scene
        Button restoreButton = new Button("Restore Window " + windowCount);

        // restore window on button click
        restoreButton.setOnAction(evt -> {
            stage.setIconified(false);
        });

        // we rely on the minimize button of the stage here

        // listen to the iconified state to add/remove button form main window
        stage.iconifiedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue) {
                buttonContainer.getChildren().add(restoreButton);
            } else {
                buttonContainer.getChildren().remove(restoreButton);
            }
        });
        stage.show();
    });


    Scene scene = new Scene(buttonContainer, 200, 200);

    primaryStage.setScene(scene);
    primaryStage.show();
}

BTW: Note that without loading a fxml, FXMLLoader will never create a controller instance, let alone without the fxml being specified. 顺便说一句:请注意,不加载fxml, FXMLLoader将永远不会创建控制器实例,更不用说不指定fxml了。 Furthermore under no circumstances would it return the same controller instance as the one used with your Restaurant window. 此外,在任何情况下都不会返回与“ Restaurant窗口使用的控制器实例相同的控制器实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM