我正在嘗試編寫簡單的Vaadin窗體。我想在用戶點擊按鈕後將文本從TextField寫入Label。Vaadin:將TextField中的文本設置爲按鈕後的標籤單擊
我有這樣的代碼:
package com.example;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
public class TestForm extends UI {
@WebServlet(value = "/TestForm", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = TestForm.class)
public static class Servlet extends VaadinServlet {
}
private TextField tf;
private Label lbl;
private Button btn;
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
tf = new TextField("Enter text");
lbl = new Label();
btn = new Button("Click");
btn.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
lbl.setCaption(tf.getValue());
}
});
layout.addComponent(tf);
layout.addComponent(btn);
layout.addComponent(lbl);
}
}
但是在我點擊按鈕doensn't發生與標籤,但網頁瀏覽器顯示錯誤:
通信問題 採取注未保存的數據, (SyntaxError)stack:eval @ [native code] Jda BR Zc Xc line:340:意外標記'<' - 原始JSON文本:html>
Wha我做得不好?
我創建了一個測試項目,您的代碼工作。控制檯中是否有任何堆棧跟蹤? – nexus
看到nexus的評論後,我寫了答案。沒想到親自嘗試你的代碼。如果你有一個堆棧跟蹤,它確實很有用。 –