我意识到ListGrid必须永远是最终的。对我来说,静态对象是最好的,因为我想从另一个类修改它的属性。
在我的项目中,为了有一个清晰的代码,我创建了几个类。
DataGrid extends ListGrid,它设置新对象的属性并填充数据。@重写方法将按钮添加到网格对象中。PopupWindow extend Window,它用于在单击ListGrid中的编辑按钮时创建一个窗口对象。在窗口中有一些文本框,您可以在其中添加新数据和提交按钮。Submit按钮的OnClick事件将将数据写入MySQL服务器,应该使用实际的数据(来自MySQL的查询)更新网格。这是我无法实现的部分。我是java中的新手,也许我不应该使用这么多类,只需将所有内容都放在入口点类onModuleLoad()中即可。
如果在PopupWindow扩展窗口类中,我声明Button OnClick从Entry Point类运行onModuleLoad() methode,这会复制我的网页吗?我是说这么做
EntryPointClass ep = new EntryPointClass();
ep.onModuleLoad();发布于 2013-05-01 21:04:20
这是不必要的。可以将ListGrid变量创建为非最终变量。
您一定看到过一些示例,其中ListGrid变量被声明为最终变量,但由于其他原因。
例如,不可能在匿名内部类中使用非最终局部变量(在方法中声明的局部变量)。
因此,为了从内部类访问局部变量,需要将它们声明为最终变量。
在SmartGWT/Swing/等等中,内部类用于实现各种回调功能,如事件处理。
public class Screen {
ListGrid grid1 = new ListGrid();
TextItem text1 = new TextItem("text1", "Text 1");
public void initialize() {
// normally its not required to create subclasses of ListGrid/Button/Window/etc.
// unless a significant change in their behavior is needed
ListGrid grid2 = new ListGrid();
// setup grid properties
// set grid fields
TextItem text2 = new TextItem("text2", "Text 2");
final ListGrid grid3 = new ListGrid();
final TextItem text3 = new TextItem("text3", "Text 3");
IButton button = new IButton("Edit");
button.addClickHandler(new ClickHandler() { // this is declaring an anonymous inner class
public void onClick(ClickEvent clickEvent) { // this method is a member of that anonymous inner class
// out of three ListGrid and thee TextItem instances, only following can be accessed in this method/class
// grid1, text1 (these are not local variables, inner class can access outer class members without any issue)
// grid3, text3 (as they are final, even though local variables)
}
});
// that does not mean, grid2 and text2 can not be used, they can be, just not inside an anonymous inner class
// e.g.-
DynamicForm form = new DynamicForm();
form.setFields(text2);
VLayout layout = new VLayout();
layout.addMember(grid2);
}
}有关在内部类中使用局部变量的详细信息,请参阅以下链接
在对象之间有比使用静态变量更好的通信方式。
最好将onModuleLoad()中的代码保持在最小值。
所需类的数量取决于您试图实现什么。
您不能删除EntryPoint实现,因为这是GWT将切换执行以创建应用程序的地方。
onModuleLoad()由GWT/JavaScript引擎为此调用。
您的代码不能调用它。
查看SmartGWT橱窗,包括代码示例。
有关更多细节,请参阅SmartGWT API。
创建UI的方法有多种,以实现相同的结果。
与服务器通信以在SmartGWT中发送/接收数据是其本身的一个主题。
可能的实施准则。
public class EntryPointClass implements EntryPoint {
public void onModuleLoad() {
ApplicationScreen screen = new ApplicationScreen();
HStack drawArea = new HStack();
drawArea.setWidth100();
drawArea.setHeight100();
drawArea.addMember(screen.getComponents());
drawArea.draw();
}
}
public class ApplicationScreen { // this class does not need to extend from a widget
public Canvas getComponents() {
// a method that prepares the interface
// using a from+grid type layout, without a popup window
ListGrid grid = getListGrid();
DynamicForm form = getDynamicForm(grid); // have to pass grid in order to add/update records on button events
VLayout layout = new VLayout();
layout.addMember(form);
layout.addMember(grid);
return layout;
}
private DynamicForm getDynamicForm(final ListGrid grid) { // have to declare grid as final to access from event handler inner classes
final TextItem text1 = new TextItem("text1", "Text 1"); // have to declare as final for same reason
ButtonItem saveButton = new ButtonItem("Save");
saveButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent clickEvent) {
// use text1, grid and other components to save form values and refresh grid
}
});
// creating and configuring form
DynamicForm form = new DynamicForm();
form.setWidth100();
form.setFields(text1, saveButton);
return form;
}
private ListGrid getListGrid() {
// preparing grid fields
ListGridField field1 = new ListGridField("field1", "Field 1");
// creating and configuring grid
ListGrid grid = new ListGrid(); // not final, does not need to be
grid.setWidth100();
grid.setFields(field1);
return grid;
}
}https://stackoverflow.com/questions/16313936
复制相似问题