zoukankan      html  css  js  c++  java
  • javafx progressbar

    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.concurrent.Task;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Pos;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.ProgressBar;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    public class Main extends Application {
        Task copyWorker;
        public static void main(String[] args) {
            Application.launch(args);
        }
        @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("Background Processes");
            Group root = new Group();
            Scene scene = new Scene(root, 330, 120, Color.WHITE);
    
            BorderPane mainPane = new BorderPane();
            root.getChildren().add(mainPane);
    
            final Label label = new Label("Files Transfer:");
            final ProgressBar progressBar = new ProgressBar(0);
    
            final HBox hb = new HBox();
            hb.setSpacing(5);
            hb.setAlignment(Pos.CENTER);
            hb.getChildren().addAll(label, progressBar);
            mainPane.setTop(hb);
    
            final Button startButton = new Button("Start");
            final Button cancelButton = new Button("Cancel");
            final HBox hb2 = new HBox();
            hb2.setSpacing(5);
            hb2.setAlignment(Pos.CENTER);
            hb2.getChildren().addAll(startButton, cancelButton);
            mainPane.setBottom(hb2);
    
            startButton.setOnAction(new EventHandler<ActionEvent>() {
    
                public void handle(ActionEvent event) {
                    startButton.setDisable(true);
                    progressBar.setProgress(0);
                    cancelButton.setDisable(false);
                    copyWorker = createWorker();
    
                    progressBar.progressProperty().unbind();
                    progressBar.progressProperty().bind(copyWorker.progressProperty());
                   
                    copyWorker.messageProperty().addListener(new ChangeListener<String>() {
                        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                            System.out.println(newValue);
                        }
                    });
    
                    new Thread(copyWorker).start();
                }
            });
            cancelButton.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
                    startButton.setDisable(false);
                    cancelButton.setDisable(true);
                    copyWorker.cancel(true);
                    progressBar.progressProperty().unbind();
                    progressBar.setProgress(0);
                    System.out.println("cancelled.");
                }
            });
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public Task createWorker() {
            return new Task() {
                @Override
                protected Object call() throws Exception {
                    for (int i = 0; i < 10; i++) {
                        Thread.sleep(2000);
                        updateMessage("2000 milliseconds");
                        updateProgress(i + 1, 10);
                    }
                    return true;
                }
            };
        }
    }
  • 相关阅读:
    SpringBoot中Word转PDF
    使用Word模板导出标准表Word样式文件
    Chrome 80及以上版本 中 Iframe 跨域 Cookie 的 Samesite 问题
    Visual Studio 2022 Key
    《HelloGitHub》第 69 期
    重玩 40 年前的经典游戏小蜜蜂,这次通关了源码
    那些年的开源项目,你跑起来了吗?
    图片处理看这篇就完了「GitHub 热点速览 v.21.48」
    年底巩固下 CS 知识「GitHub 热点速览 v.21.49」
    误入 GitHub 游戏区,意外地收获颇丰
  • 原文地址:https://www.cnblogs.com/rojas/p/4724347.html
Copyright © 2011-2022 走看看