![](/img/trans.png)
[英]How to pass class-name as arguments and invoke common static methods in GWT jsni?
[英]How to create new object in GWT by string class-name?
我的目标是创建一个灵活的应用程序 - >扩展我的Web应用程序的功能我只是为数据库添加一个位置并将缺少的类上传到服务器。
我在Java-GWT中创建了动态菜单。 我使用MenuBar()和循环,所有位置,wchich都是从db(数组,字符串)中获取的。 当然,菜单中的'end-option'必须做一些事情,一般在点击应用程序后会打开,即带有按钮,标签,textareas的FlowPanel。
我的想法是:使用java反射为菜单中的所有位置添加Command。 每个Command取名称选项(字符串),名称相同,如类名称 - >接下来,点击后我会自动创建对象。 不幸的是,Java Reflection不能与GWT一起使用,所以这种方式是不可能的。
我的问题是:
当我在字符串中有类名时,如何创建对象? 或者,如果我的想法是错误的,maybye是另一种创建灵活菜单/程序的方法吗?
我的事情,我找到了一个小问题的解决方案,但有另一个概念 - 只有一个类(和一个接口); 在这个课程中,我有一个主要的方法,选择正确的'打开方法'; 在这个例子中,我通过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并使用更多方法替换一个类的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.