用两个不同的值绑定标签-javafx [英] Bind label with two different values-javafx
问题描述
我有一个实时的javafx
用户应用程序,该应用程序的标签为:No of answers/No of questions
,
用户回答问题时,答案的数量会增加,而一堂课向用户提出问题时,问题的数量会增加.
I have a live javafx
user application which has a label : No of answers/No of questions
,
No of answers increases as the user answers the question, and number of questions increases as one class throws questions to user.
标签最初看起来像-0/0
我想将两个不同的变量(NumberOfAnswers, NumberOfQuestions)
绑定到此标签,例如,如果我向用户抛出10个问题,并且用户回答了2个问题,则它应该类似于:2/10
I want to bind two different variables (NumberOfAnswers, NumberOfQuestions)
to this label, say if I have 10 questions thrown to user and user has answered 2 it should look like : 2/10
Label ansQuestLbl = new Label("0/0");
if (answerConnector!= null) {
log.info("Going to bind , No of answers: "+answerConnector.getNoOfAnswers());
ansQuesLbl.textProperty().bind(answerConnector.getNoOfAnswers().asString());
log.info("Bound number of answers with label on UI");
}
这仅将答案数"绑定到标签.
This only binds Number of answers to the label.
提前谢谢.
推荐答案
所有您需要的是 Bindings.concat(Object... args)
:
All you need is Bindings.concat(Object... args)
:
例如:
IntegerProperty noOfAnswers = answerConnector.noOfAnswersProperty();
IntegerProperty noOfQuestions = answerConnector.noOfQuestionsProperty();
ansQuesLbl.textProperty().bind(Bindings.concat(noOfAnswers, "/", noOfQuestions));
或者:
IntegerProperty noOfAnswers = answerConnector.noOfAnswersProperty();
IntegerProperty noOfQuestions = answerConnector.noOfQuestionsProperty();
ansQuesLbl.textProperty().bind(noOfAnswers.asString().concat("/").concat(noOfQuestions.asString()));
注意:为避免属性出现问题,我将建议您遵循javafx命名约定,以便answerConnector
的类应如下所示:
Note: To avoid problems with the Properties I would recommand to follow the javafx naming convention, so that the class for answerConnector
should look like this:
public class AnswerConnector {
private final IntegerProperty noOfAnswers = new SimpleIntegerProperty(0);
public IntegerProperty noOfAnswersProperty() {
return noOfAnswers;
}
public int getNoOfAnswers() {
return noOfAnswers.get();
}
public void setNoOfAnswers(int noOfAnswers) {
this.noOfAnswers.set(noOfAnswers);
}
// same for noOfQuestions
}
这篇关于用两个不同的值绑定标签-javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!