我不明白如何添加简单的图像。我导入了所有内容并按照他们在此页面上的说法进行操作:
http://www.java2s.com/Code/Java/JavaFX/LoadajpgimagewithImageanduseImageViewtodisplay.htm
Javafx代码
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class test extends Application {
@Override
public void start(Stage stage) {
stage.setTitle("HTML");
stage.setWidth(500);
stage.setHeight(500);
Scene scene = new Scene(new Group());
VBox root = new VBox();
final ImageView selectedImage = new ImageView();
Image image1 = new Image(test.class.getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));
selectedImage.setImage(image1);
root.getChildren().addAll(selectedImage);
scene.setRoot(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
错误
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1110)
at javafx.scene.image.Image.<init>(Image.java:694)
at prototype.test.start(test.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
答案 0 :(得分:2)
Class.getResourceAsStream
尝试在类路径中加载一个元素。它并不意味着加载文件,除非它们包含在类路径中。要在类路径之外加载文件,请改用FileInputStream
:
REGISTER 'smsiuc_udf.py' using streaming_python as smsiuc_udfs;
cdrs = load '2016040111*' USING PigStorage('|','-tagFile') ;
mastergtrec = load 'master.txt' USING PigStorage(',','-tagFile');
mastergt = FOREACH mastergtrec GENERATE (chararray) UPPER($1) as opcdpc, (chararray) UPPER($2) as gtoptname,(chararray) UPPER($3) as gtoptcircle;
mastergttup = FOREACH mastergt generate TOTUPLE(opcdpc,gtoptname,gtoptcircle) as mstgttup;
cdrrecord = FOREACH cdrs GENERATE (chararray) UPPER($1) as aparty, (chararray) UPPER($2) as bparty,$3 as smssentdate,$4 as smssenttime,($29=='6' ? 'S' : 'F') as status,(chararray) UPPER($26) as srcgt,(chararray) UPPER($27) as destgt,($12=='405899136999995' ? 'MTSDEL-CDMA' : ($12=='919875089998' ? 'MTSRAJ-GSM' : ($12=='405899150999995' ? 'MTSCHN-CDMA' : $12) ) ) as smscgt, (chararray)$0 as cdrfname,(chararray) $13 as prepost;
filteredp2pcdrs = FILTER cdrrecord by smsiuc_udfs.pullp2pcdrs(aparty,bparty,srcgt,destgt) and status == 'S' and SUBSTRING(smssentdate,4,6) == '$MON';
groupp2pcdrs = GROUP filteredp2pcdrs by (srcgt,destgt,aparty,bparty,smscgt,status,prepost);
distinctp2pcdrs= FOREACH groupp2pcdrs {
uniq = DISTINCT filteredp2pcdrs.(srcgt,destgt,aparty,bparty,smscgt,status,prepost);
GENERATE FLATTEN(group),COUNT(uniq) as cnt;
};
p2preportmap = FOREACH distinctp2pcdrs GENERATE smsiuc_udfs.p2preport(srcgt,destgt,aparty,bparty,mastergttup ),smscgt,status,prepost,cnt
或者使用Image(String)
constructor并传递文件的Image image1 = new Image(new FileInputStream("C:\\Users\\user\\Desktop\\x.jpg"));
:
URL
答案 1 :(得分:1)
尝试将图片添加到GridPane
ImageView img1 = new ImageView(new
Image(getClass().getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg")));
GridPane.setConstraints(img1, 0, 4);
答案 2 :(得分:0)
您的问题似乎就是这一行:
Image image1 = new Image(test.class.getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));
因为你得到一个NullPointerException:java.lang.NullPointerException: Input stream must not be null
。
请参阅:
答案 3 :(得分:0)
我认为解决此问题的一个好方法是将图像拖到项目文件夹中。这样您就可以单独搜索名称
Image image1 = new Image(test.class.getResourceAsStream("x.jpg"));
如果这样做,则添加文件:
答案 4 :(得分:0)
正如fabian所提到的,Class.getResourceAsStream(...)
从类路径加载InputStream
(意思是从jar文件本身)。要从您的计算机加载文件,您只需使用file
协议并提供Image
构造函数的路径,如下所示:
Image image1 = new Image("file:///C:/Users/user/Desktop/x.jpg");
你得到的原因:
Caused by: java.lang.NullPointerException: Input stream must not be null
错误中的是因为Class.getResourceAsStream(...)
只是在找不到您要求其查找的资源时返回null
。 Image
构造函数正在接收null
,然后抛出异常。
答案 5 :(得分:-1)
Your problem is at line getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));
替换&#34; \\&#34;到&#34; /&#34;,你的问题解决了