【笔记】javafx_弹窗_多任务

28 篇文章 0 订阅
//弹窗dialogPane
//更好的Dialog
import javafx.application.Application;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
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.util.Duration;


public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button("点击显示窗口");
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color:#00CED1");
        an.getChildren().addAll(button);
        AnchorPane.setTopAnchor(button,100.0);
        AnchorPane.setLeftAnchor(button,100.0);
//        an.setPadding(new Insets(100));
//        button.setOnAction(action -> System.out.println(button.getText()));
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                DialogPane dialogPane = new DialogPane();
                //新窗口
                Stage stage = new Stage();

                Scene sc = new Scene(dialogPane);
                stage.setScene(sc);
                //标题文字
                dialogPane.setHeaderText("从一到十");
                //内容文字
                dialogPane.setContentText("内容文字");

                ImageView imageView = new ImageView("icon/icon.png");
                dialogPane.setGraphic(imageView);
                dialogPane.setStyle("-fx-background-color:#7CCD7C");

                //扩展内容
                dialogPane.setExpandableContent(new Text("扩展的内容"));
                //添加组件
                dialogPane.getButtonTypes().add(ButtonType.APPLY);
                dialogPane.getButtonTypes().add(ButtonType.CLOSE);
                Button apply = (Button)dialogPane.lookupButton(ButtonType.APPLY);
                Button close = (Button)dialogPane.lookupButton(ButtonType.CLOSE);

                apply.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        System.out.println(apply.getText());
                    }
                });
                close.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        System.out.println(close.getText());
                    }
                });
                //不允许改变大小
                stage.setResizable(false);


                stage.setTitle("这是弹出的窗口");
                stage.initOwner(primaryStage);
//                stage.initStyle(StageStyle.UTILITY);//去掉装饰UNDECORATED
                stage.initModality(Modality.WINDOW_MODAL);//模态化
                stage.show();

                myScheduledService my = new myScheduledService(dialogPane,stage);
                my.setDelay(Duration.seconds(0));
                my.setPeriod(Duration.seconds(1));
                my.start();
            }
        });
        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX");
        primaryStage.setWidth(500);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}

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

    myScheduledService(DialogPane dialogPane, Stage stage){
        this.dialogPane = dialogPane;
        this.stage = stage;
    }
    @Override
    protected Task<Integer> createTask() {
        return new Task<Integer>() {
            @Override
            protected Integer call() throws Exception {
                return i = i + 1;
            }

            @Override
            protected void updateValue(Integer value) {
                if(value <= 10){
                    myScheduledService.this.dialogPane.setContentText(String.valueOf(value));
                }
                else{
                    stage.close();
                    myScheduledService.this.cancel();
                    System.out.println("!");
                }
            }
        };
    }
}

在这里插入图片描述
参考: https://space.bilibili.com/5096022/video?tid=36&page=8&keyword=&order=pubdate

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值