先写一个MailInfoPanel.html.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<wicket:panel>
<span wicket:id="receivedMail">收到的邮件数量</span>
<br/>
<span wicket:id="deletedMail">删除的邮件数量</span>
<br/>
<span wicket:id="innerMail">总的数量</span>
</wicket:panel>
</html>
接着编写MailInfoPanel.java
package net.kentop.wicket;
import java.text.MessageFormat;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
public class MailInfoPanel extends Panel {
private Label receivedMailLabel;
private Label deletedMailLabel;
private Label innerMailLabel;
public MailInfoPanel(String id) {
super(id);
receivedMailLabel = new Label("receivedMail","");
deletedMailLabel = new Label("deletedMail","");
innerMailLabel = new Label("innerMail","");
this.add(receivedMailLabel);
this.add(deletedMailLabel);
this.add(innerMailLabel);
}
protected void onBeforeRender() {
Object[] objs = new Object[]{new Integer(11),new Integer(22),new Integer(33)};
receivedMailLabel.setModelObject(MessageFormat.format("收到的邮件数量为{0}件", objs));
deletedMailLabel.setModelObject(MessageFormat.format("删除的邮件数量为{1}件", objs));
innerMailLabel.setModelObject(MessageFormat.format("总的数量是{2}件", objs));
// TODO Auto-generated method stub
super.onBeforeRender();
}
}
注意红色部分为重写Panel的onBeforeRender方法.在载入MailInfoPanel之前初始化的代码,也就是说,程序会先执行这里的 onBeforeRender()方法,以初始化这里的值.
这样一个自定义的控件就已经定义好了.
就可以在页面中像使用Label 控件那样使用这个控件了.