Programming

JavaFX 응용 프로그램 아이콘

procodes 2020. 6. 5. 22:01
반응형

JavaFX 응용 프로그램 아이콘


JavaFX를 사용하여 응용 프로그램 아이콘을 변경할 수 있습니까? 아니면 Swing을 사용하여 수행해야합니까?


스테이지가 "스테이지"이고 파일이 파일 시스템에 있다고 가정합니다.

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

아래 주석에 따라 포함 된 항아리에 싸여 있으면 대신 다음 방법을 사용해야합니다.

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

나는 이것을 시도했고 그것은 완전히 작동한다. 코드는 다음과 같습니다

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

icon.png는 소스 파일과 같은 폴더에 있습니다.


초보자를위한 전체 프로그램 :)이 프로그램은 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 스크린 샷

JavaFX 8 용으로 업데이트

코드를 변경할 필요가 없습니다. 여전히 잘 작동합니다. 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"));

여기에 이미지 설명을 입력하십시오

도움이 되길 바랍니다. 감사!!


fxml로 추가 할 수 있습니다. 무대 수준

<icons>
    <Image url="@../../../my_icon.png"/>
</icons>

이미지 폴더가 있고 해당 폴더에 아이콘이 저장된 경우

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

and if you are directly using it from your package which is not a good practice use this

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

and if you have a folder structure and you have your icon inside that use

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

What do you think about creating new package i.e image.icons in your src directory and moving there you .png images? Than you just need to write:

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

This solution works for me perfectly, but still I'm not sure if it's correct (beginner here).


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

If your icon.png is in resources dir and remember to put a '/' before otherwise it will not work


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

You can add more than one icon with different sizes using this method.The images should be different sizes of the same image and the best size will be chosen. eg. 16x16, 32,32


stage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("images/icon.png")));

images folder need to be in Resource folder.


You can easily put icon to your application using this code line

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


stage.getIcons().add(new Image("/images/logo_only.png"));

It is good habit to make images folder in your src folder and get images from it.


I used this in my application

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

Here window is the stage.


stage.getIcons().add(new Image(("nospaniol/ui/icons/nospaniol.png")));


If you run the jar file, the code specified by Michael Berry will change the icon in the title bar and in the taskbar. Shortcut icon cannot be changed.

If you run a native program compiled with com.zenjava, You must add a link to the program icon:

<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>

This will add an icon to the shortcut and taskbar.


Toggle icons in runtime:

In addition to the responses here, I found that once you have assigned an Icon to your application by the first time you cannot toggle it by just adding a new icon to your stage (this would be helpful if you need to toggle the icon of your app from on/off enabled/disabled).

To set a new icon during run time use the getIcons().remove(0) before trying to add a new icon, where 0 is the index of the icon you want to override like is shown here:

//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")));

To access the stage from other methods or classes you can create a new static field for stage in you main class so can access it from out of the start() method by encapsulating in on a static method that you can access from anywhere in your app.

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")));
}
} 

I tried this and it works:

stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));

If you got Invalid URL or resource not found put your icon.png in the "bin" folder in your workspace.


Another easy way to insert your own icon on the title bar in JavaFX is to add the image to your primary stage using the following method:

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

Make sure your import javafx.scene.image.Image (if using an ide like netbeans this should be automatically done for you).

참고 URL : https://stackoverflow.com/questions/10121991/javafx-application-icon

반응형