Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法通过操作事件函数关闭javafx中的阶段_Java_User Interface_Javafx 8_Stage - Fatal编程技术网

无法通过操作事件函数关闭javafx中的阶段

无法通过操作事件函数关闭javafx中的阶段,java,user-interface,javafx-8,stage,Java,User Interface,Javafx 8,Stage,我是JavaFx的初学者,我在简单地关闭一个阶段时遇到了一个问题。基本上,我正在尝试创建一个类,它是一个确认框控制器。有一个“是”按钮和一个“否”按钮,但无论哪种方式,阶段都应关闭并继续应用程序: import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; // more imports... public class ConfirmBoxController implements IView { public javafx

我是JavaFx的初学者,我在简单地关闭一个阶段时遇到了一个问题。基本上,我正在尝试创建一个类,它是一个确认框控制器。有一个“是”按钮和一个“否”按钮,但无论哪种方式,阶段都应关闭并继续应用程序:

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
// more imports...

public class ConfirmBoxController implements IView {

public javafx.scene.control.Button BTN_save;
public javafx.scene.control.Label label_Message;

private Stage stage;
private boolean answer;
private String title;
private String message;

public ConfirmBoxController(){
    this("Confirmation","Are you sure?");
}

public ConfirmBoxController(String title, String message){

    this.title = title;
    this.message = message;
    stage = new Stage();
}

public boolean confirm(){
    try{
        stage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader();
        Parent root = fxmlLoader.load(getClass().getResource("ConfirmBox.fxml").openStream());
        Scene scene = new Scene(root, 250, 140);
        stage.setTitle(title);
        stage.setScene(scene);
        stage.showAndWait();

        return answer;
    }
    catch(Exception E){
        E.printStackTrace();
        return true;
    }
}

public void yes(ActionEvent actionEvent) {
    answer = true;
    stage.close();
}

public void no(ActionEvent actionEvent) {
    answer = false;
    stage.close();
}
注意:IView是一个空接口。 基本上,舞台出现了,我可以点击按钮,我知道这是注册的,因为当我点击按钮时,功能“是”和“否”会打印出来。但什么也没发生


我确信我在监督一些基本的事情,但我还没有找到正确的方法。谢谢。

在控制器类中为按钮命名: @FXML 公共按钮关闭按钮

并添加如下方法:

@FXML
public void handleCloseButtonAction(ActionEvent event) {
    Stage stage = (Stage) closeButton.getScene().getWindow();
    stage.close();
}
在FXML中,需要引用按钮名称和调用onAction的方法:

<Button fx:id="closeButton" cancelButton="true" layoutX="350.0" layoutY="767.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" prefWidth="100.0" text="Close" />

谢谢,效果很好。但我真的不明白为什么。另外,您知道为什么函数“yes”和“no”不会更改“answer”的值吗?i、 e确认始终返回默认值“应答”。