JavaFX - ScheduledService(衔接Dialog)

实现案例:弹出的dialog在十秒之后自动关闭,采用的方法是ScheduledService

package sample;


import javafx.application.Application;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.ValueAxis;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

import java.util.concurrent.Delayed;


public class Main extends Application {


    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {


        Button b1 = new Button("显示Dialog");
        Button b2 = new Button("b2");
        Button b3 = new Button("b3");
        Button b4 = new Button("b4");
        Button b5 = new Button("b5");
        Button b6 = new Button("b6");
        Button b7 = new Button("b7");
        Button b8 = new Button("b8");

        AnchorPane ap = new AnchorPane();
        ap.setStyle("-fx-background-color: #3c6ab8");
        ap.getChildren().addAll(b1);
        ap.setTopAnchor(b1, 100.0);
        ap.setLeftAnchor(b1, 100.0);

        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                DialogPane dlg = new DialogPane();//对话框,点击是否的小框框
                dlg.setStyle("-fx-background-color: #07f379");
                dlg.setHeaderText("计时十秒钟之后关闭本Dialog");//对话框上显示的头文字
                dlg.setContentText("你知道旁边的图代表什么吗");//对话框上显示的正文文字
                dlg.getChildren().addAll(b2, b3);///无效
                dlg.getButtonTypes().add(ButtonType.CLOSE);//有效
                dlg.getButtonTypes().add(ButtonType.APPLY);//有效
                dlg.getButtonTypes().add(ButtonType.CANCEL);//有效

                //设置按钮事件
                Button apply = (Button) dlg.lookupButton(ButtonType.APPLY);
                apply.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        System.out.println("应用");
                    }
                });
                Button CLOSE = (Button) dlg.lookupButton(ButtonType.CLOSE);
                CLOSE.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        System.out.println("关闭");
                    }
                });

//                右上角图标::

                ImageView iv = new ImageView("icon/huochezhan.png");

                dlg.setGraphic(iv);

//                详细信息::

                dlg.setExpandableContent(new Text("这是一条扩展内容"));


                Scene sc = new Scene(dlg);


                Stage stg = new Stage();
                stg.setScene(sc);
                //模态化
                stg.initOwner(primaryStage);
                stg.initStyle(StageStyle.UTILITY);
                stg.initModality(Modality.WINDOW_MODAL);//不关闭本界面无法对别的界面进行操作。
                stg.setAlwaysOnTop(true);//窗口总显示在最上方
                stg.setTitle("Dialog窗口");
                stg.setResizable(false);//对话框的大小禁止改变
                stg.show();//显示一个新窗口


                 MyscheduleServices my = new MyscheduleServices(dlg,stg );
                 my.setDelay(Duration.millis(0));//立即执行
                my.setPeriod(Duration.millis(1000));//一秒一次
                 my.start();

            }
        });


        Scene scene = new Scene(ap);


        primaryStage.setScene(scene);
        primaryStage.setTitle("Java FX Lesson23.ScheduledService ");///对话框、、提示框
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();


    }


}


class MyscheduleServices extends ScheduledService<Integer> {
    private DialogPane dlg = null;
    private Stage stage = null;
    int i = 0;

    public MyscheduleServices(DialogPane dlg, Stage stage){

        this.dlg = dlg;
        this.stage = stage;

    }

    @Override
    protected Task<Integer> createTask() {//创建task方法


        return new Task<Integer>() {//实现了task一个方法


            @Override
            protected Integer call() throws Exception {//call不会更新UI
                System.out.println("call" + Thread.currentThread().getName());
                return i = i+1;//这里的i值传递给下面的updatevalue
            }

            @Override
            protected void updateValue(Integer value) {//这里会更新ui线程
                System.out.println("updateValue" + Thread.currentThread().getName());
                System.out.println("updateValue的值::"+value);
                if(value <11){
                    MyscheduleServices.this.dlg.setContentText(String.valueOf(value));
                }
                else {
                    MyscheduleServices.this.cancel();
                    MyscheduleServices.this.stage.close();

                }

            }
        };
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值