Java 超时后带有取消响应的自动关闭对话框

Java 超时后带有取消响应的自动关闭对话框,java,timer,javafx,javafx-8,Java,Timer,Javafx,Javafx 8,我借助Oracle文档实现了javaFX对话框 和教程 到目前为止,我已经做了以下工作: 我在对话框窗口中添加了时间线 public static void idleness(final DialogTemplate template) { System.out.println("timeline Started -" + Calendar.getInstance().getTime()); Timeline idlestage = new Timeline(new KeyF

我借助Oracle文档实现了javaFX对话框

和教程

到目前为止,我已经做了以下工作:
我在对话框窗口中添加了时间线

public static void idleness(final DialogTemplate template) {
    System.out.println("timeline Started -" + Calendar.getInstance().getTime());
    Timeline idlestage = new Timeline(new KeyFrame(Duration.seconds(15), new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            template.hide();
        }
    }));
    idlestage.setCycleCount(1);
    idlestage.play();

}
公共静态无效空闲(最终对话框模板){
System.out.println(“时间线已启动-”+Calendar.getInstance().getTime());
Timeline idlestage=new Timeline(新的关键帧(Duration.seconds(15),new EventHandler()){
@凌驾
公共无效句柄(ActionEvent事件){
template.hide();
}
}));
idlestate.setCycleCount(1);
Idlestate.play();
}
现在对话框窗口在15秒后隐藏,但无法获得任何对话框响应

期望:
如果用户在给定的时间内没有给出任何响应,则必须记录否定响应,并关闭对话框窗口。

直接设置结果是有效的

@Override
public void start( Stage stage )
{
    Scene scene = new Scene( new Group(), 200, 300 );

    Alert alert = new Alert( Alert.AlertType.CONFIRMATION );
    alert.setTitle( "Confirmation Dialog" );
    alert.setHeaderText( "Look, a Confirmation Dialog" );
    alert.setContentText( "Are you ok with this?" );

    System.out.println( "timeline Started -" + Calendar.getInstance().getTime() );
    Timeline idlestage = new Timeline( new KeyFrame( Duration.seconds(5 ), new EventHandler<ActionEvent>()
    {

        @Override
        public void handle( ActionEvent event )
        {
            alert.setResult(ButtonType.CANCEL);
            alert.hide();
        }
    } ) );
    idlestage.setCycleCount( 1 );
    idlestage.play();

    Optional<ButtonType> result = alert.showAndWait();

    if ( result.get() == ButtonType.OK )
    {
        System.out.println( "ok clicked" );
    }
    else if ( result.get() == ButtonType.CANCEL)
    {
        System.out.println( "cancel clicked" );
    }

    stage.setScene( scene );
    stage.show();
}
@覆盖
公众假期开始(阶段)
{
场景=新场景(新组(),200,300);
警报警报=新警报(Alert.AlertType.CONFIRMATION);
alert.setTitle(“确认对话框”);
setHeaderText(“查看,确认对话框”);
setContentText(“你同意吗?”);
System.out.println(“时间线已启动-”+Calendar.getInstance().getTime());
Timeline idlestage=new Timeline(新的关键帧(Duration.seconds(5),new EventHandler())
{
@凌驾
公共无效句柄(ActionEvent事件)
{
alert.setResult(ButtonType.CANCEL);
alert.hide();
}
} ) );
idlestate.setCycleCount(1);
Idlestate.play();
可选结果=alert.showAndWait();
if(result.get()==ButtonType.OK)
{
System.out.println(“单击确定”);
}
else if(result.get()==ButtonType.CANCEL)
{
System.out.println(“单击取消”);
}
舞台场景;
stage.show();
}

您可以编辑问题,然后更正。同时纠正“拨号键”。链接您所遵循的教程。什么是DialogTemplate?你说的“负面反应”是什么意思?什么是DialogTemplate?它是什么样子的?它应该在自动关闭时给出取消按钮的响应。与其隐藏对话框,不如从对话框或
对话框模板
中获取取消按钮,不管是什么,然后单击它。