我通过 Oracle 文档的帮助实现了 javaFX-Dialogs
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html
和教程
http://code.makery.ch/blog/javafx-dialogs-official/
到目前为止我已经做了以下事情:
我在对话框窗口中添加了时间线。
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();
}
现在对话框窗口在 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();
}
关于java - 超时后自动关闭对话框并取消响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32391905/