JavaFX:如何在用户输入上动态添加文本字段和按钮

我对 Java 有点陌生,我正在尝试在 JavaFX 中创建一个用户界面,每当用户按下“+”按钮时,它就会动态添加一个 TextField 和一个“编辑”按钮。我知道尝试将这些按钮和文本字段存储在列表中是个好主意,但我不确定如何将它们放在界面上


ListView<Button> dinamicButtons = new ListView<Button>();

ListView<TextField> dinamicTextFields = new ListView<TextField>();

int quantityOfDinamicFieldsAdded =0;

 Button add= new Button ("+");

        add.setOnAction(e -> {

            dinamicButtons .add(quantityOfDinamicFieldsAdded , new Button("Edit"));

            dinamicTextFields .add(quantityOfDinamicFieldsAdded , new TextField("nome do Componente"));

            quantityOfDinamicFieldsAdded ++;

            //remove dynamically added buttons and text fields

            for (int i=0 ; i<quantityOfDinamicFieldsAdded ; i++){

                //add them all back

            }

        });


芜湖不芜
浏览 124回答 1
1回答

繁星点点滴滴

这是一个将Buttonand添加TextField到ListView. 只是POJO跟踪提示TextField's文本和Button's文本。关键是使用CellFactory.import java.util.concurrent.atomic.AtomicInteger;import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.ListCell;import javafx.scene.control.ListView;import javafx.scene.control.TextField;import javafx.scene.layout.HBox;import javafx.scene.layout.Priority;import javafx.scene.layout.VBox;import javafx.stage.Stage;/**&nbsp;*&nbsp;* @author blj0011&nbsp;*/public class JavaFXTestingGround extends Application{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param args the command line arguments&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage stage) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; ListView<CellData> listView = new ListView();&nbsp; &nbsp; &nbsp; &nbsp; listView.setCellFactory((ListView<CellData> param) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListCell<CellData> cell = new ListCell<CellData>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextField textField = new TextField();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Button button = new Button();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HBox hBox = new HBox(textField, button);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HBox.setHgrow(textField, Priority.ALWAYS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected void updateItem(CellData item, boolean empty) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.updateItem(item, empty);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (item != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textField.setPromptText(item.getTextFieldPromptText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button.setText(item.getButtonText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button.setOnAction((actionEvent) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You clicked " + button.getText() + ". TextField Prompt text is " + textField.getPromptText() + ".");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setGraphic(hBox);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setGraphic(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return cell;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AtomicInteger i = new AtomicInteger(1);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Button button = new Button("Add Item");&nbsp; &nbsp; &nbsp; &nbsp; button.setOnAction((event) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listView.getItems().add(new CellData("Prompt Text: " + i.get(), "Button: " + i.getAndIncrement()));&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; VBox root = new VBox(button, listView);&nbsp; &nbsp; &nbsp; &nbsp; stage.setScene(new Scene(root));&nbsp; &nbsp; &nbsp; &nbsp; stage.show();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java