Javafx从jar加载图像

时间:2015-05-16 17:08:22

标签: java javafx fxml

我正在使用Scene Builder 2.0和eclipse Luna。 在fxml文件中,我有我的场景代码与图像。 如果我在日食上测试这一切都没关系,但如果我把它导出到runnable jar然后运行这个我有没有图像的窗口...... 以下是代码的一部分:

<ImageView layoutX="63.0" layoutY="14.0">
                     <image>
                        <Image url="@../Images/MainPage/O.png" />
                     </image>
                  </ImageView>

文件结构:

Graph:
    MainPage:
        MainPage.fxml
    Images:
        MainPage:
            O.png

我认为问题出在路径上,但我不知道如何制作这条路。 装载机代码:

public class ScreensController extends StackPane{

    private HashMap<String, Node> screens = new HashMap<>();

    public ScreensController() {
        super();
    }

    public void addScreen(String name, Node screen){
        System.out.println("name: "+name);
        System.out.println("screen: "+name);
        screens.put(name, screen);
    }

    public Node getScreen(String name){
        return screens.get(name);
    }

    //name - nazwa Screenu
    //rescue - nazwa pliku FXML
    public boolean loadScreen(String name, String rescue){
        try {
            System.out.println("loading Screen "+name+" path: "+rescue);
            System.out.println("0001");
            FXMLLoader myLoader= new FXMLLoader(getClass().getResource(rescue));
            System.out.println("0002");
            Parent loadScreeen = (Parent) myLoader.load();
            System.out.println("0003");
            ScreenControlled myScreenControler = ((ScreenControlled) myLoader.getController());
            System.out.println("0004");
            myScreenControler.setScreenParent(this);
            System.out.println("0005");
            addScreen(name, loadScreeen);//Dodanie do HashMap
            System.out.println("0006");
            return true;
        } catch (IOException e) {
            System.out.println("IOException Error in ScreenController: "+e.getMessage().substring(1,e.getMessage().length())
                    +"\n Cause: "+e.getCause()
                    +"\n Localized Message: "+e.getLocalizedMessage().substring(1,e.getLocalizedMessage().length())
                    +"\n Class: "+e.getClass()
                    +"\n ST: ");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setScreen(final String name){
        System.out.println("Setting Screen: "+name);
        if(screens.get(name) != null) { //screen loaded
            final DoubleProperty opacity = opacityProperty();

            if(!getChildren().isEmpty()) { //if there is more than one screen
                Timeline fade = new Timeline(
                    new KeyFrame(Duration.ZERO, new KeyValue(opacity, 1.0)),
                    new KeyFrame(new Duration(1000), new EventHandler<ActionEvent>(){

                        @Override
                        public void handle(ActionEvent t){
                            getChildren().remove(0); //remove the displayed screen
                            getChildren().add(0, screens.get(name)); //add the screen
                            Timeline fadeIn = new Timeline(
                                    new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                                    new KeyFrame(new Duration(800), new KeyValue(opacity, 1.0)));
                            fadeIn.play();
                        }
                    }, new KeyValue(opacity, 0.0)));
                    fade.play();
                }else{
                    setOpacity(0.0);
                    getChildren().add(screens.get(name)); //no one else been displayed, then just show
                    Timeline fadeIn = new Timeline(
                            new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                            new KeyFrame(new Duration(1000), new KeyValue(opacity, 1.0)));
                    fadeIn.play();
                }
            return true;
            }else{
                System.out.println("screen hasn't been loaded!!!\n");
                return false;
        }
    }

    public boolean unloadSCreen(String name) {
        if(screens.remove(name)==null) {
            System.out.println("Screen " +name+ " didn't exist");
            return false;
        }else{
            return true;
        }
    }
}

初始化功能:

public void start(Stage stage) throws Exception {

        new Fonts().initialize();

        this.stage=stage;
        ScreensController mainContainer = new ScreensController();
        mainContainer.loadScreen(ScreensFramework.screen2ID,  ScreensFramework.screen2File);

        mainContainer.setScreen(ScreensFramework.screen2ID);

        root = new StackPane();//Group root = new Group();
        root.getChildren().addAll(mainContainer);
        Scene scene = new Scene(root);
        this.stage.setScene(scene);
        this.stage.show();
}

1 个答案:

答案 0 :(得分:0)

您在路径的开头缺少/。请使用:

FXMLLoader myLoader= new FXMLLoader(getClass()
 .getResource(/com/gmail/Slupik98/Project_Stangar/Graph/MainPage/MainPage.fxml));