Javafx 2 向Javafx应用程序添加图像
我在向javafx桌面应用程序添加徽标时遇到问题。收到的错误为“无效URL:无效URL或未找到资源”。我检查了一下,但没有这样的错误Javafx 2 向Javafx应用程序添加图像,javafx-2,Javafx 2,我在向javafx桌面应用程序添加徽标时遇到问题。收到的错误为“无效URL:无效URL或未找到资源”。我检查了一下,但没有这样的错误 import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.layout.*; import javafx.application.Application; import javafx.fxml.*; publ
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.application.Application;
import javafx.fxml.*;
public class Main extends Application{
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage primaryStage) {
try {
primaryStage.setTitle("App");
primaryStage.setIconified(true);
primaryStage.getIcons().add(new Image("logo.jpg"));
AnchorPane root=new AnchorPane();
root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene s=new Scene(root, 800, 600);
primaryStage.setScene(s);
primaryStage.show();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
类
Image
有一个构造函数,它接受一个字符串
参数。此参数是图像的URL<代码>“logo.jpg”不是有效的URL。使用构造函数的InputStream
变量会更幸运:
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("logo.jpg")));
(在上述情况下,logo.jpg
必须与类位于同一个包中;如果不是,则相应地修改路径)“Sample.fxml”
需要与类位于同一个包中