java - 如何将信息从 java 发送到 windows 任务栏到 javafx

标签 java windows javafx progress-bar

比如当您从 Chrome 下载内容时,任务栏图标显示如下:

windows taskbar

我想像那样在任务栏中显示我的程序的进度。您如何使用 javaFX 执行此操作...?

最佳答案

希望这样对你有帮助

package so;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXExample extends Application {

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

    private static final int ICONSIZE = 16;

    ProgressBar bar = new ProgressBar(0.2d);

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("change taskbar icon");
        Button btn = new Button();
        btn.setText("click me several times while looking at task bar");

        bar.setMaxWidth(ICONSIZE);

        btn.setOnAction(ev -> {

            bar.setProgress(bar.getProgress() + 0.1d);

            WritableImage image = new WritableImage(ICONSIZE, ICONSIZE);

            bar.snapshot(new SnapshotParameters(), image);

            File file;
            try {
                file = File.createTempFile("temp-image", ".png");
            } catch (IOException e1) {
                e1.printStackTrace();
                return;
            }

            try {
                ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }

            InputStream is;
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return;
            }

            primaryStage.getIcons().clear();
            primaryStage.getIcons().add(new Image(is));

            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        });

        VBox box = new VBox(bar, btn);
        primaryStage.setScene(new Scene(box, 300, 250));
        primaryStage.show();

    }
}

关于java - 如何将信息从 java 发送到 windows 任务栏到 javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192528/

相关文章:

java - 最欣赏的传输静态值的方式?

c++ - 我如何(有没有办法)将 HRESULT 转换为系统特定的错误消息?

windows - vbscript 中的 CommonAppData

php - x64 PHP/MYSQL 构建是否支持 64 位 Windows 服务器上的完整 64 位时间戳

JavaFX 不同类型列表的bindContent双向

java - 我无法在 Eclipse 中配置 JavaFx SDK 路径

javafx - 按键53次后声音停止工作

java - 如何在 Spring Boot 应用程序中排除给定运行时配置文件中的包

java - IntentService onCreate() 已调用但 onHandleIntent() 未调用

java - 如何将字符串转换为bigInteger并存储在哈希集中?