如何将标签置于矩形图形的中心
我是draw2d的完全新手,我正在尝试制作一些示例以学习如何使用它...我正在尝试编写一个图,其中显示带有白色背景、一些填充和封闭的标签灰色背景。为此,我写了以下内容:
public class MyFigure extends Figure {
private Label label;
private RectangleFigure rectangle;
public MyFigure() {
rectangle = new RectangleFigure();
rectangle.setBackgroundColor(ColorConstants.gray);
label = new Label("Test label");
label.setFont(new Font(null, "Arial", 12, SWT.BOLD));
label.setTextAlignment(PositionConstants.CENTER);
label.setBackgroundColor(ColorConstants.white);
label.setForegroundColor(ColorConstants.black);
label.setOpaque(true);
rectangle.add(label);
this.add(rectangle);
}
protected void paintFigure(Graphics graphics) {
// Add padding to the label
label.setSize(label.getTextBounds().resize(35, 10).getSize());
// Set rectangle size, with some margin around the label
Dimension d = label.getSize().expand(40, 10);
rectangle.setSize(d);
// Center the label inside the rectangle
label.setLocation(rectangle.getLocation().translate(20, 5));
// Finally, set this figure's bounds
this.setBounds(rectangle.getBounds());
super.paintFigure(graphics);
}
}
我的类扩展了Figure而不是RectangleFigure,因为我想稍后添加一个ToolbarLayout,以在RectangleFigure+Label下方添加内容...现在我的问题:
- 有没有办法将图形(标签)居中它的父级(RectangleFigure)没有像我一样计算位置?
- 有没有办法向数字添加“填充”(导致类似于我在标签上设置的大小)或“边距”(从其子项和边距自动计算的大小?
- 一般来说,是否有更好的方法来实现我正在尝试做什么?
我在 api 文档中找不到任何方法,并且总体上缺少一些文档...提前谢谢您。
I'm a complete newbie with draw2d, i'm trying to make some examples in order to learn how to use it... I'm trying to write a Figure which shows a Label with white background, some padding, and an enclosing grey background. For that, i wrote the following:
public class MyFigure extends Figure {
private Label label;
private RectangleFigure rectangle;
public MyFigure() {
rectangle = new RectangleFigure();
rectangle.setBackgroundColor(ColorConstants.gray);
label = new Label("Test label");
label.setFont(new Font(null, "Arial", 12, SWT.BOLD));
label.setTextAlignment(PositionConstants.CENTER);
label.setBackgroundColor(ColorConstants.white);
label.setForegroundColor(ColorConstants.black);
label.setOpaque(true);
rectangle.add(label);
this.add(rectangle);
}
protected void paintFigure(Graphics graphics) {
// Add padding to the label
label.setSize(label.getTextBounds().resize(35, 10).getSize());
// Set rectangle size, with some margin around the label
Dimension d = label.getSize().expand(40, 10);
rectangle.setSize(d);
// Center the label inside the rectangle
label.setLocation(rectangle.getLocation().translate(20, 5));
// Finally, set this figure's bounds
this.setBounds(rectangle.getBounds());
super.paintFigure(graphics);
}
}
My class extends Figure instead RectangleFigure because i want to add a ToolbarLayout later, to add things below the RectangleFigure+Label... Now my questions:
- Is there any way to center a figure (Label) inside its parent (RectangleFigure) without calculating the position as i do?
- Is there any way to add "padding" to figures (resulting in something like the size i set on the Label) or "margin" (size calculated automatically from its children and a margin?
- Is there, in general, a better way to implement what i'm trying to do?
I can't find any method in the api docs, and there's some lack of documentation in general... Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BorderLayout
,然后将标签的约束设置为CENTER
。这应该自动处理居中。MarginBorder
来填充数字。BorderLayout
and then set the Label's constraint toCENTER
. This should handle the centering automatically.MarginBorder
to pad your figures.