1 回答
TA贡献1946条经验 获得超3个赞
好吧,您基本上是在 2 个面板中添加相同的按钮 2 次,并向ClickHandler其中添加 2 。您需要像这样更改您的代码:
首先将您的方法更改createMyButton为静态方法并在那里创建一个新对象
class MyButton {
public MyButton(){};
public static Button createMyButton(){
MyButton button = new MyButton();
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
createAlert("I'm button");
}
});
return button;
}
}
现在您可以在面板中使用它:
class PanelFirst {
private static FlowPanel firstPanel;
public PanelFirst (){};
public FlowPanel createPanelFirst(){
firstPanel = new FlowPanel();
firstPanel.add(MyButton.createMyButton());
return firstPanel;
}
}
class PanelSecond {
private static FlowPanel secondPanel;
public PanelSecond(){};
public FlowPanel createPanelSecond(){
secondPanel= new FlowPanel();
secondPanel.add(MyButton.createMyButton());
return secondPanel;
}
}
添加回答
举报