以下代码继续给我:
不能引用在不同方法中定义的内部类中的非final变量textBoxPassword Login.java / ChurchWebLogin / src / com / gwt / churchweb / churchweblogin / client line 61
package com.gwt.churchweb.churchweblogin.client;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.Window;
public class Login extends Composite {
public Login() {
VerticalPanel verticalPanel = new VerticalPanel();
initWidget(verticalPanel);
verticalPanel.setSize("329px", "186px");
Label lblNewLabel = new Label("Sign into your account");
lblNewLabel.setStyleName("gwt-Login-SigninLabel");
verticalPanel.add(lblNewLabel);
FlexTable flexTable = new FlexTable();
verticalPanel.add(flexTable);
flexTable.setWidth("308px");
Label lblNewLabel_1 = new Label("Username:");
lblNewLabel_1.setStyleName("gwt-Label-Login");
flexTable.setWidget(0, 0, lblNewLabel_1);
lblNewLabel_1.setWidth("72px");
TextBox textboxUsername = new TextBox();
textboxUsername.setStyleName("gwt-LoginTextBox");
flexTable.setWidget(0, 1, textboxUsername);
textboxUsername.setWidth("204px");
Label lblNewLabel_2 = new Label("Password:");
lblNewLabel_2.setStyleName("gwt-Label-Login");
flexTable.setWidget(1, 0, lblNewLabel_2);
lblNewLabel_2.setWidth("66px");
TextBox textBoxPassword = new TextBox();
textBoxPassword.setStyleName("gwt-LoginTextBox");
flexTable.setWidget(1, 1, textBoxPassword);
textBoxPassword.setWidth("204px");
flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
flexTable.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT);
CheckBox chckbxRememberMeOn = new CheckBox("Remember me on this computer");
chckbxRememberMeOn.setStyleName("gwt-Checkbox-Login");
flexTable.setWidget(2, 1, chckbxRememberMeOn);
Button btnSignIn = new Button("Sign In");
btnSignIn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (textboxUsername.getText().length() == 0
|| textBoxPassword.getText().length() == 0) {
Window.alert("Username or password is empty.");
}
}
});
btnSignIn.setStyleName("gwt-Login-SigninButton");
flexTable.setWidget(3, 1, btnSignIn);
}
}
答案 0 :(得分:6)
final TextBox textboxUsername = new TextBox();
和
final TextBox textBoxPassword = new TextBox();
应该修复它。
Java要求将内部类中的变量引用为final
变量(出于并发管理的原因,我想)。
答案 1 :(得分:4)
如果在内部类中使用局部变量,则必须声明它们final
。你应该写:
final TextBox textBoxPassword = new TextBox();
答案 2 :(得分:2)
匿名类实例应该能够访问周围范围的本地 变量和参数。但是,实例可能比它的方法更长 构思(由于将实例的引用存储在字段中),并尝试 访问方法返回后不再存在的局部变量和参数。
因为Java不允许这种非法访问,这很可能会使虚拟机崩溃 机器,它允许匿名类实例只访问局部变量和 声明为final的参数。遇到最终的局部变量/参数 在匿名类实例中,编译器执行以下两种操作之一: