TextField、TextArea也是SWT里常用的控件,接下来我们来模拟它们。
效果图:
上面是TextField,下面是TextArea。
TextField就比较简单了,给Label设一个边框,修改一下背景色,然后绘制一下文字就行了。
TextFieldFigure.java
public class TextFieldFigure extends Label {
public TextFieldFigure() {
this("");
}
public TextFieldFigure(String text) {
super(text);
setMinimumSize(new Dimension(FigureConstants.COMBOBOX_DEFAULT_WIDTH,
FigureConstants.COMBOBOX_FIXED_HEIGHT));
LineBorder lineBorder = new LineBorder();
lineBorder.setColor(ResourceConstants.BORDER_COLOR);
setBorder(lineBorder);
}
/* (non-Javadoc)
* @see org.eclipse.draw2d.Figure#setBounds(org.eclipse.draw2d.geometry.Rectangle)
*/
@Override
public void setBounds(Rectangle rect) {
rect.height = FigureConstants.COMBOBOX_FIXED_HEIGHT;
super.setBounds(rect);
}
/* (non-Javadoc)
* @see org.eclipse.draw2d.Label#paintFigure(org.eclipse.draw2d.Graphics)
*/
@Override
protected void paintFigure(Graphics graphics) {
super.paintFigure(graphics);
Rectangle bound = getBounds();
graphics.setBackgroundColor(ColorConstants.white);
graphics.fillRectangle(bound.x,bound.y, bound.width, bound.height);
graphics.drawText(getText(), bound.x + 2, bound.y + 4);
}
/* (non-Javadoc)
* @see org.eclipse.draw2d.Label#setText(java.lang.String)
*/
@Override
public void setText(String s) {
super.setText(s);
repaint();
}
}
不解释了。
TextAreaFigure就比较麻烦了,主要是右边的滚动条,还需要根据大小来调整,不过自Draw2D,绘制功能尤其强大,有draw就足够实现它了。
右边分为三部分,上下是箭头按钮,图片模拟,中间为滚动区域,填充颜色就行。
TextAreaFigure.java:
public class TextAreaFigure extends Label{
private static final Image UP = createImage("icons/textarea_1.gif");
private static final Image DOWN = createImage("icons/textarea_2.gif");
private static Image createImage(String name) {
InputStream stream = TextAreaFigure.class.getResourceAsStream(name);
Image image = new Image(null, stream);
try {
stream.close();
} catch (IOException ioe) {
}
return image;
}
/**
*
*/
public TextAreaFigure() {
LineBorder lineBorder = new LineBorder();
lineBorder.setColor(ResourceConstants.BORDER_COLOR);
setBorder(lineBorder);
}
/* (non-Javadoc)
* @see org.eclipse.draw2d.Label#paintFigure(org.eclipse.draw2d.Graphics)
*/
@Override
protected void paintFigure(Graphics graphics) {
super.paintFigure(graphics);
Rectangle bound = getBounds();
graphics.setBackgroundColor(ColorConstants.white);
graphics.fillRectangle(FigureUtils.adjustForLineBorder(bound));
graphics.setBackgroundColor(ColorConstants.button);
graphics.fillRectangle(bound.x - UP.getBounds().width + bound.width, bound.y + 2, UP.getBounds().height, bound.height-2);
graphics.drawImage(UP,bound.x - UP.getBounds().width -2 + bound.width, bound.y + 2);
graphics.drawImage(DOWN,bound.x - UP.getBounds().width - 2 + bound.width, bound.y + bound.height - UP.getBounds().height );
}
}
图片见附件。其他的就不解释了。
分享到:
相关推荐
对于swt控件知识的部分整理,有助于对其的学习。
NULL 博文链接:https://as3.iteye.com/blog/910354
EXT表单验证之TextField,vtype 包括所有EXT中TextField的验证方式
一套完整的自定义的qmlGUI控件,功能强大完全可以拿去开发项目而不用重复造轮子。包括Button,Calendar,CheckBox,ComboBox,CtrlDialog,DateEdit,DatePicker,Dialog,FileDialog,ImageButton,Label,LineEdit...
无论哪种界面框架输入文本框都是非常重要的控件, 但是发现flutter中的输入框TextField介绍的虽然多,但是各个属性怎么组合满足需要很多文章却说不清楚, 再加上控件版本变更频繁很多功能的介绍都是比较陈旧的属性....
textfield控件的实时监听
Swift 几个简单控件 以及 上传系统相册照片
TextField多行输入(自动换行),小惊奇。仅仅只是导入了一个字体文件
自定义textfield
NULL 博文链接:https://lovebeyond.iteye.com/blog/440899
其中包含五个项目,是博客《解读 LWUIT 之五:LWUIT 控件(下)》中作者写的 Hello TabbedPane、Hello TextArea、Hello TextField、Hello Calendar、Hello Tickering 源代码。博客链接地址:...
java panel frame button textfield textarea
Flutter文本框TextField,是一个输入文本的控件,,,,
以前有许多人需要将 tree 的节点拖拽到其他控件中的问题,前段时间已经解决了,现在也将它的源代码贡献上来。 这个树的数据是静态的,没有做动态的,而且树节点的拖拽也没有做过多的处理,如果要知道怎么样做树...
8-22textfield8-22textfield8-22textfield8-22textfield
ExtJs5 去掉textfield边框
手工创建TextField,TextField的属性基本上上面都描述了,以及键盘的回调方法
TextField 自动换行
TextField-InputView, UITextField InputView模式:终结注册类、信息采集类