亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaFX:如何在用戶輸入上動態添加文本字段和按鈕

JavaFX:如何在用戶輸入上動態添加文本字段和按鈕

蕪湖不蕪 2023-04-26 16:41:21
我對 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            }        });
查看完整描述

1 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

這是一個將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;


/**

 *

 * @author blj0011

 */

public class JavaFXTestingGround extends Application{


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        launch(args);

    }


    @Override

    public void start(Stage stage) throws Exception {

        ListView<CellData> listView = new ListView();

        listView.setCellFactory((ListView<CellData> param) -> {

            ListCell<CellData> cell = new ListCell<CellData>() {

                TextField textField = new TextField();

                Button button = new Button();

                HBox hBox = new HBox(textField, button);

                {

                    HBox.setHgrow(textField, Priority.ALWAYS);

                }


                @Override

                protected void updateItem(CellData item, boolean empty) {

                    super.updateItem(item, empty);

                    if (item != null) {

                        textField.setPromptText(item.getTextFieldPromptText());

                        button.setText(item.getButtonText());

                        button.setOnAction((actionEvent) -> {

                            System.out.println("You clicked " + button.getText() + ". TextField Prompt text is " + textField.getPromptText() + ".");

                        });


                        setGraphic(hBox);

                    } else {

                        setGraphic(null);

                    }

                }

            };

            return cell;

        });        


        AtomicInteger i = new AtomicInteger(1);        

        Button button = new Button("Add Item");

        button.setOnAction((event) -> {

            listView.getItems().add(new CellData("Prompt Text: " + i.get(), "Button: " + i.getAndIncrement()));

        });


        VBox root = new VBox(button, listView);

        stage.setScene(new Scene(root));

        stage.show();

    }    

}

http://img1.sycdn.imooc.com//6448e3d10001a6fb02490456.jpg

查看完整回答
反對 回復 2023-04-26
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號