简体   繁体   English

在Draw2D中向PolylineConnection添加标签

[英]Adding label to PolylineConnection in Draw2D

I'm trying to add a label to a PolylineConnection in Draw2d. 我正在尝试在Draw2d中为PolylineConnection添加标签。 I'm using the example in java2s as a basis. 我在java2s中使用这个例子作为基础。 The problem is that even if I can create the text by using graphics.drawText() on the paintFigure method from the PathFigure object (that extends PolylineConnection), the label is cut out most of the time, as shown in these captures: 问题是,即使我可以通过在PathFigure对象(扩展PolylineConnection)的paintFigure方法上使用graphics.drawText()来创建文本,标签也会在大多数时间被删除,如以下捕获所示:

看'完成'标签剪切

两个标签都被剪掉了

To me, it looks like the bounds of the figure are leaving part of the text outside from the paint area, as it does indeed paint correctly in diagonal arrows, which have bigger bounds. 对我来说,看起来图形的边界将部分文本留在绘画区域之外,因为它确实在具有较大边界的对角线箭头中正确绘制。

I have tried to set explicitly the bounds of the object, both in constructor and paint methods, but it seems like the PolylineConnection is ignoring them. 我试图在构造函数和绘制方法中明确设置对象的边界,但似乎PolylineConnection忽略了它们。 Any idea of how to solve this or if there is another way of achieving this kind of label? 知道如何解决这个问题,或者是否有另一种方法来实现这种标签?

Please use below figure for your connection figure. 请使用下图为您的连接数字。

import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MidpointLocator;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;

public class LabelConnectionFigure extends PolylineConnection {
    protected Label label;

    public LabelConnectionFigure() {
        setTargetDecoration(new PolygonDecoration());
        MidpointLocator labelLocator = new MidpointLocator(this, 0);
        label = new Label("1");
        label.setOpaque(true);
        add(label, labelLocator);
    }

    public void setLabelText(String labelText) {
        label.setText(labelText);
    }

    public String getLabelText() {
        return label.getText();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM