我尝试使用对话框中的文本框子项为Layoutpanel设置动画。当我在一个简单的类/视图中测试它时,我能够做到这一点(即动画)但是当我在对话框中导入相同的代码时,动画不再有效。
基本上,我试图完成的动画有点像抽屉,当用户点击复选框时,我的文本框所在的布局面板,隐藏或取消隐藏(如抽屉),具体取决于复选框。以下是我的代码:
public class Testingwork implements EntryPoint {
private final GreetingServiceAsync greetingService = GWT
.create(GreetingService.class);
private static final String SERVER_ERROR = "An error occurred while "
+ "attempting to contact the server. Please check your network "
+ "connection and try again.";
public void onModuleLoad() {
final Button sendButton = new Button("Send");
final TextBox nameField = new TextBox();
nameField.setText("GWT User");
final Label errorLabel = new Label();
// We can add style names to widgets
sendButton.addStyleName("sendButton");
// Add the nameField and sendButton to the RootPanel
// Use RootPanel.get() to get the entire body element
RootPanel.get("nameFieldContainer").add(nameField);
RootPanel.get("sendButtonContainer").add(sendButton);
RootPanel.get("errorLabelContainer").add(errorLabel);
// Focus the cursor on the name field when the app loads
nameField.setFocus(true);
nameField.selectAll();
// Create the popup dialog box
final DialogBox dialogBox = new DialogBox();
dialogBox.setText("Remote Procedure Call");
dialogBox.setAnimationEnabled(true);
final Button closeButton = new Button("Close");
// We can set the id of a widget by accessing its Element
closeButton.getElement().setId("closeButton");
final Label textToServerLabel = new Label();
final HTML serverResponseLabel = new HTML();
VerticalPanel dialogVPanel = new VerticalPanel();
dialogVPanel.addStyleName("dialogVPanel");
dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
dialogVPanel.add(textToServerLabel);
dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
dialogVPanel.add(serverResponseLabel);
dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
dialogVPanel.add(closeButton);
final CheckBox danCheck2 = new CheckBox("Dan2");
final LayoutPanel testPanel2 = new LayoutPanel(); //-----------> LayoutPanel
final HorizontalPanel cont2 = new HorizontalPanel();
final TextBox tb12 = new TextBox();
final TextBox tb22 = new TextBox();
final TextBox tb32 = new TextBox();
dialogVPanel.add(danCheck2);
testPanel2.add(cont2);
cont2.add(tb12);
cont2.add(tb22);
cont2.add(tb32);
dialogBox.setWidget(dialogVPanel);
// Add a handler to close the DialogBox
closeButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
dialogBox.hide();
sendButton.setEnabled(true);
sendButton.setFocus(true);
}
});
// Create a handler for the sendButton and nameField
class MyHandler implements ClickHandler, KeyUpHandler {
/**
* Fired when the user clicks on the sendButton.
*/
public void onClick(ClickEvent event) {
if (event.getSource().equals(sendButton)) {
sendNameToServer();
}
else if(event.getSource().equals(danCheck2)) {
danMethod2();
}
}
/**
* Fired when the user types in the nameField.
*/
public void onKeyUp(KeyUpEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
sendNameToServer();
}
}
/**
* Send the name from the nameField to the server and wait for a response.
*/
private void sendNameToServer() {
// First, we validate the input.
errorLabel.setText("");
String textToServer = nameField.getText();
if (!FieldVerifier.isValidName(textToServer)) {
errorLabel.setText("Please enter at least four characters");
return;
}
// Then, we send the input to the server.
sendButton.setEnabled(false);
textToServerLabel.setText(textToServer);
serverResponseLabel.setText("");
greetingService.greetServer(textToServer,
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
dialogBox
.setText("Remote Procedure Call - Failure");
serverResponseLabel
.addStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(SERVER_ERROR);
dialogBox.center();
closeButton.setFocus(true);
}
public void onSuccess(String result) {
dialogBox.setText("Remote Procedure Call");
serverResponseLabel
.removeStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(result);
dialogBox.center();
closeButton.setFocus(true);
}
});
}
private void danMethod2(){
if (danCheck2.getValue()){
System.out.println("[dan]: chbCrossReferencePnr2 = Checked!");
testPanel2.setWidgetTopHeight(cont2, 0, PX, 60, PX);
// testPanel2.setHeight("60px");
testPanel2.animate(500);
}
else {
System.out.println("[dan]: chbCrossReferencePnr2 = unChecked!");
testPanel2.setWidgetTopHeight(cont2, 0, PX, 0, PX);
// testPanel2.setHeight("0px");
testPanel2.animate(500);
}
}
}
// Add a handler to send the name to the server
MyHandler handler = new MyHandler();
sendButton.addClickHandler(handler);
danCheck2.addClickHandler(handler);
nameField.addKeyUpHandler(handler);
}
}
我在课堂上对此进行了测试;它的工作原理。但是当我在对话框中导入它时,我的layoutpanel(以及里面的文本框)不再显示。
你能告诉我我在这里做错了什么吗?我可能刚刚在对话框实现中遗漏了一些内容,因为我的代码在类中工作。
提前致谢!
答案 0 :(得分:0)
您的LayoutPanel永远不会添加到另一个小部件。