在JavaFX中加载图像
我正在尝试从文件系统加载图像,但我没有错误并且不显示图像。图像文件位于Package文件夹../src/application/a.png中,我尝试以不同的方式加载图像,如下所示: 图像=新图像(“文件:a.png”) Image Image=新图像(新文件(“a.png”).toURI().toString())在JavaFX中加载图像,java,image,javafx,Java,Image,Javafx,我正在尝试从文件系统加载图像,但我没有错误并且不显示图像。图像文件位于Package文件夹../src/application/a.png中,我尝试以不同的方式加载图像,如下所示: 图像=新图像(“文件:a.png”) Image Image=新图像(新文件(“a.png”).toURI().toString()) 感谢帮助如果您尝试加载的图像与您的类位于同一目录中,请尝试: image=新图像(getClass().getResourceAsStream(“a.png”)) 否则,如果它位于类
感谢帮助如果您尝试加载的图像与您的类位于同一目录中,请尝试: image=新图像(getClass().getResourceAsStream(“a.png”)) 否则,如果它位于类所属目录的子目录中,请尝试: image=新图像(getClass().getResourceAsStream(“application/a.png”))。 鉴于您的项目结构是: |----src |----主要 |--------应用 |--------a、 巴布亚新几内亚
package application;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
Image image = new Image(new File("/a.png").toURI().toString());
// Setting the image view
ImageView imageView = new ImageView(image);
// Setting the position of the image
imageView.setX(0);
imageView.setY(0);
// setting the fit height and width of the image view
imageView.setFitHeight(200);
imageView.setFitWidth(400);
// Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
// Creating a Group object
Group root = new Group(imageView);
// Creating a scene object
Scene scene = new Scene(root, 600, 300);
// Setting title to the Stage
stage.setTitle("Coloradjust effect example");
// Adding scene to the stage
stage.setScene(scene);
// Displaying the contents of the stage
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}