在JavaFX中创建Singleton Controller类 [英] Creating a Singleton Controller class in JavaFX

查看:176
本文介绍了在JavaFX中创建Singleton Controller类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,让下面的代码执行。所有它的工作除了提示的部分,说这是我的问题区域。在那个区域,我需要返回controller.getNoun()。getText();的值。它工作正常,但返回null,因为我无法弄清楚如何调用控制器,而无需初始化它。我试过一个Singleton类基于另一个线程在这个网站,但控制器从来没有被初始化,直到我在这里调用它,所以我的名词值仍然被nulled出来。
我的唯一目标是这个帖子是从Noun Textfield并插入到用户输入的故事。
你的故事是什么?使用#noun#etc作为提示词的占位符。
应该成为
你的故事是什么?使用TextField Value等作为提示字的占位符。

I am running into issues getting the below code to execute. All of it works except for the part in the Prompter that says This is my issue area. In that area i need to return the value of controller.getNoun().getText(); which works fine but returns null because i cant figure out how to call controller without initializing it. I tried a Singleton class based on another thread on this site but Controller never got initialized until i called it here so my Noun value was still getting nulled out.. My sole goal with this post is to get the value from the Noun Textfield and plug it in to the story the user has entered. What is your story? Use #noun# etc as placeholders for prompted words. should become What is your story? Use TextField Value etc as placeholders for prompted words.

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);
    }


}


推荐答案

首先,从控制器暴露控件是一个不好的主意。如果您想要更改,例如从 TextField TextArea ,如果控件公开暴露,您将非常困难。它们应该被视为视图 - 控制器对的实现细节。所以我只会公开数据,例如。

First, it's a really bad idea to expose controls from your controller. If you want to change, e.g. from a TextField to a TextArea, you will have a really difficult time doing so if the controls are publicly exposed. They should be considered implementation details of the view-controller pair. So I would expose only the data, e.g.

public class Controller {

    // ...

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

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

    // ...
}

对于访问控制器,所有你需要的是传递一个引用到 Prompter 类:

As for accessing the controller, surely all you need is to pass a reference to it to your Prompter class:

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);
    }

}

当然,如果你真的need是名词,你只需将它传递给 Prompter

Of course, if all you really need is the noun, you could just pass that to the Prompter instead:

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);
}

singleton controller是一个矛盾。

A "singleton controller" (in this context) is an oxymoron, imho.

这篇关于在JavaFX中创建Singleton Controller类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆