JavaFX& MVP,Object不应该是垃圾收集

时间:2015-12-29 18:04:22

标签: java javafx javafx-2 javafx-8 mvp

我被困在这一段时间,我真的是最后的希望。我真的不知道问题是什么。我有一个GUI项目使用我希望是正确的MVP形式,我的问题是我有一个presenter对象在我的视图类中在任何方法中使用时变为null。到目前为止,gui应该加载并且用户应该按下按钮,该方法调用presenter对象的方法来执行某些操作,但它始终为null。我尝试传入视图类的构造函数,使其成为视图类中的对象等,我无法修复它。任何帮助表示赞赏。我不知道发生了什么,似乎对我认为的任何其他对象都没有这样做

main.java

package main;

import model.Model;
import presenter.Presenter;
import view.View;

public class Main
{
    public static void main(String[] args) 
    {
        View view = new View();
        Model model = new Model();
        Presenter presenter = new Presenter(view, model);
        view.setPresenter(presenter);
        // this was a way I found on stack overflow to call
        // another class that inhierts the application class
        // and be able to have args passed if needed
        View.launch(View.class, args);
    }

}

View.java

    package view;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import presenter.Presenter;

public class View extends Application
{
    private Presenter presenter;
    private Button test;
    private Views views; 

    public final static String LOGINVIEW = "loginscreen";
    public final static String LOGINVIEWFILE = "FXMLViewLogin.fxml";
    public final static String CHATVIEW = "chatscreen";
    public final static String CHATVIEWFILE = "FXMLViewChat.fxml";

    public View()
    {
        // init Views class
        views = new Views();
    }

    public void setPresenter(Presenter pres)
    {
        this.presenter = pres;
    }

    @Override
    public void start(Stage primaryStage) 
    {
        loadViews();
        primaryStage.setScene(setView(LOGINVIEW));
        primaryStage.setTitle("");
        primaryStage.show();
    }

    private void loadViews()
    {
        views.loadScreen(LOGINVIEW, LOGINVIEWFILE);
        views.loadScreen(CHATVIEW, CHATVIEWFILE);
    }

    private Scene setView(String name)
    {
        return views.setView(name);
    }

    @FXML
    private void test(ActionEvent event) 
    {
        //***********************************************************
        // ALWAYS NULL IN THIS EVENT METHOD METHOD AND ANY OTHER METHOD FOR THAT FACT AFTER setPresenter
        if(presenter == null)
            System.out.println("Why is this null right here?");
    }
}

Presenter.java

package presenter;

import view.View;
import model.Model;

public class Presenter 
{
    private Model model;
    private View view;

    public Presenter(View view, Model model)
    {
        this.model = model;
        this.view = view;

    }

    // any methods start here 
}

Views.java

package view;

import java.util.HashMap;
import javafx.fxml.FXMLLoader;

import javafx.scene.Parent;
import javafx.scene.Scene;


public class Views 
{
    private final HashMap<String, Parent> view;

    public Views() 
    {
        this.view = new HashMap<>();
    }

    public void addView(String name, Parent screen) 
    {
        view.put(name, screen);
    }

    public boolean loadScreen(String name, String file)
    {
        try 
        {
            FXMLLoader myLoader = new FXMLLoader(getClass().getResource(file));
            Parent loadScreen = (Parent) myLoader.load();
            addView(name, loadScreen);
            return true;
        } 
        catch (Exception e) 
        {
            return false;
        }
    }

    public Scene setView(final String name) 
    {
        Scene scene = null;
        if (view.get(name) != null)  
            scene = new Scene(getParentNode(name));   
        unloadView(name);
        return scene;
    }

    private boolean unloadView(String name) 
    {
        return view.remove(name) != null;
    }

    private Parent getParentNode(String name)
    {
        return view.get(name);
    }

}

0 个答案:

没有答案