我测试了这段代码:
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage primaryStage)
{
// Image
Image image = new Image("za.png");
ImageView imageView = new ImageView();
imageView.setImage(image);
// Text
Text t = new Text();
t.setText("Do you want to quit?");
// Buttons
Button btnYes = new Button("Yes");
Button btnNo = new Button("No");
btnYes.setStyle("-fx-background-color:\n"
+ " #090a0c,\n"
+ " linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),\n"
+ " linear-gradient(#20262b, #191d22),\n"
+ " radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));\n"
+ " -fx-background-radius: 5,4,3,5;\n"
+ " -fx-background-insets: 0,1,2,0;\n"
+ " -fx-text-fill: white;\n"
+ " -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );\n"
+ " -fx-font-family: \"Arial\";\n"
+ " -fx-text-fill: linear-gradient(white, #d0d0d0);\n"
+ " -fx-font-size: 12px;\n"
+ " -fx-padding: 10 20 10 20;");
btnNo.setStyle("-fx-background-color:\n"
+ " #090a0c,\n"
+ " linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),\n"
+ " linear-gradient(#20262b, #191d22),\n"
+ " radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));\n"
+ " -fx-background-radius: 5,4,3,5;\n"
+ " -fx-background-insets: 0,1,2,0;\n"
+ " -fx-text-fill: white;\n"
+ " -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );\n"
+ " -fx-font-family: \"Arial\";\n"
+ " -fx-text-fill: linear-gradient(white, #d0d0d0);\n"
+ " -fx-font-size: 12px;\n"
+ " -fx-padding: 10 20 10 20;");
// Buttons layout
HBox hbox = new HBox(8); // spacing = 8
hbox.setStyle("-fx-padding: 15; -fx-font-size: 15pt;");
hbox.getChildren().addAll(btnYes, btnNo);
hbox.setAlignment(Pos.BASELINE_RIGHT);
BorderPane bp = new BorderPane();
bp.setStyle("-fx-background-color: linear-gradient(#ffffff,#f3f3f4);\n"
+ " -fx-border-width: 1 1 1 1;\n"
+ " -fx-border-color: #b4b4b4 transparent #b4b4b4 transparent;\n"
+ " -fx-font-size: 1.083333em;\n"
+ " -fx-text-fill: #292929;");
bp.setPadding(new Insets(10, 20, 10, 20));
//Button btnTop = new Button("Top");
bp.setTop(null);
//Button btnLeft = new Button("Left");
bp.setLeft(imageView);
//Button btnCenter = new Button("Center");
bp.setCenter(t);
//Button btnRight = new Button("Right");
bp.setRight(null);
//Button btnBottom = new Button("Bottom");
bp.setBottom(hbox);
Scene scene = new Scene(bp, 500, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
我收到此错误:
Executing com.javafx.main.Main from /home/rcbandit/Desktop/test/DX-57DC/dist/run429319394/DX-57DC.jar using platform /opt/jdk1.8.0/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.javafx.main.Main.launchApp(Main.java:642)
at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:986)
at javafx.scene.image.Image.<init>(Image.java:538)
at com.dx57dc.main.DX57DC.start(DX57DC.java:28)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:979)
... 12 more
Java Result: 1
Deleting directory /home/rcbandit/Desktop/test/DX-57DC/dist/run429319394
jfxsa-run:
BUILD SUCCESSFUL (total time: 6 seconds)
我将图像文件放在java源代码文件旁边,但找不到该文件。你能告诉我如何解决这个问题吗?
答案 0 :(得分:29)
Image image = new Image("za.png");
这个构造函数需要指向一个URI,所以如果你指向文件系统上的东西,那就是:
Image image = new Image("file:za.png");
或者,您可以这样做:
Image image = new Image(new File("za.png").toURI().toString());
......这可以说是更整洁。如果图像捆绑在jar而不是文件系统中,则可以像这样获取URI:
Image image = new Image(getClass().getResource("za.jpg").toURI().toString());
JavaFX中的大多数方法/构造函数以这种方式将字符串作为参数(即指定资源)通过字符串URI而不是纯文本路径或URL来实现。
答案 1 :(得分:15)
待办事项
javafx.scene.image.Image image = new javafx.scene.image.Image(getClass().getResource("za.jpg").toExternalForm());
ImageView iv = new ImageView(image);
或者只是
ImageView iv = new ImageView(getClass().getResource("za.jpg").toExternalForm());