这不是必需的。可以将 ListGrid 变量创建为非最终变量。
您一定看过示例,其中 ListGrid 变量被声明为 final,但出于其他原因。
例如,不可能在匿名内部类中使用非最终局部变量(在方法内声明的变量)。
因此,为了从内部类访问局部变量,需要将它们声明为 final。
在 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);
}
}
检查以下链接以获取有关在内部类中使用局部变量的更多详细信息 内部
类和局部变量
关于 Java 中的局部最终变量的问题
- “静态对象是最好的,因为我想从另一个类修改它的属性”
有比使用静态变量更好的方法在对象之间进行通信。
- “也许我不应该使用这么多类,只需将所有内容都放在入口点类 onModuleLoad() 中”
最好将 onModuleLoad() 中的代码保持在最低限度。
所需的类数量取决于您尝试实现的内容。
您不能删除 EntryPoint 实现,因为 GWT 将在此处移交执行以创建应用程序。
为此,GWT/JavaScript 引擎会调用 onModuleLoad()。
您的代码不得调用它。
浏览包含代码示例的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;
}
}