你必須創建一個容器圖形,如您有XYLayout提到和「手動」的地方,「尺寸」 2×(形狀和標籤)的孩子的身影使用IFigure.add(IFigure child, Object constraint)
方法與類型矩形的約束這種佈局內(在Draw2D)
編輯的代碼示例
這裏是你的身材類可以是什麼樣子的例子:
package draw2dtest.views;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureListener;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
public class LabeledFigure extends Figure {
private final Figure shapeFigure;
private final Label labelFigure;
private Rectangle customShapeConstraint;
public LabeledFigure(String label) {
setLayoutManager(new XYLayout());
setBackgroundColor(ColorConstants.lightGray);
setOpaque(true);
shapeFigure = new Ellipse();
this.add(shapeFigure);
shapeFigure.setBackgroundColor(ColorConstants.yellow);
shapeFigure.addMouseListener(new MouseListener.Stub() {
@Override
public void mousePressed(MouseEvent me) {
customShapeConstraint = new Rectangle(
(Rectangle) LabeledFigure.this.getLayoutManager()
.getConstraint(shapeFigure));
customShapeConstraint.width -= 6;
customShapeConstraint.x += 3;
LabeledFigure.this.getLayoutManager().setConstraint(
shapeFigure, customShapeConstraint);
LabeledFigure.this.revalidate();
}
});
labelFigure = new Label(label);
labelFigure.setOpaque(true);
labelFigure.setBackgroundColor(ColorConstants.green);
labelFigure.addMouseListener(new MouseListener.Stub() {
@Override
public void mousePressed(MouseEvent me) {
Rectangle shapeFigureConstraint = new Rectangle(0, 0,
bounds.width, bounds.height - 15);
LabeledFigure.this.getLayoutManager().setConstraint(
shapeFigure, shapeFigureConstraint);
LabeledFigure.this.revalidate();
}
});
this.add(labelFigure);
this.addFigureListener(new FigureListener() {
@Override
public void figureMoved(IFigure source) {
Rectangle bounds = LabeledFigure.this.getBounds();
Rectangle shapeFigureConstraint = new Rectangle(0, 0,
bounds.width, bounds.height - 15);
LabeledFigure.this.getLayoutManager().setConstraint(
shapeFigure, shapeFigureConstraint);
Rectangle labelFigureConstraint = new Rectangle(0,
bounds.height - 15, bounds.width, 15);
if (customShapeConstraint != null) {
labelFigureConstraint = customShapeConstraint;
}
LabeledFigure.this.getLayoutManager().setConstraint(
labelFigure, labelFigureConstraint);
}
});
}
}
這不是一個乾淨的類,但它應該是一個不錯的入門展示你好w實現你的目標。這是一個基於沒有任何Gef代碼的純Draw2d的示例,因此通過單擊黃色橢圓(尺寸減小)和綠色標籤(初始尺寸恢復)來完成形狀的調整大小。
To測試這個類我創建了一個簡單的Eclipse視圖如下:
@Override
public void createPartControl(Composite parent) {
FigureCanvas fc = new FigureCanvas(parent, SWT.DOUBLE_BUFFERED);
fc.setBackground(ColorConstants.red);
Panel panel = new Panel();
panel.setLayoutManager(new XYLayout());
LabeledFigure labeledFigure = new LabeledFigure("This is the label");
fc.setContents(panel);
panel.add(labeledFigure, new Rectangle(10,10, 200,100));
}
希望這可以幫助, 馬努
感謝您的回答,但是我不確定XYLayout是否能比我已經嘗試過的佈局(DelegatingLayout和BorderLayout)更好地工作。希望的行爲是用戶可以調整子形狀圖的大小而不影響標籤圖的大小。 – Phillipus 2011-03-15 14:43:40
我添加了代碼示例來說明 – 2011-03-15 15:41:48
感謝您花時間創建代碼示例。不幸的是,它不能解決問題。我正在尋找一種解決方案,即調整圖形大小來調整形狀圖形,但標籤的大小與標籤中的文本保持一致(與寬度相同)。 – Phillipus 2011-03-15 16:40:26