java - 进度更新较晚

标签 java javafx javafx-2 javafx-8

您好,我正在尝试让进度条在进程发生时更新。复制文件。我也输出到控制台,但我的进度条在完成该过程之前不会更新。

我做错了什么?这是我的代码,它是一个文件。

package tvconvertversion3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.control.ProgressBar;

/**
 *
 * @author brett
 */
public class CreateWorkFile extends Application {

    private final File fileIn = new File("C:\\Users\\brett\\Documents\\Humans Need Not Apply-7Pq-S557XQU.mp4");
    private Task task;
    private File fileOut;
    private ProgressBar mpgb;

    @Override
    public void start(Stage primaryStage) {
        mpgb = new ProgressBar();
        mpgb.setVisible(true);

        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
                Thread th = new Thread(task);
                th.setDaemon(true);
                th.start();
            }
        });
        task = new Task<Integer>() {
            @Override
            protected Integer call() throws Exception {
                fileOut = new File("C:\\Users\\brett\\Documents\\ConvertedTvFiles\\Tenacious D - 39 HQ-86LT83IFDfQ.mp4");
                try {
                    FileInputStream fin;
                    long length = fileIn.length();
                    long counter = 0;
                    int r;
                    byte[] b = new byte[1024];
                    fin = new FileInputStream(fileIn);
                    FileOutputStream fout = new FileOutputStream(fileOut);
                    while ((r = fin.read(b)) != -1) {
                        mpgb.setProgress(100 * counter / length);
                        counter += r;
                        System.out.println(1.0 * counter / length);
                        fout.write(b, 0, r);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(CreateWorkFile.class.getName()).log(Level.SEVERE, null, ex);
                }
                return null;

            }
        };

        StackPane root = new StackPane();
        StackPane.setAlignment(mpgb, Pos.BOTTOM_CENTER);
        root.getChildren().addAll(btn, mpgb);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

任何建议都会很棒

最佳答案

您正在 FX 线程中完成主要工作,因此您会阻塞 UI。

将复制代码放入一个Task中并让它在一个线程中运行。看看TaskConcurrency文档。

关于java - 进度更新较晚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28321591/

相关文章:

java - 在浏览器上实现视频聊天的最佳方式

Java J2ee 插件安装存在依赖错误

Java:如何修复挂起的线程?

Java - 如何编写包含给定集合变量析取的正则表达式

java - FXML 文件中的 getHostServices().showDocument()

java - 在 JavaFX 的节点的子列表中查找特定节点

menu - 在 JavaFX 中制作垂直菜单栏

java - Spring boot JPA,带有 @EmbeddedId 的实体 - findyById 方法不起作用

java - 模块从 java.xml 和 stax.api 读取包

java - 如何在javafx中重复同一场景中的节点