在JavaFX中创建单例控制器类
我在执行下面的代码时遇到了一些问题。除了提示器中说这是我的问题区域的部分外,所有这些都有效。在该区域中,我需要返回controller.getNon().getText()的值;它工作正常,但返回null,因为我不知道如何在不初始化控制器的情况下调用它。我在这个站点上尝试了一个基于另一个线程的单例类,但控制器在我调用它之前从未初始化过,所以我的名词值仍然为null。。 我写这篇文章的唯一目的是从名词文本字段中获取值,并将其插入到用户输入的故事中。 你的故事是什么?使用#名词#等作为提示词的占位符。 应该成为 你的故事是什么?使用文本字段值等作为提示词的占位符在JavaFX中创建单例控制器类,javafx,controller,singleton,Javafx,Controller,Singleton,我在执行下面的代码时遇到了一些问题。除了提示器中说这是我的问题区域的部分外,所有这些都有效。在该区域中,我需要返回controller.getNon().getText()的值;它工作正常,但返回null,因为我不知道如何在不初始化控制器的情况下调用它。我在这个站点上尝试了一个基于另一个线程的单例类,但控制器在我调用它之前从未初始化过,所以我的名词值仍然为null。。 我写这篇文章的唯一目的是从名词文本字段中获取值,并将其插入到用户输入的故事中。 你的故事是什么?使用#名词#等作为提示词的占位符
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;
}
}
包装样品;
导入java.util.*;
公共类提示器{
私人字符串最终记录;
专用控制器;
公共字符串getFinalStory(){
回归终结论;
}
公共作废运行(模板tmpl){
列出空白;
空白=提示词(tmpl);
//System.out.printf(“%s”,tmpl.render(空白));
finalStory=tmpl.render(空白);
}
私有列表提示词(模板tmpl){
List words=new ArrayList();
String word=“这是我的问题区域”;
添加(word);
返回单词;
}
}
包装样品;
导入javafx.fxml.fxml;
导入javafx.scene.control.Button;
导入javafx.scene.control.TextArea;
导入javafx.scene.control.TextField;
导入javafx.scene.layout.HBox;
导入javafx.scene.text.text;
公共类控制器{
@FXML
私有文本字段名词;
@FXML
私人文本字段年龄;
@FXML
私人文本结果;
@FXML
私人HBox秀词;
@FXML
私人HBox验证EAGE;
@FXML
私人按钮按钮;
@FXML
私家区树木;
@FXML
私人按钮按钮2;
公共整数getAge(){
返回Integer.parseInt(age.getText());
}
公共文本区域getTreeStory(){
返回树;
}
公共文本字段getNon(){
返回名词;
}
私有字符串getToYoung(){
return“抱歉,您必须年满13岁才能使用此程序。”;
}
公共无效手册(){
if(getAge()首先,从您的控制器公开控件是一个非常糟糕的主意。如果您想更改,例如从TextField
更改为TextArea
,那么如果控件是公开的,您将很难做到这一点。它们应该被视为视图-控制器对的实现细节。因此,我只公开e数据,例如
public class Controller {
// ...
public String getNoun() {
return noun.getText();
}
// and if you need it:
public void setNoun(String noun) {
this.noun.setText(noun);
}
// ...
}
至于访问控制器,您所需要的肯定是将对它的引用传递给您的提示器
类:
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 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;
}
}
“单例控制器”(在此上下文中)这是一个矛盾修饰法,imho。首先,从控制器公开控件是一个非常糟糕的主意。如果您想更改,例如从文本字段
更改为文本区域
,那么如果控件公开,您将很难这样做。它们应该被视为视图控制器对的实现细节。所以我只会公开数据,例如
public class Controller {
// ...
public String getNoun() {
return noun.getText();
}
// and if you need it:
public void setNoun(String noun) {
this.noun.setText(noun);
}
// ...
}
至于访问控制器,您所需要的肯定是将对它的引用传递给您的提示器
类:
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 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;
}
}
“单例控制器”(在此上下文中)这是一个矛盾修饰法,imho。非常感谢。我曾尝试设置mController变量,但我错过了设置此变量的一部分。mController=controller。它工作起来就像一个梦。同样感谢您提供的提示,我将更好地指定公共与私人,因为这完全有道理。非常感谢。我已尝试设置mController变量,但我错过了设置这个的一部分。mController=controller。它工作起来就像做梦一样。也感谢您的提示,我将在指定公共与私人方面做得更好,因为这完全有意义。