正在尝试用java为按钮创建图像,但图像未显示
imagePath变量现在看起来像这样:C:\Files\Photos\btn1.png。 按钮的代码如下所示:正在尝试用java为按钮创建图像,但图像未显示,java,Java,imagePath变量现在看起来像这样:C:\Files\Photos\btn1.png。 按钮的代码如下所示: dyButton.setGraphic(新的图像视图(getButtonIcon(计数器)) 编辑:我通过了NullPointerException,但现在我的文件没有显示 private Image getButtonIcon(int counter){ String imagePath = arrPrograms.get(counter).getIconPath(
dyButton.setGraphic(新的图像视图(getButtonIcon(计数器))代码>
编辑:我通过了NullPointerException,但现在我的文件没有显示
private Image getButtonIcon(int counter){
String imagePath = arrPrograms.get(counter).getIconPath();
Image image = new Image(getClass().getResourceAsStream(imagePath));
return image;
}
按钮代码:
private void getButtonIconint counter(){
try {
String imagePath = arrPrograms.get(counter).getIconPath();
FileInputStream inputstream = new FileInputStream(imagePath);
FileInputStream(");
Image image = new Image(inputstream);
}
catch (FileNotFoundException e){
System.out.println("File not found");
}
}
图像的变量在类中创建,然后getClass返回null。显式声明类名,如ShowImage.class.getResourceAsStreamgetClass随后返回null。显式声明类名,例如ShowImage.class.getResourceAsStreamgetResourceAsStream()
应用于获取资源,而不是任意文件。资源通常是IDE中资源文件夹中的文件,当创建程序的jar时,资源文件夹会放在jar中并从中读取。@Real怀疑论者,这确实很有帮助,谢谢。getResourceAsStream()
应该用于获取资源,而不是任意文件。资源通常是IDE中的Resources文件夹中的文件,当程序的jar被创建时,这些文件被放置在jar中并从那里读取。@realponsign这确实很有帮助,谢谢。
getButtonIcon();
dyButton.setGraphic(new ImageView(image));