创建工作不变量的图像 [英] Creating image variable not working

查看:156
本文介绍了创建工作不变量的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了下面的code在eclipse:

package myProgram;

import com.sun.glass.ui.Screen;
import com.sun.prism.paint.Color;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application implements EventHandler<ActionEvent> {

Button btn1;
Button btn2;
Button btn3;

public float[] winSize = {200, 400};

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

@Override
public void start(Stage stage) throws Exception {
    VBox layout = new VBox(10);
    layout.setAlignment(Pos.CENTER);
    Scene scene = new Scene(layout, winSize[0], winSize[1]);

    Rectangle2D rect = javafx.stage.Screen.getPrimary().getVisualBounds();
    stage.setY(rect.getMinY() + rect.getHeight() - winSize[1]);
    stage.setX(rect.getMinX() + rect.getWidth() - winSize[0]);

    //Not working part below
    Image imageDecline = new Image(getClass().getResourceAsStream("image.png"));
    Button button5 = new Button();
    button5.setGraphic(new ImageView(imageDecline));
    //

    btn1 = new Button("Close");
    btn1.setOnAction(e -> stage.close());
    btn1.setStyle("-fx-font: 20 arial");
    layout.getChildren().add(btn1);
    btn1.setMaxWidth(160);
    System.out.print(System.getProperty("user.dir").toString());

    btn2 = new Button("Open new Win");
    btn2.setOnAction(e -> {

    });
    btn2.setStyle("-fx-font: 20 arial");
    layout.getChildren().add(btn2);
    btn2.setMaxWidth(160);

    btn3 = new Button("Alert");
    btn3.setOnAction(e -> {

    });
    btn3.setStyle("-fx-font: 20 arial");
    layout.getChildren().add(btn3);
    btn3.setMaxWidth(160);

    stage.setScene(scene);
    stage.show();
}
public void handle(ActionEvent event){

}
}

但是,如果我删除这部分工作原理:

//Not working part below
Image imageDecline = new Image(getClass().getResourceAsStream("image.png"));
    Button button5 = new Button();
    button5.setGraphic(new ImageView(imageDecline));
    //

我得到以下控制台文本和程序不打开:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Input stream must not be null
    at javafx.scene.image.Image.validateInputStream(Unknown Source)
    at javafx.scene.image.Image.<init>(Unknown Source)
    at myProgram.Main.start(Main.java:45)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    ... 1 more
Exception running application myProgram.Main

有没有人有任何想法,为什么这个问题到了我尝试创建一个变量来保存/参考(不知道)与为.png格式的图像?
我想加载图像,它处于脱机状态。而且我不明白我怎么会在一个类似的问题的答案解决这个问题,我的文件似乎很不同的我。

Does anyone have any idea why this problem arrives when i try to create a variable to hold/reference(not sure) an image with a .png format? I want to load an image and it is offline. And I can not understand how I would solve this from the answers in a similar questions, my file seems very different to me.

推荐答案

通常它是请求的资源名称和现有资源之间的不匹配。
特别是当提到没有文件夹/包。 Eclipse中可以看到自己的资源,但部署的应用程序可能没有他们。

Usually it is mismatch between requested resource name and existing resource. Especially when no folder/package is mentioned. Eclipse could see its own resources but deployed app may not have them.

简单的解决方案,以消除这种模糊性将在你的资源文件夹,并直接使用此名称。
不一样的 image.png ,但 /images/image.png

Simple solution to eliminate such ambiguity would be to have folder in your resources and use this name directly. Like not image.png but /images/image.png

我会建议从根开始的资源名称,以消除其他相对混乱。

I would recommend to start resource name from the root to eliminate other relative confusions.

如果你还在困惑我建议你开始调查你从你的应用程序中有什么样的资源。列出现有资源或试图访问一个你认为是在这里。然后,你将有例外,在code和可以很容易地看到什么是错的。

If you are still confused I'd recommend that you start to investigate what resources you have from within your app. List existing resources or try to access one what you think is here. Then you will have exception in your code and can easily see what is wrong.

这篇关于创建工作不变量的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆