运行背景Javafx的函数 [英] Running a function the background Javafx

查看:204
本文介绍了运行背景Javafx的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是java的新手,我为什么会发生这种情况而感到困惑,当我点击一个按钮时我创建的程序崩溃直到动作完成(SendPost();),问题在于函数发送一个post请求并解析大约10秒钟的响应,因此GUI崩溃不可用,直到SendPost()完成。我需要它以某种方式在后台运行,所以当我添加一个计时器时它不会一直崩溃。

I'm still pretty new to java and I am stuck for why this happens, when I click a button I have created the program crashes until the action is complete (SendPost();), the problem with this is that the function sends a post request and parses the response which takes around 10 seconds so the GUI crashes is unusable until the SendPost() has finished. I need it to run in the background somehow so it doesn't keep crashing when I add a timer.

这里是我的按钮代码

            EventHandler<ActionEvent> login = new EventHandler<ActionEvent>() { 
            @Override public void handle(ActionEvent event) { 
                SendPost();


            }       
        };


推荐答案

你的程序发生的事情就是打电话给你正在做的是阻止JavaFX线程正常工作。当发生这种情况时,您的界面会停止响应输入,使您的程序看起来像是挂起/崩溃。

What is happening to your program is that the call you are doing is blocking the JavaFX thread while it is working. When this happens your interface stops responding to input, making it seem like your program has hung/crashed.

正如已经评论过的那样,你可以简单地启动一个新的普通线程来做你需要什么,因为重要的部分是将工作转移到另一个线程,保持应用程序线程的响应。在这种情况下,您可以这样做:

As has been commented you could simply start a new plain Thread to do what you need, as the important part is moving the work to another thread, keeping the application thread responsive. In this case you could simply do it like this:

Thread th = new Thread(() -> {
    sendPost();
});
th.setDaemon(true);
th.start();

稍后,你可能想要查看Task类,它为后台提供了更多选项JavaFX中的任务非常令人愉快。以下是使用它的示例程序:

Later however, you might want to look into the Task class, which gives a lot more options for background tasks in JavaFX and is very pleasant to work with. Here is an example program using it:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.geometry.*;
import javafx.stage.*;
import javafx.event.*;
import javafx.concurrent.*;
import java.util.concurrent.*;

public class HelloTask extends Application {
    Button button;
    ProgressBar progress;

    @Override
    public void start(Stage stage) {
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));

        button = new Button("Click me!");
        button.setOnAction(this::handleClick);
        grid.add(button, 0, 0);

        progress = new ProgressBar();
        progress.setProgress(0);
        grid.add(progress, 0, 1);

        Scene scene = new Scene(grid);

        stage.setTitle("Hello Task!");
        stage.setScene(scene);
        stage.show();
    }

    ExecutorService executor = Executors.newCachedThreadPool();

    @Override
    public void stop() {
        executor.shutdown();
    }

    public void handleClick(ActionEvent event) {
        Task<String> task = new Task<String>() {
            @Override
            protected String call() {
                updateValue("Working...");

                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        return "Interrupted!";
                    }
                    updateProgress(i + 1, 10);
                }
                return "Done!";
            }
        };
        progress.progressProperty().bind(task.progressProperty());
        button.textProperty().bind(task.valueProperty());

        executor.submit(task);
    }
}

这篇关于运行背景Javafx的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆