在研究了很多文章并尝试了所有方法之后,我独自选择了使用Eclipse IDE和Java FX 2在标题栏上设置图标这样简单的任务。

它要么带有默认的空窗口图标,要么变回黑色。请让我知道我在做什么错。

这是我的一些尝试,

//Image ico = new Image(UI.class.getResourceAsStream("Sunset.jpg"), 16, 16,      true,true);
//Image ico = new Image("Sunset.jpg", true);// looks inside src folder
//primaryStage.getIcons().add(new Image(UI.class.getResourceAsStream("/title.jpeg")));
//primaryStage.getIcons().add(new Image(UI.class.getResourceAsStream("title.jpeg")));

Image ico = new Image(UI.class.getResourceAsStream("Sunset.jpg"));
primaryStage.getIcons().add(ico);


我已经用过的照片尝试过以下方法,


我使用的图片属性为300x300像素
我将300x300像素的图片转换为16x16像素
我同时使用* .ico和32x32和16x16像素
我将.ico转换为jpeg并尝试了。


请让我知道,我该如何克服这个问题。
谢谢 !

系统细节:

java.runtime.version-1.7.0_11-b21
javafx.runtime.version-2.2.4-b19
操作系统名称-MS Win XP Professional
操作系统版本-5.1.2600 Service Pack 3 Build 2600
操作系统架构-32位
显卡-英特尔®高清显卡
显卡驱动程序– igxpmp32.sys版本6.14.10.5384

最佳答案

以下对我有用:

Image image = new Image(<some valid image location here>);
stage.getIcons().setAll(image);


这是一个示例应用程序:

import static javafx.application.Application.launch;
import javafx.application.*;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class IconApp extends Application {
  @Override public void start(Stage stage) {
    Image image = new Image(
      "http://icons.iconarchive.com/icons/tooschee/misc/128/Present-icon.png"
    );
    stage.getIcons().setAll(image);

    final VBox layout = new VBox(10);
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
    layout.getChildren().setAll(new ImageView(image));

    stage.setScene(new Scene(layout));
    stage.show();
  }

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

// icon license (creative commons 3 with attribution):
//   http://creativecommons.org/licenses/by-nc/3.0/
// icon attribution:
//   http://tooschee.com/portfolio?worksCategory=icons


以及应用程序的输出(您可以在标题栏的左上角看到舞台图标):



该图标还会显示在操作系统任务栏中:



测试系统为Windows 7,Java 8b77。

10-05 18:28
查看更多