【问题标题】:Why my Stage.close is not Working为什么我的 Stage.close 不工作
【发布时间】:2016-09-15 04:35:45
【问题描述】:

Stage.close() 对我不起作用。

我检查过: JavaFX 2.0: Closing a stage (window)

这是我的代码:

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.stage.Modality;

public class MsgBox {

    public Stage MessageBox(String Title, String Message){

        VBox Pnl = new VBox();
        Pnl.setPadding(new Insets(10,10,10,10));
        Pnl.setSpacing(10);
        Pnl.setAlignment(Pos.CENTER);

            Label LblMsg = new Label(Message);
            Button CmdOK = new Button("OK");

        Pnl.getChildren().addAll(LblMsg, CmdOK);

        Scene SCN = new Scene(Pnl);

        Stage Window = new Stage();
            Window.initModality(Modality.APPLICATION_MODAL);
            Window.setTitle(Title);
            Window.setScene(SCN);
            Window.showAndWait();


        CmdOK.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent ev){
                Window.close();
            }
        });

        return Window;
    }
}

下面是调用消息框类的代码:

CmdUpdate.setOnAction(new EventHandler<ActionEvent>(){
        public void handle(ActionEvent ev){
            new MsgBox().MessageBox("Hello", "Hello World");

        }
 });

【问题讨论】:

  • 仅供参考,但不要在 Java 中使用 Camel Case。您的变量应以小写字母开头。类名以大写开头,但不是变量。
  • 谢谢 ManoDestra...

标签: javafx stage


【解决方案1】:

调用Stage#showAndWait 会等到舞台关闭后再返回,所以实际上下一行永远没有机会运行。

移动线

Window.showAndWait();

成为方法中的最后一个(至关重要 - 在您设置处理程序以允许关闭阶段之后),或者只需使用Stage#show,您的问题应该得到解决。

【讨论】:

    猜你喜欢
    • 2014-05-17
    • 2017-09-20
    • 2012-04-14
    • 2012-02-14
    • 2012-08-10
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多