1 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
好吧,您基本上是在 2 個(gè)面板中添加相同的按鈕 2 次,并向ClickHandler其中添加 2 。您需要像這樣更改您的代碼:
首先將您的方法更改createMyButton為靜態(tài)方法并在那里創(chuàng)建一個(gè)新對(duì)象
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;
}
}
現(xiàn)在您可以在面板中使用它:
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;
}
}
添加回答
舉報(bào)