在JavaFX中创建Singleton Controller类

时间:2016-02-22 23:24:56

标签: javafx controller singleton

我遇到了让下面的代码执行的问题。所有这些都有效,除了Prompter中的部分说这是我的问题区域。在那个区域我需要返回controller.getNoun()。getText();的值。哪个工作正常,但返回null因为我无法弄清楚如何调用控制器而不初始化它。我尝试了一个基于该网站上另一个线程的Singleton类,但是控制器从未被初始化,直到我在这里调用它,所以我的名词值仍然被淘汰了.. 这篇文章的唯一目标是从名词文本字段中获取值并将其插入用户输入的故事中。 你的故事是什么?使用#novory#等作为提示词的占位符。 应该成为 你的故事是什么?使用TextField Value等作为提示单词的占位符。

package sample;

import java.util.*;

public class Prompter {
    private String finalStory;
    private Controller mController;

    public String getFinalStory() {
        return finalStory;
    }

    public void run (Template tmpl) {
        List<String> blanks;
            blanks = promptForWords(tmpl);
        //System.out.printf("%s", tmpl.render(blanks));
        finalStory = tmpl.render(blanks);
    }

    private List<String> promptForWords(Template tmpl) {
        List<String> words = new ArrayList<>();
        String word = "THIS IS MY ISSUE AREA";
        words.add(word);
        return words;
    }
}
package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;

public class Controller {
    @FXML
    private TextField noun;
    @FXML
    private TextField age;
    @FXML
    private Text results;
    @FXML
    private HBox showWords;
    @FXML
    private HBox validateAge;
    @FXML
    private Button buttonFour;
    @FXML
    private TextArea treeStory;
    @FXML
    private Button buttonTwo;

    public int getAge() {
        return Integer.parseInt(age.getText());
    }

    public TextArea getTreeStory() {
        return treeStory;
    }

    public TextField getNoun() {
        return noun;
    }

    private String getToYoung() {
        return "Sorry you must be at least 13 to use this program.";
    }

    public void handleAge() {
        if (getAge() <= 12) {
            validateAge.setVisible(false);
            results.setText(getToYoung());
            new TimedExit();
        } else {
            validateAge.setVisible(false);
            treeStory.setVisible(true);
            buttonTwo.setVisible(true);
        }
    }

    public void handleStory() {
        showWords.setVisible(true);
        buttonFour.setVisible(true);
        treeStory.setVisible(false);
        buttonTwo.setVisible(false);
    }


    public void handleResults() {
        Prompter prompter = new Prompter();
        String story = getTreeStory().getText();
        Template tmpl = new Template(story);
        prompter.run(tmpl);
        results.setText(prompter.getFinalStory());
        showWords.setVisible(false);
        buttonFour.setVisible(false);
    }


}

1 个答案:

答案 0 :(得分:2)

首先,从控制器公开控件是一个非常糟糕的主意。如果你想改变,例如从TextFieldTextArea,如果控件公开,您将很难这样做。它们应被视为视图 - 控制器对的实现细节。所以我只公开数据,例如

public class Controller {

    // ...

    public String getNoun() {
        return noun.getText();
    }

    // and if you need it:
    public void setNoun(String noun) {
        this.noun.setText(noun);
    }

    // ...
}

至于访问控制器,当然您只需将对它的引用传递给Prompter类:

public class Prompter {
    private String finalStory;
    private Controller mController;

    public Prompter(Controller controller) {
        this.mController = controller ;
    }

    // ...

    private List<String> promptForWords(Template tmpl) {
        List<String> words = new ArrayList<>();
        String word = mController.getNoun();
        words.add(word);
        return words;
    }    

}

然后

public class Controller {

    // existing code...

    public void handleResults() {
        Prompter prompter = new Prompter(this);
        String story = getTreeStory().getText();
        Template tmpl = new Template(story);
        prompter.run(tmpl);
        results.setText(prompter.getFinalStory());
        showWords.setVisible(false);
        buttonFour.setVisible(false);
    }

}

当然,如果您真正需要的只是名词,那么您可以将其传递给Prompter而不是:

public class Prompter {
    private String finalStory;

    public String getFinalStory() {
        return finalStory;
    }

    public void run (Template tmpl, String noun) {
        List<String> blanks;
        blanks = promptForWords(tmpl, noun);
        //System.out.printf("%s", tmpl.render(blanks));
        finalStory = tmpl.render(blanks);
    }

    private List<String> promptForWords(Template tmpl, String word) {
        List<String> words = new ArrayList<>();
        words.add(word);
        return words;
    }
}

然后只是

public void handleResults() {
    Prompter prompter = new Prompter();
    String story = getTreeStory().getText();
    Template tmpl = new Template(story);
    prompter.run(tmpl, noun.getText());
    results.setText(prompter.getFinalStory());
    showWords.setVisible(false);
    buttonFour.setVisible(false);
}

A&#34;单身控制器&#34; (在这种情况下)是矛盾的,imho。