如何将标签置于矩形图形的中心

发布于 2024-12-05 06:18:25 字数 1604 浏览 2 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不会写诗 2024-12-12 06:18:25
  1. 您可以将矩形的布局设置为 BorderLayout,然后将标签的约束设置为 CENTER。这应该自动处理居中。
  2. 您可以添加 MarginBorder 来填充数字。
  3. 使用上面的两个答案可能会改进您的代码:-)
  1. You could set the layout of the rectangle to BorderLayout and then set the Label's constraint to CENTER. This should handle the centering automatically.
  2. You can add a MarginBorder to pad your figures.
  3. Using the two answers above will probably improve your code :-)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文