【问题标题】:Trouble setting application icon with JavaFX使用 JavaFX 设置应用程序图标时遇到问题
【发布时间】:2018-03-18 13:57:06
【问题描述】:

我在使用 JavaFX 更改应用程序图标时遇到问题(代码如下,我的尝试已被注释掉)。我尝试从以前的堆栈溢出答案中实现几个解决方案,但我不确定这些方法现在是否已被弃用。我使用的是 NetBeans 8.2(图标位于源包下名为 images 的文件夹中)。

第一次尝试:非法开始表达。预期标识符:JavaFX Application Icon

第二次尝试:找不到合适的方法添加(java.awt.Image):Changing the icon of my java application

第三次尝试:找不到符号。 Cannot instantiate the type Image java?

第五次尝试:图像是抽象的,无法实例化。 http://docs.oracle.com/javafx/2/deployment/self-contained-packaging.htm

package javafxapplication1;

import java.awt.Image;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javax.imageio.ImageIO;


public class JavaFXApplication1 extends Application {
    private double xOffset = 0;
    private double yOffset = 0;


    @Override
    public void start(Stage stage) throws Exception {

        //stage.getIcons().add(Image(<JavaFXApplication1>.class.getResourceAsStream( "/images/fiji.png" ));

        Image i = ImageIO.read(getClass().getResource("/images/fiji.png"));
        //setIconImage(i);
        //stage.getIcons().add(i);

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

        // stage.getIcons().add(ImageIO.read(getClass().getResource("/images/fiji.png")));

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

        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        //stage.initStyle(StageStyle.UNDECORATED);
        // makes it moveble
        // LOOK INTO!!!!!!!!!!!
         root.setOnMousePressed(new EventHandler<MouseEvent>() {
             @Override
             public void handle(MouseEvent event) {
                 xOffset = event.getSceneX();
                 yOffset = event.getSceneY();
             }
         });
         root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
            stage.setX(event.getScreenX() - xOffset);
            stage.setY(event.getScreenY() - yOffset);
            }
         });        
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

【问题讨论】:

    标签: java image javafx stage


    【解决方案1】:

    您需要加载一个图像并将其添加到舞台的图标中。

    import javafx.scene.image.Image;
    Image icon = new Image(Controller.class.getResource("/game.png").toExternalForm(), false);  
    primaryStage.getIcons().add(icon);
    

    但是,在 Ubuntu 上,这些图标不会显示。这个JavaFX缺陷很久没有解决了。

    您的第一次尝试似乎缺少 Image 实例化的 new 关键字,并确保它是 javafx.scene.image.Image,而不是具有不同构造函数的 java.awt.Image 图像。试试这个:

    stage.getIcons().add(new Image(JavaFXApplication1.class.getResource( "/images/fiji.png" ).toExternalForm());
    

    【讨论】:

      【解决方案2】:

      首先,加载一张图片,然后添加到舞台对象。请确保给出从资源文件夹内部开始的路径,而不是从资源文件夹开始,否则使用整个项目路径。

      Image favicon = new Image('URL_OF_THE_IMAGE');
      stage.getIcons.add(favicon);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-11
        • 1970-01-01
        相关资源
        最近更新 更多