在 JavaFX 中在舞台上设置图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20094620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:06:57  来源:igfitidea点击:

Set Icon on Stage in JavaFX

javajavafxstagewebview

提问by Siavash

I wanted to know how should I set icons on javaFX stage. I have found this method, but it did not work properly.

我想知道我应该如何在 javaFX 舞台上设置图标。我找到了这个方法,但它不能正常工作。

  stage.getIcons().add(new Image(iconImagePath));

stage is an instance of javafx.stage.Stage, and I have imported javafx.scene.image.Image. This is the exception which we receive:

stage是javafx.stage.Stage的一个实例,我导入了javafx.scene.image.Image。这是我们收到的例外:

Invalid URL: Invalid URL or resource not found

无效的 URL:无效的 URL 或找不到资源

Also, there is nothing wrong with the iconImagePath, its value is "G:/test.jpg" and there is a jpg file in the G drive named test. In addition, when we use ImageIO to read the same URL we can do it easily.

另外,iconImagePath也没有问题,它的值为“G:/test.jpg”,并且在G盘中有一个名为test的jpg文件。此外,当我们使用 ImageIO 读取相同的 URL 时,我们可以轻松完成。

回答by Maher Abuthraa

use

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

and put the image logo.png in root of your project ( at same directory where src )

并将图像 logo.png 放在项目的根目录中(在 src 所在的同一目录中)

回答by isnot2bad

The constructors of javafx.scene.image.Imageexpect a URI, not a (full) path. This URI can either be relative (e.g. /images/flower.png) or absolute (e.g. file:flower.png).

javafx.scene.image.Image期望 URI的构造函数,而不是(完整)路径。此 URI 可以是相对的(例如/images/flower.png)或绝对的(例如file:flower.png)。

Strings like G:/test.jpgare no valid URLs and hence illegal.

像这样G:/test.jpg的字符串不是有效的 URL,因此是非法的。

Try file:g:/test.jpginstead.

试试吧file:g:/test.jpg

Usually, the icons should be bundled with your application, so simply put the image file into your classpath (e.g. if you're using eclipse, put it into your 'src' directory) and use it like that:

通常,图标应该与您的应用程序捆绑在一起,因此只需将图像文件放入您的类路径中(例如,如果您使用的是 eclipse,请将其放入您的 'src' 目录中)并像这样使用它:

stage.getIcons().add(new Image("/logo.jpg"));

回答by subash

stage.getIcons().add(new Image(getClass().getResourceAsStream("bal.png")));

This example works. I placed an icon in the same folder/package as the source .java file.

这个例子有效。我在与源 .java 文件相同的文件夹/包中放置了一个图标。

Directory structure

目录结构

回答by subash

This is what I've done and it work. The image is located in the root of my resource folder.

这就是我所做的并且有效。该图像位于我的资源文件夹的根目录中。

stage.getIcons().add(new Image("/ubuntu-mini.png"));

I am using JavaFX 8

我正在使用 JavaFX 8

回答by King houssem

The solution is:

解决办法是:

File f = new File("image.png");
Image ix = new Image(f.toURI().toString());
stage.getIcons().add(ix);

回答by ChromeV

don't forget that your icon must be in 32x32or 16x16resolution, if not, it doesn't work.

不要忘记您的图标必须是32x3216x16分辨率,否则,它不起作用。

回答by user5591786

Best Way:

最好的办法:

stage.getIcons().add(new Image(getClass().getResource(IconImagePath).toExternalForm()));

回答by manikant gautam

Here is the working code, which is exactly what you neened:

这是工作代码,这正是您所需要的:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Manikant gautam
 * This is a beginner's sample application
 * using JAVAFX
 * 
*/

public class Helloworld extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);
        // Set Icon From Here.
        primaryStage.getIcons().add(
            new Image("/resource/graphics/app_logo.png"));
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

Set Icon by statement:

按语句设置图标:

primaryStage.getIcons().add(new Image("/resource/graphics/app_logo.png"));

回答by babbi

// Set the icon
stage.getIcons().add(new Image(getClass().getResourceAsStream("penguin.png")));

I faced the same problem. I used Netbeans. I'm not sure if the folder structure is different for other IDEs, but I had to put the picture in /build/classes/(package that contains the JavaFX class file). This means it doesn't go into the src folder.

我遇到了同样的问题。我使用了 Netbeans。我不确定其他 IDE 的文件夹结构是否不同,但我必须将图片放在 /build/classes/(包含 JavaFX 类文件的包)中。这意味着它不会进入 src 文件夹。

回答by Sunil

public class Main extends Application
{
    private static final Logger LOGGER = Logger.getLogger(Main.class);

    @Override
    public void start(Stage primaryStage)
        {
            try
                {
                    // BorderPane root = new BorderPane();
                    BorderPane root = (BorderPane) FXMLLoader
                            .load(getClass().getResource("/org/geeksynergy/view/layout/FrontPageBorder.fxml"));
                    root.setAccessibleText("good");

                    Scene scene = new Scene(root, 400, 400);
                    scene.getStylesheets().add(getClass()
                            .getResource("/org/geeksynergy/view/cssstyle/application.css").toExternalForm());
                    primaryStage.setScene(scene);
                    primaryStage.setTitle("AiRJuke");
                    primaryStage.getIcons().add(new Image("/org/geeksymergy/resource/images/download.png"));
                    primaryStage.show();
                    AnchorPane personOverview = (AnchorPane) FXMLLoader
                            .load(getClass().getResource("/org/geeksynergy/view/layout/Ui.fxml"));

                    root.setCenter(personOverview);

                    // added this line to save the playlist , when we close
                    // application window
                    Platform.setImplicitExit(false);

                    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
                        {
                            public void handle(WindowEvent event)
                                {
                                    M3UPlayList.defaultSavePlaylist();
                                    Platform.setImplicitExit(true);
                                    primaryStage.hide();
                                }
                        });

                } catch (Exception e)
                {

                    LOGGER.error("Exception while loding application", e);
                }
        }

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