gpt4 book ai didi

java - 单击 GWT 会触发多个单击事件

转载 作者:行者123 更新时间:2023-11-29 06:56:04 43 4
gpt4 key购买 nike

我有一个 FlexTable 并有一个与之关联的点击事件。有时会多次触发事件。有时单击一次最多 10 次。我已经检查了多个事件处理程序,但我只有一个。从案例中可以明显看出,它有时每次点击只运行一次。

有没有人知道它为什么会发生。

这是示例片段,我尝试对其进行调试,发现点击处理程序被触发了多次。

        private FlexTable flex = new FlexTable(); //it is a global variable
function A(){
flex.clear();
flex.removeAllRows();
flex.removeAllRows();
flex.setBorderWidth(2);
flex.setCellPadding(2);
flex.setCellSpacing(2);
flex.getRowFormatter().addStyleName(0, "historyTableHeader");
flex.setText(0, 0, "ID");
flex.setText(0, 1, "TIME);
flex.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//some code which might result into call of function B
}
}

//function A is being called multiple times by say function B

最佳答案

您已经回答了您的问题:

flex.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//some code which might result into call of function B
}
}
//function A is being called multiple times by say function B

在这种情况下,ClickHandler 存在多次。您可以将代码更改为:

private FlexTable flex = new FlexTable(); //it is a global variable
private ClickHandler handler = null;

private void A(){
flex.clear();
//
if(handler == null){
handler = new ClickHandler(){}...
flex.addHandler(handler);
}

更好:

private FlexTable flex = new FlexTable(); //it is a global variable
private HandlerRegistration handler = null;

private void A(){
flex.clear();
//
if(handler != null){
handler.removeHandler();
}
handler = flex.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//some code which might result into call of function B
}
}

但我建议在创建 FlexTable 之后直接添加处理程序,并且只添加一次。

关于java - 单击 GWT 会触发多个单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33874706/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com