如何使用 JavaFX 将选择框、复选框和文本字段实现为一个按钮 [关闭]
Posted
技术标签:
【中文标题】如何使用 JavaFX 将选择框、复选框和文本字段实现为一个按钮 [关闭]【英文标题】:How can a choice box, checkbox, and text field be implemented into one button with JavaFX [closed] 【发布时间】:2021-07-18 04:41:33 【问题描述】:我正在尝试创建一个程序,该程序可以从选择框、复选框和文本字段中获取数据,并将其作为输出放入单个 textArea 中。如何通过一个按钮组合所有信息。我已经创建了选择框、复选框和文本字段。
-有两个文本字段。其中一个取名字,另一个取数字。该数字将决定程序循环的次数。
-复选框将在名字前面加上什么样的前缀(先生、夫人等)。
-选择框给出了关于将给出什么后续声明的选项。 (今天过得怎么样?我喜欢你的帽子等)
点击按钮后,如果 textField 中的数字输入为 2,程序将在 textArea 中显示类似的内容
“1您好,Johnson 先生。今天过得怎么样?”
“2您好,Johnson 先生。今天过得怎么样?”
这就是我在按钮事件处理程序中的内容。它只实现文本字段
private class CreateButtonHandler implements EventHandler<ActionEvent>
@Override
public void handle(ActionEvent event)
int i = 0;
String myString = number.getText();
int foo = Integer.parseInt(myString);
do
OutputTxtArea.setText(OutputTxtArea.getText() + (i + 1) + "Hello " + firstNameTxtFld.getText() + "\n");
while(i<foo);
【问题讨论】:
您可以通过调用...getValue()
从ChoiceBox
中获取选定的值。您可以致电CheckBox.isSelected()
来确定@987654326@ 是否被...选中。
我还建议学习 Java naming conventions 并坚持下去。
【参考方案1】:
绑定!
如果您将布局与数据和动作分开,然后让动作作用于数据,事情会更容易做。这使得屏幕只是一个屏幕,您不必担心“我如何处理获取 CheckBox 值?”诸如此类的问题。
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Greeter extends Application
public static void main(String[] args)
launch(args);
@Override
public void start(Stage primaryStage)
Model model = new Model();
ChoiceBox<String> questionChoiceBox = new ChoiceBox<>(model.questionList);
TextArea textArea = new TextArea("");
textArea.textProperty().bind(model.greetingProperty());
VBox vBox = new VBox(20,
createBoundCheckBox("Miss", model.isMissProperty()),
createBoundCheckBox("Missus", model.isMissusProperty()),
createBoundCheckBox("Mister", model.isMisterProperty()),
questionChoiceBox,
createBoundTextField(model.nameProperty()),
createBoundTextField(model.loopCountProperty()),
textArea);
primaryStage.setScene(new Scene(vBox, 200, 300));
primaryStage.show();
@NotNull
private Node createBoundCheckBox(String label, BooleanProperty boundProperty)
CheckBox checkBox = new CheckBox(label);
boundProperty.bind(checkBox.selectedProperty());
return checkBox;
private Node createBoundTextField(StringProperty boundProperty)
TextField textField = new TextField();
boundProperty.bind(textField.textProperty());
return textField;
class Model
private StringProperty loopCount = new SimpleStringProperty("");
private BooleanProperty isMister = new SimpleBooleanProperty(false);
private BooleanProperty isMissus = new SimpleBooleanProperty(false);
private BooleanProperty isMiss = new SimpleBooleanProperty(false);
private StringProperty name = new SimpleStringProperty("");
private StringProperty question = new SimpleStringProperty("");
private ObservableList<String> questionList = FXCollections.observableArrayList();
Model()
questionList.add("How was your day?");
questionList.add("Would you like kippers for breakfast");
public StringProperty loopCountProperty()
return loopCount;
public BooleanProperty isMisterProperty()
return isMister;
public BooleanProperty isMissusProperty()
return isMissus;
public BooleanProperty isMissProperty()
return isMiss;
public StringProperty nameProperty()
return name;
public StringProperty questionProperty()
return question;
public ObservableList<String> getQuestionList()
return questionList;
public ObservableValue<String> greetingProperty()
return Bindings.createStringBinding(() -> buildGreeting(), loopCount, isMiss, isMissus, isMister, question, name);
private String buildGreeting()
int howMany;
try
howMany = Integer.parseInt(loopCount.get());
catch (Exception e)
return "";
String title = "Master of Time and Space";
if (isMiss.get())
title = "Miss";
if (isMissus.get())
title = "Mrs.";
if (isMister.get())
title = "Mr.";
String greeting = "Hello " + title + " " + name.get() + " " + question.get();
return IntStream.range(1, howMany + 1).mapToObj(idx -> idx + greeting).collect(Collectors.joining("\n"));
所以现在布局只是一个布局,屏幕上的所有控件的值属性都绑定到模型上。然后模型有代码把所有的东西放在一起来创建结果。从技术上讲,该代码应该属于某种业务逻辑类,但这至少应该让您有所了解。
我省略了按钮,因为它并没有真正添加任何有意义的功能,而且它对 UI 有一点问题。无法指示 TextArea 中的结果是否是最后一次单击按钮时的结果。例如,您可以单击按钮,然后选择另一个标题复选框,现在屏幕上的选择与 TextArea 不匹配。但是用户无法知道这一点,要实现它,您可能必须使用 DirtyFX 库之类的东西。
但如果你真的想要一个按钮,那么你所要做的就是为结果引入一个新的 StringProperty,然后让按钮在其 EventHandler 中调用 Model.buildGreeting():
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class GreeterButton extends Application
public static void main(String[] args)
launch(args);
@Override
public void start(Stage primaryStage)
Model model = new Model();
ChoiceBox<String> questionChoiceBox = new ChoiceBox<>(model.questionList);
TextArea textArea = new TextArea("");
textArea.textProperty().bind(model.greetingProperty());
Button button = new Button("Say Hi!");
button.setOnAction(evt -> model.updateGreeting());
VBox vBox = new VBox(20,
createBoundCheckBox("Miss", model.isMissProperty()),
createBoundCheckBox("Missus", model.isMissusProperty()),
createBoundCheckBox("Mister", model.isMisterProperty()),
questionChoiceBox,
createBoundTextField(model.nameProperty()),
createBoundTextField(model.loopCountProperty()),
button,
textArea);
primaryStage.setScene(new Scene(vBox, 200, 300));
primaryStage.show();
@NotNull
private Node createBoundCheckBox(String label, BooleanProperty boundProperty)
CheckBox checkBox = new CheckBox(label);
boundProperty.bind(checkBox.selectedProperty());
return checkBox;
private Node createBoundTextField(StringProperty boundProperty)
TextField textField = new TextField();
boundProperty.bind(textField.textProperty());
return textField;
class Model
private StringProperty loopCount = new SimpleStringProperty("");
private BooleanProperty isMister = new SimpleBooleanProperty(false);
private BooleanProperty isMissus = new SimpleBooleanProperty(false);
private BooleanProperty isMiss = new SimpleBooleanProperty(false);
private StringProperty name = new SimpleStringProperty("");
private StringProperty question = new SimpleStringProperty("");
private StringProperty greeting = new SimpleStringProperty("");
private ObservableList<String> questionList = FXCollections.observableArrayList();
Model()
questionList.add("How was your day?");
questionList.add("Would you like kippers for breakfast");
public StringProperty loopCountProperty()
return loopCount;
public BooleanProperty isMisterProperty()
return isMister;
public BooleanProperty isMissusProperty()
return isMissus;
public BooleanProperty isMissProperty()
return isMiss;
public StringProperty nameProperty()
return name;
public StringProperty questionProperty()
return question;
public ObservableList<String> getQuestionList()
return questionList;
String buildGreeting()
int howMany;
try
howMany = Integer.parseInt(loopCount.get());
catch (Exception e)
return "";
String title = "Master of Time and Space";
if (isMiss.get())
title = "Miss";
if (isMissus.get())
title = "Mrs.";
if (isMister.get())
title = "Mr.";
String greeting = "Hello " + title + " " + name.get() + " " + question.get();
return IntStream.range(1, howMany + 1).mapToObj(idx -> idx + greeting).collect(Collectors.joining("\n"));
public void updateGreeting()
greeting.set(buildGreeting());
public StringProperty greetingProperty()
return greeting;
【讨论】:
以上是关于如何使用 JavaFX 将选择框、复选框和文本字段实现为一个按钮 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何在选择列表项时将文本框附加到来自共享点列表的复选框列表项