我在尝试运行显示图像的代码时获得了很大的跟踪,但是我不确定为什么代码没有运行。有什么想法吗?
static constexpr bool value { true };
我还尝试了一个小测试课程,确认我是否有正确的JPG路径并且似乎工作正常,所以我不认为问题在于JPG路径。 为了澄清,JPG位于类文件夹中,而不是在src文件夹中
public class SplashScreen extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
//Parent rootNode = null;
Group root = new Group();
Scene scene = new Scene(root, 500, 500);
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(10);
gridpane.setVgap(10);
ImageView algLogo = new ImageView();
Image logo = new Image("D:/Users/Tozu/workspace/ACA 5 OOJP/AlgonquinCollegeLogo.jpg");
algLogo.setImage(logo);
final HBox pictureRegion = new HBox();
pictureRegion.getChildren().add(algLogo);
gridpane.add(pictureRegion, 1, 1);
root.getChildren().add(gridpane);
stage.setTitle("ACA 5");
stage.setScene(scene);
stage.show();
}
答案 0 :(得分:1)
您必须设置URL文件协议
Image logo = new Image("file:D:/Users/Tozu/workspace/ACA 5 OOJP/AlgonquinCollegeLogo.jpg");
^ here
来自图片https://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html
的文档// load an image and resize it only in one dimension, to the height of 100 and
// the original width, without preserving original aspect ratio
// The image is located in the current working directory
Image image4 = new Image("file:flower.png", 0, 100, false, false);
执行相同操作的更好方法是使用File
对象(由James_D建议)
File f = new File("D:/Users/Tozu/workspace/ACA 5 OOJP/AlgonquinCollegeLogo.jpg");
Image logo = new Image(f.toURI().toString());