如何为按钮列表创建gwt clickhandler

ltqd579y  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(213)

我有3个主按钮和一个数据输入字段:
1.sendbutton 2.sortbutton 3.reserbutton 4.numberfield
通过点击send按钮,我需要创建按钮,其数量将等于输入字段中写入的数量。例如,如果在输入字段中写入5,则将创建5个按钮。
我有一个按钮来排序这些按钮和复位按钮,删除所有按钮和清除面板。
但我仍然有一个任务,通过按下创建按钮,我需要创建新的按钮,这些按钮将等于按钮上的数字文本。但不幸的是,我不知道如何为按钮列表创建一个单击处理程序。

package com.gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.gwt.shared.ButtonComparator;
import java.util.ArrayList;
import java.util.List;

public class Gwt implements EntryPoint {

    private static boolean sortedDesc = true;

    @Override
    public void onModuleLoad() {

        final VerticalPanel mainButtons = new VerticalPanel();
        final VerticalPanel buttonPanel = new VerticalPanel();

        final Button sendButton = new Button("Enter");
        final Button sortButton = new Button("Sort");
        final Button resetButton = new Button("Reset");

        List<Button> buttons = new ArrayList<>();

        TextBox numberField = new TextBox();

        mainButtons.add(sortButton);
        mainButtons.add(resetButton);
        mainButtons.add(buttonPanel);

        RootPanel.get("nameFieldContainer").add(numberField);
        RootPanel.get("sendButtonContainer").add(sendButton);

        //Fired when the user clicks on the sendButton
        sendButton.addClickHandler(event -> {

            if (numberField.getText() != null && !numberField.getText().equals(" ")) {

                int numberOfButtons = Integer.parseInt(numberField.getText());

                if (numberOfButtons < 1 || numberOfButtons > 1000) {

                    Window.alert("Please select a value more than 0 or less than 1000");

                } else {
                    int randomValue = 0;
                    boolean checker = true;

                        while(checker){
                            buttons.clear();
                            buttonPanel.clear();

                            for (int i = 0; i < numberOfButtons; i++) {
                                randomValue = (int) (Math.random() * 1000);

                                if(randomValue<=30 && randomValue > 0){
                                    checker = false;
                                }

                                Button updatedButton = new Button(randomValue + "");
                                buttons.add(updatedButton);
                                buttonPanel.add(updatedButton);
                            }
                        }

                    RootPanel.get("mainButtons").add(mainButtons);
                    DOM.getElementById("intro").getStyle().setDisplay(Style.Display.NONE);
                    DOM.getElementById("mainButtons").getStyle().setDisplay(Style.Display.BLOCK);
                }
            }
        });

        //Fired when the user clicks on the resetButton
        resetButton.addClickHandler(event -> {
            buttons.clear();
            buttonPanel.clear();
            sortedDesc = false;

            DOM.getElementById("mainButtons").getStyle().setDisplay(Style.Display.NONE);
            DOM.getElementById("intro").getStyle().setDisplay(Style.Display.BLOCK);
        });

        //Fired when the user clicks on the sortButton
        sortButton.addClickHandler(event -> {

            ButtonComparator buttonComparator = new ButtonComparator();

            if (sortedDesc) {
                buttons.sort(buttonComparator);
                sortedDesc = false;
            } else {
                buttons.sort(buttonComparator.reversed());
                sortedDesc = true;
            }

            buttonPanel.clear();

            for (Button button : buttons) {
                buttonPanel.add(button);
            }
        });
    }
}

我有这样的想法,但我不知道我应该把它放在哪里,如果我把它放在sendbutton里面。这个方法应该放在哪里,或者如何为按钮列表创建clickhendler

for(Button button: buttons){
                        button.addClickHandler(new ClickHandler() {
                            @Override
                            public void onClick(ClickEvent event) {
                                int valueOfText = Integer.parseInt(button.getText());
                                int randomValue = 0;

                                buttons.clear();
                                buttonPanel.clear();

                                for(int i = 0; i < valueOfText; i++){
                                    randomValue = (int) (Math.random() * 1000);

                                    Button updatedButton = new Button(randomValue + "");

                                    buttons.add(updatedButton);
                                    buttonPanel.add(updatedButton);
                                }
                            }
                        });
                    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题