public class RightLeftAnchor { public static void main(String args[]) { Shell shell = new Shell(); shell.setText("Draw2d Hello World"); shell.setSize(400, 400); shell.open(); // create content 4 shell. createContent4Shell(shell); while (!shell.isDisposed ()) { if (!Display.getDefault().readAndDispatch ()) Display.getDefault().sleep (); } } private static void createContent4Shell(Shell shell) { Panel rootFigure = new Panel(); rootFigure.setLayoutManager(new XYLayout()); IFigure figure1 = new Ellipse(); // Label figure2 = new Label("ddddddddddddddddd"); Triangle figure2 = new Triangle(); // -------------------------------------------------------- // add connection PolylineConnection connection = new PolylineConnection(); connection.setSourceAnchor(new RightAnchor(figure1)); connection.setTargetAnchor(new LeftAnchor(figure2)); // connection.setTargetAnchor(new ChopboxAnchor(figure2)); // add connection // -------------------------------------------------------- rootFigure.add(figure1,new Rectangle(10,10,60,30)); rootFigure.add(figure2,new Rectangle(80,90,90,190)); rootFigure.add(connection); LightweightSystem lws = new LightweightSystem(shell); lws.setContents(rootFigure); } } class LeftAnchor extends AbstractConnectionAnchor { public LeftAnchor(IFigure owner) { super(owner); } public Point getLocation(Point reference) { Point p; p = getOwner().getBounds().getLeft(); getOwner().translateToAbsolute(p); return p; } } class RightAnchor extends AbstractConnectionAnchor { public RightAnchor(IFigure owner) { super(owner); } public Point getLocation(Point reference) { Point p; p = getOwner().getBounds().getRight(); getOwner().translateToAbsolute(p); return p; } }
相关推荐
其中,锚点属性(如LeftAnchor、RightAnchor、TopAnchor、BottomAnchor)用于定义控件在窗口中的定位方式。 2. **锚点类型**:控件的锚点通常有四个方向,即左、右、上、下。设置某个方向的锚点为"开",意味着当...
label.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor, constant: -padding).isActive = true ``` 这里,我们设置了label与cell.contentView的左右上边界约束,并留出了适当的内边距(padding)。 ...
view3领先.view2.rightAnchor.constraint(equalTo: parentView.trailingAnchor).isActive = true view3.topAnchor.constraint(equalTo: parentView.topAnchor).isActive = true ``` 然而,上述代码并没有实现等距离...
例如,你可以直接写`view1.leftAnchor.constraintEqualTo(view2.rightAnchor).isActive = true`来创建约束,而无需调用多个方法。 2. **方便的快捷方法**:针对常见的布局操作,如居中、对齐、等宽、等高,...
页视图的约束设置也很关键,它们的leftAnchor应与containerView的上一个页视图的rightAnchor相连,以形成连续的分页效果。同时,页视图的宽度和高度应与containerView保持一致,这样在滚动时,每个页视图都能占据一...
customButton.centerXAnchor.constraint(equalTo: navigationItem.leftAnchor).isActive = true customButton.topAnchor.constraint(equalTo: navigationBar.topAnchor, constant: 8).isActive = true ``` 这段代码...