我的事情,我找到了一个解决这个问题的小方法,但是有另一个概念——只有一个类(和一个接口);在这堂课中,我有一个主要方法,它选择正确的“打开方法”;在这个例子中,我通过 int 检查:
/ -------------------------------------------------------- / 主要部分的示例循环程序:/ ------------------------------------------------------- / (. ..)
MenuBar menu = new MenuBar(true);
for (int k = 0; k < 5; k++) {
int ii=k;
menu.addItem(tekst + k, new Command() {
@Override
public void execute() {
ClassMetodInterface metoda = (ClassMetodInterface) GWT.create(Method2.class);
vPanel.clear(); //clearing before I open a new widget.
/*I sent int (easier to tests), but I can sent string[i] too*/
/* logging - instance of main class to communicate with 'subclasses', in my project necessery*/
vPanel.add(metoda.defaultMethod(ii, logging));
}
});
}
(...)
/*-------------------------------------------*/
ClassMetodInterface, only for implementation class Method2 :
/*-------------------------------------------*/
public interface ClassMetodInterface {
Widget defaultMethod(int g, Logging logging);
}
/*-------------------------------------------*/
class Method2 with swich:
/*-------------------------------------------*/
public class Method2 implements ClassMetodInterface {
public Widget zapisUsera(int g, Logging logging) {
Logging logging;
HorizontalPanel lPanel = new HorizontalPanel();
switch(g) {
case 1: {
Window.alert("switch for test no:"+g);
MenuWindow1(logging);
break;
}
case 2:{
Window.alert("switch for test no:"+g);
break;
}
default:
Window.alert("switch default - loop >2 && <1");
break;
}
return lPanel; //all methods will be void - they will be add widgets and Panels to lPanel
}
public Widget MenuWindow1(Logging logging) {
this.logging = logging;
lPanel.setBorderWidth(2);
this.lPanel.setPixelSize(100, 50);
Button b1 = new Button("test button, not used");
lPanel.add(b1);
Label lbl = new Label("label no "+logging.i);
lPanel.add(lbl);
Button addGlobal = new Button("+");
Button removeGlobal = new Button("-");
lPanel.add(addGlobal);
addGlobal.addClickHandler(new addGlobal());
removeGlobal.addClickHandler(new removeGlobal());
lPanel.add(removeGlobal);
return lPanel;
}
//for tests communication with class, where I have menu and global variable
public class addGlobal implements ClickHandler {
@Override
public void onClick(ClickEvent event) {
logging.i++;
}
}
public class removeGlobal implements ClickHandler {
@Override
public void onClick(ClickEvent event) {
logging.i--;
}
}
}
它还没有完成,但你可以展示主要思想。如果我需要向菜单添加新选项,我将更新 db 并用更多方法替换一个版本的类。