JAVA技巧:wicket一些基礎(chǔ)控件代碼

字號:

僅限記錄!wicket對所有控件設(shè)定都存放與Server,不知好是不好,感覺有些不靠譜!
    package com.wicket.page;
    import java.awt.Graphics2D;
    import java.awt.image.renderable.RenderableImage;
    import java.io.IOException;
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.apache.wicket.Component;
    import org.apache.wicket.Page;
    import org.apache.wicket.PageParameters;
    import org.apache.wicket.behavior.IBehavior;
    import org.apache.wicket.markup.html.WebMarkupContainer;
    import org.apache.wicket.markup.html.WebMarkupContainerWithAssociatedMarkup;
    import org.apache.wicket.markup.html.WebPage;
    import org.apache.wicket.markup.html.basic.Label;
    import org.apache.wicket.markup.html.basic.MultiLineLabel;
    import org.apache.wicket.markup.html.border.Border;
    import org.apache.wicket.markup.html.border.BoxBorder;
    import org.apache.wicket.markup.html.border.Border.BorderBodyContainer;
    import org.apache.wicket.markup.html.form.Button;
    import org.apache.wicket.markup.html.form.Check;
    import org.apache.wicket.markup.html.form.CheckBox;
    import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
    import org.apache.wicket.markup.html.form.CheckGroup;
    import org.apache.wicket.markup.html.form.CheckGroupSelector;
    import org.apache.wicket.markup.html.form.DropDownChoice;
    import org.apache.wicket.markup.html.form.Form;
    import org.apache.wicket.markup.html.form.IChoiceRenderer;
    import org.apache.wicket.markup.html.form.ListChoice;
    import org.apache.wicket.markup.html.form.ListMultipleChoice;
    import org.apache.wicket.markup.html.form.PasswordTextField;
    import org.apache.wicket.markup.html.form.Radio;
    import org.apache.wicket.markup.html.form.RadioChoice;
    import org.apache.wicket.markup.html.form.RadioGroup;
    import org.apache.wicket.markup.html.form.SubmitLink;
    import org.apache.wicket.markup.html.form.TextArea;
    import org.apache.wicket.markup.html.form.TextField;
    import org.apache.wicket.markup.html.form.upload.FileUpload;
    import org.apache.wicket.markup.html.form.upload.FileUploadField;
    import org.apache.wicket.markup.html.form.validation.IFormValidator;
    import org.apache.wicket.markup.html.image.Image;
    import org.apache.wicket.markup.html.image.resource.RenderedDynamicImageResource;
    import org.apache.wicket.markup.html.include.Include;
    import org.apache.wicket.markup.html.link.BookmarkablePageLink;
    import org.apache.wicket.markup.html.link.ExternalLink;
    import org.apache.wicket.markup.html.link.IPageLink;
    import org.apache.wicket.markup.html.link.Link;
    import org.apache.wicket.markup.html.link.PageLink;
    import org.apache.wicket.markup.html.list.ListItem;
    import org.apache.wicket.markup.html.list.ListView;
    import org.apache.wicket.markup.html.list.PageableListView;
    import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
    import org.apache.wicket.markup.html.panel.ComponentFeedbackPanel;
    import org.apache.wicket.markup.html.panel.FeedbackPanel;
    import org.apache.wicket.model.CompoundPropertyModel;
    import org.apache.wicket.model.IModel;
    import org.apache.wicket.model.Model;
    import org.apache.wicket.model.PropertyModel;
    import org.apache.wicket.util.file.File;
    import org.apache.wicket.util.lang.Bytes;
    import com.wicket.model.BookModel;
    public class HelloWorldPage extends WebPage {
    private static final long SVU = 1L;
    private int count = 0;
    public HelloWorldPage(){
    //信息輸出
    Label label = new Label("message","Hello world!");
    this.add(label);
    MultiLineLabel linelabel = new MultiLineLabel("multimessage","Hello \nworld");
    this.add(linelabel);
    //容器
    WebMarkupContainerWithAssociatedMarkup panel =
    new WebMarkupContainerWithAssociatedMarkup("panel");
    Label label1 = new Label("panel_label","這是被panel容納進(jìn)來的Label!");
    panel.add(label1);
    this.add(panel);
    WebMarkupContainer webpanel =
    new WebMarkupContainer("webpanel");
    Label label2 = new Label("webpanel_label","這是被webpanel容納進(jìn)來的Label!");
    webpanel.add(label2);
    this.add(webpanel);
    Border border = new BoxBorder("border");
    Label label3 = new Label("border_label","這是被border容納的label");
    border.add(label3);
    this.add(border);
    this.add(new Include("include","inc/include.html"));
    //超級鏈接控件
    Link link = new Link("link") {@Override
    public void onClick() {
    count++;
    }};  link.add(new Label("count",new Model(){
    public String getObject(){
    return count+"";
    }
    }));
    this.add(link);
    ExternalLink externallink = new ExternalLink
    ("externallink","http://www.baidu.com","百度");
    this.add(externallink);
    PageLink pagelink = new PageLink("pagelink",new IPageLink(){
    public Class getPageIdentity() {
    return TestPageLink.class;
    }
    public Page getPage(){
    return new TestPageLink();
    }
    });
    this.add(pagelink);
    PageParameters pps = new PageParameters();
    pps.put("id", 1);
    BookmarkablePageLink bmplink =
    new BookmarkablePageLink("bookmarkablepagelink",TestPageLink.class,pps);
    this.add(bmplink);
    //表單輸入控件
    final FeedbackPanel feed = new FeedbackPanel("errmsg");
    //form表單的button組件
    Form form = new Form("myform"){
    protected void onSubmit(){
    feed.info(loadonSubmit("你提交了button表單"));
    }
    };
    Button button1 = new Button("button1"){
    public void onSubmit(){
    feed.info(loadonSubmit("你按下了button1"));
    }
    };
    Button button2 = new Button("button2"){
    public void onSubmit(){
    feed.info(loadonSubmit("你按下了button2"));
    }
    };
    form.add(button1);
    form.add(button2);
    this.add(feed);
    this.add(form);
    //form表單的Link組件
    final FeedbackPanel feed1 = new FeedbackPanel("errmsg1");
    Form linkform = new Form("myform1"){
    protected void onSubmit(){
    feed1.info(loadonSubmit("你提交了linkform表單"));
    }
    };
    SubmitLink submitlink = new SubmitLink("submit_form_link"){
    public void onSubmit(){
    feed1.info(loadonSubmit("你按下了linkform的內(nèi)部提交"));
    }
    };
    SubmitLink submitlink1 = new SubmitLink("submit_link",linkform){
    public void onSubmit(){
    feed1.info(loadonSubmit("你按下了linkform的外部提交"));
    }
    };
    linkform.add(submitlink);
    this.add(submitlink1);
    this.add(feed1);
    this.add(linkform);
    //文本輸入控件
    //只允許輸入數(shù)字,且不能為空,默認(rèn)值為"請輸入測試字符"
    TextField text = new TextField("textload",Integer.class);
    text.setLabel(new Model("請輸入測試字符"));
    text.setRequired(true);
    final ComponentFeedbackPanel feed2 = new ComponentFeedbackPanel("errmsg2",text);
    Form textform = new Form("textform"){
    protected void onSubmit(){
    feed2.info(loadonSubmit("你提交了textform表單"));
    }
    };
    textform.add(text);
    this.add(feed2);
    this.add(textform);
    //密碼輸入控件
    //只允許輸入數(shù)字,且不能為空,默認(rèn)值為"請輸入測試字符"
    PasswordTextField textpass = new PasswordTextField("text_pass");
    final FeedbackPanel feed3 = new FeedbackPanel("errmsg3");
    Form passform = new Form("passform"){
    protected void onSubmit(){
    feed3.info(loadonSubmit("你提交了passform表單"));
    }
    };
    passform.add(textpass);
    this.add(feed3);
    this.add(passform);
    //多行文本輸入
    TextArea textarea = new TextArea("textarea");
    this.add(textarea);
    //選擇框
    CheckBox checkbox = new CheckBox("checkboxbool");
    this.add(checkbox);
    //多項(xiàng)選擇框
    List list = new ArrayList();
    list.add("1");
    list.add("2");
    list.add("3");
    final HashMap map = new HashMap();
    map.put("1","語文");
    map.put("2","數(shù)學(xué)");
    map.put("3","英語");
    //實(shí)現(xiàn)IChoiceRenderer接口以保存checkbox的顯示文字與值 IChoiceRenderer renderer = new IChoiceRenderer(){
    public Object getDisplayValue(Object object){
    return map.get(object);
    }
    public String getIdValue(Object arg0, int arg1) {
    return null;
    }
    };
    CheckBoxMultipleChoice onlysites =
    new CheckBoxMultipleChoice("sites",list,renderer);
    List ALLSITES = new ArrayList();
    final Map ALLNAMES = new HashMap();
    ALLSITES.add("1");
    ALLSITES.add("2");
    ALLSITES.add("3");
    ALLNAMES.put("1", "語文");
    ALLNAMES.put("2", "數(shù)學(xué)");
    ALLNAMES.put("3", "英語");
    Form allcheckform = new Form("allcheckform");
    CheckGroup group = new CheckGroup("allcheckgroup",new ArrayList());
    group.add(new CheckGroupSelector("allcheckgroupselector"));
    ListView allsites = new ListView("allsites", ALLSITES) {
    protected void populateItem(ListItem item) {
    Object object=item.getModelObject();
    item.add(new Check("allcheck", new
    Model(Integer.toString(item.getIndex()))));
    item.add(new Label("allcheckname",new Model((Serializable) ALLNAMES.get(object))));
    }
    };
    this.add(onlysites);
    group.add(allsites);
    allcheckform.add(group);
    this.add(allcheckform);
    //下拉框
    DropDownChoice dropdownchoice =
    new DropDownChoice("dropsites",ALLSITES,renderer);
    this.add(dropdownchoice);
    //單選列表框
    ListChoice listchoice =
    new ListChoice("multiple",ALLSITES,renderer);
    this.add(listchoice);
    //單選組合框
    ListMultipleChoice alllistchoice =
    new ListMultipleChoice("allmultiple",ALLSITES,renderer);
    this.add(alllistchoice);
    RadioChoice radio =
    new RadioChoice("onlyradio",ALLSITES,renderer);
    this.add(radio);
    //單選增強(qiáng)組合框
    Form radioform = new Form("radioform");
    add(radioform);
    RadioGroup radiogroup=new RadioGroup("radiogroup");
    radioform.add(radiogroup);
    ListView radiosites = new ListView("radiosites", ALLSITES) {
    protected void populateItem(ListItem item) {
    Object object=item.getModelObject();
    item.add(new Radio("radioradio", new Model(Integer.toString(item.getIndex()))));
    item.add(new Label("radioname",new Model((Serializable) ALLNAMES.get(object))));
    };
    };
    radiogroup.add(radiosites);
    //靜態(tài)圖像
    Image staticimg = new Image("staticimg","img/mks.jpg");
    this.add(staticimg);
    //動(dòng)態(tài)圖像
    Image dynameicimg = new Image("dynameicimg",
    new RenderedDynamicImageResource(200,100){
    protected boolean render(Graphics2D graphics) {
    graphics.drawString("看看生成以后的樣子", 50, 50);
    return true;
    }
    }
    );
    this.add(dynameicimg);
    //文件上傳
    final FileUploadField fileup = new FileUploadField("upfileload");
    Form upfileform = new Form("upfileform"){
    protected void onSubmit(){
    final FileUpload upload = fileup.getFileUpload();
    if(upload!=null){
    try {
    upload.writeTo(new File("d:\\load\\1.txt"));
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    super.onSubmit();
    }
    };
    upfileform.add(fileup);
    upfileform.setMultiPart(true);
    upfileform.setMaxSize(Bytes.kilobytes(1000));
    this.add(upfileform);
    //數(shù)據(jù)列表
    ListView listView = new ListView("listView", ALLSITES) {
    protected void populateItem(ListItem item) {
    String name = (String) ALLNAMES.get(item.getModelObject());
    item.add(new Label("name", name));
    item.add(new ExternalLink("url", (String) item.getModelObject()));
    }
    };
    this.add(listView);
    }
    public final String loadonSubmit(String str){
    String backstr = "";
    if(this.getRequest().getParameter("uname") == null)
    backstr = str + ";" + "請輸入字符";
    else
    backstr = str + ";" + "你輸入的字符為:" + this.getRequest().getParameter("uname");
    return backstr;
    }
    }