在Draw2d中,IFigure对象代表图形,IFigure对象还能包含其他的IFigure对象,所有的IFigure对象组合成用户指定的图形
通常用户可以继承Figure类实现自己的图形。Figure实现了IFigure接口
我下面写了个实例,实现了一个Figure,并设置边框为 LineBorder(边框类似为连线),而且还给它加了颜色,好像是淡绿色,对颜色没啥研究,另外还添加了以个Label作为显示的Figure,并设置为Label的现实文字和文字对齐方式,以及Figure的布局
- package com.heming.table.editor.figure;
- import org.eclipse.draw2d.ColorConstants;
- import org.eclipse.draw2d.Figure;
- import org.eclipse.draw2d.FlowLayout;
- import org.eclipse.draw2d.Label;
- import org.eclipse.draw2d.LineBorder;
- import org.eclipse.draw2d.PositionConstants;
- import org.eclipse.swt.graphics.Image;
- /**
- * Figure功能
- * @author 何明
- *
- */
- public class KCGCommonFigure extends Figure {
- //在Figure中添加标签(标签也是Figure)
- protected Label label = new Label();
- public KCGCommonFigure(String fieldLabel, Image typeIcon){
- //设置背景色
- setBackgroundColor(ColorConstants.tooltipBackground);
- //设置前景色
- setForegroundColor(ColorConstants.tooltipForeground);
- //设置Figure的边框,并给边框加上颜色
- setBorder(new LineBorder(ColorConstants.cyan));
- //设置label中文字的对齐方式
- label.setTextAlignment(PositionConstants.LEFT);
- //设置label的图标
- label.setIcon(typeIcon);
- //设置Figure的布局方式
- setLayoutManager(new FlowLayout());
- //把label添加到Figure中
- add(label);
- //设置label的显示文字
- setLabelText(fieldLabel);
- }
- private void setLabelText(String fieldLabel) {
- label.setText(fieldLabel);
- }
- }
接下来就是测试了
在写这篇文章之前,写了个HelloWorld,咱们就拿它来测试了
只需要将
在写这篇文章之前,写了个HelloWorld,咱们就拿它来测试了
只需要将
- IFigure label = new Label("Hello World");
- lws.setContents(label);
换成
- KCGCommonFigure figure = new KCGCommonFigure("Figure Demo",SWTResourceManager.getImage("icons/methpub_obj.gif"));
- lws.setContents(figure);
6075

被折叠的 条评论
为什么被折叠?



