193

JavaFXを使用してアプリケーションアイコンを変更することは可能ですか、それともSwingを使用して変更する必要がありますか?

4

17 に答える 17

274

ステージが「ステージ」であり、ファイルがファイルシステム上にあると仮定します。

stage.getIcons().add(new Image("file:icon.png"));

以下のコメントによると、それが含まれている瓶に包まれている場合は、代わりに次のアプローチを使用する必要があります。

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("icon.png")));
于 2012-04-12T11:08:40.320 に答える
82

私はこれを試しましたが、完全に機能します。コードは次のとおりです。

stage.getIcons().add(
   new Image(
      <yourclassname>.class.getResourceAsStream( "icon.png" ))); 

icon.pngは、ソースファイルと同じフォルダーの下にあります。

于 2012-10-10T21:10:40.823 に答える
77

初心者のための完全なプログラム:)このプログラムはStackOverflowIconのアイコンを設定します。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class StackoverflowIcon extends Application {

    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();
        // set icon
        stage.getIcons().add(new Image("/path/to/stackoverflow.jpg"));
        stage.setTitle("Wow!! Stackoverflow Icon");
        stage.setScene(new Scene(root, 300, 250));
        stage.show();
    }

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

出力Screnshot

JavaFXスクリーンショット

JavaFX8用に更新

コードを変更する必要はありません。それでも問題なく動作します。Java 1.8(1.8.0_45)でテストおよび検証されています。パスはローカルまたはリモートに設定できます。両方がサポートされています。

stage.getIcons().add(new Image("/path/to/javaicon.png"));

また

stage.getIcons().add(new Image("https://example.com/javaicon.png"));

ここに画像の説明を入力してください

それが役に立てば幸い。ありがとう!!

于 2013-03-04T16:30:50.833 に答える
10

fxmlに追加できます。ステージレベル

<icons>
    <Image url="@../../../my_icon.png"/>
</icons>
于 2017-04-05T08:30:33.690 に答える
9

画像フォルダがあり、アイコンがそこに保存されている場合は、これを使用してください

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/images/comparison.png")));

パッケージから直接使用している場合は、これを使用することをお勧めします。

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("comparison.png")));

フォルダ構造があり、その中にアイコンがある場合は、

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("../images/comparison.png")));
于 2016-10-25T12:26:19.573 に答える
2
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/icon.png")));

icon.pngがresourcesdirにあり、前に「/」を付けることを忘れないでください。そうでない場合は機能しません。

于 2016-05-25T11:18:52.130 に答える
2

新しいパッケージ、つまりsrcディレクトリにimage.iconsを作成し、そこに.png画像を移動することについてどう思いますか?あなたが書く必要があるより:

Image image = new Image("/image/icons/nameOfImage.png");
primaryStage.getIcons().add(image);

この解決策は私にとっては完璧に機能しますが、それでも正しいかどうかはわかりません(ここでは初心者)。

于 2017-03-22T13:51:50.307 に答える
2
stage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("images/icon.png")));

画像フォルダはリソースフォルダにある必要があります。

于 2017-11-28T12:19:17.863 に答える
1
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/icon.png" )));

この方法を使用して、サイズの異なる複数のアイコンを追加できます。画像は同じ画像の異なるサイズである必要があり、最適なサイズが選択されます。 eg. 16x16, 32,32

于 2016-09-30T15:33:27.873 に答える
1

このコード行を使用して、アプリケーションにアイコンを簡単に配置できます

stage.getIcons()。add(new Image( "image path"));

于 2018-12-03T11:21:09.980 に答える
0
stage.getIcons().add(new Image("/images/logo_only.png"));

srcフォルダーにimagesフォルダーを作成し、そこから画像を取得することをお勧めします。

于 2017-07-17T13:24:54.753 に答える
0

私は自分のアプリケーションでこれを使用しました

Image icon = new Image(getClass().getResourceAsStream("icon.png"));
window.getIcons().add(icon);

こちらが舞台です。

于 2017-12-11T04:31:54.600 に答える
0

jarファイルを実行すると、Michael Berryによって指定されたコードによって、タイトルバーとタスクバーのアイコンが変更されます。ショートカットアイコンは変更できません。

com.zenjavaでコンパイルされたネイティブプログラムを実行する場合は、プログラムアイコンへのリンクを追加する必要があります。

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.8.3</version>
    <configuration>
    ...
        <bundleArguments>
            <icon>${project.basedir}/src/main/resources/images/filename.ico</icon>
        </bundleArguments>
    </configuration>
</plugin>

これにより、ショートカットとタスクバーにアイコンが追加されます。

于 2019-03-04T14:11:03.043 に答える
0

実行時にアイコンを切り替えます。

ここでの応答に加えて、初めてアイコンをアプリケーションに割り当てた後は、ステージに新しいアイコンを追加するだけではアイコンを切り替えることができないことがわかりました(これは、のアイコンを切り替える必要がある場合に役立ちますオン/オフからのアプリの有効化/無効化)。

実行時に新しいアイコンを設定するには、新しいアイコンを追加する前にgetIcons()。remove(0)を使用します。ここで、0は、オーバーライドするアイコンのインデックスです。

//Setting icon by first time (You can do this on your start method).
stage.getIcons().add(new Image(getClass().getResourceAsStream("enabled.png")));

//Overriding app icon with a new status (This can be in another method)
stage.getIcons().remove(0);
stage.getIcons().add(new Image(getClass().getResourceAsStream("disabled.png")));

他のメソッドまたはクラスからステージにアクセスするには、メインクラスのステージに新しい静的フィールドを作成して、アプリのどこからでもアクセスできる静的メソッドにカプセル化することで、start()メソッドからステージにアクセスできるようにします。 。

public class MainApp extends Application {
    private static Stage stage;
    public static Stage getStage() { return stage; }

    @Override public void start(Stage primaryStage) {
        stage = primaryStage
        stage.getIcons().add(new Image(getClass().getResourceAsStream("enabled.png")));
    }
}

public class AnotherClass {
    public void setStageTitle(String newTitle) {
        MainApp.getStage().setTitle(newTitle);
        MainApp.getStage().getIcons().remove(0);
        MainApp.getStage().getIcons().add(new Image(getClass().getResourceAsStream("disabled.png")));
    }
}
于 2019-08-27T20:03:04.903 に答える
-1

Invalid URL or resource not foundicon.pngをワークスペースの「bin」フォルダーに配置した場合。

于 2018-11-28T19:11:19.307 に答える
-2

JavaFXのタイトルバーに独自のアイコンを挿入するもう1つの簡単な方法は、次の方法を使用して画像をプライマリステージに追加することです。

Image ico = new Image("resources/images/iconLogo.png");
stage.getIcons().add(ico);

インポートjavafx.scene.image.Imageを確認してください(netbeansのようなideを使用している場合、これは自動的に実行されます)。

于 2012-10-16T09:25:59.393 に答える
-2

私はこれを試しました、そしてそれは働きます:

stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));
于 2016-08-17T20:30:05.190 に答える