将文本流的边界限制为图Draw2D SWT

时间:2014-08-02 07:03:41

标签: java eclipse user-interface swt draw2d

我试图将文本放在RectangleFigure的中心。我需要根据图形的边界来包装,所以我使用文本流程。问题是尽管设置了RectangleFigure的边界,但它占据了整个shell,文本流包裹着shell的宽度而不是RectangleFigure。有人可以指出我正确的方向来解决这个问题吗?谢谢。 *我添加了简短的可测试代码来说明我的问题。

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.SimpleRaisedBorder;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.BlockFlow;
import org.eclipse.draw2d.text.BlockFlowLayout;
import org.eclipse.draw2d.text.FlowFigureLayout;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.draw2d.text.TextLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
/**
 * PROBLEM: Cannot restrict bounds of text flow to a particular rectangle
 *
 */

public class TestFlow {

    protected Shell shell;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            TestFlow window = new TestFlow();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");

        shell.setLayout(new FillLayout());

        FigureCanvas canvas = new FigureCanvas(shell);
        canvas.getViewport().setContentsTracksWidth(true);
        Figure panel = new Figure();
        panel.setLayoutManager(new StackLayout());

        RectangleFigure rect=new RectangleFigure();
        rect.setBounds(new Rectangle(10, 10, 5, 25));
        rect.setBorder(new SimpleRaisedBorder());
        rect.setLayoutManager(new StackLayout());

        TextFlow content= new TextFlow("I'm such a long sentence --> You don't even know what hit you" +
                " Can't help it if there's no one else.");
        FlowPage fp = new FlowPage();

        fp.setBorder(new SimpleRaisedBorder());
        fp.setHorizontalAligment(PositionConstants.CENTER);// ALIGNS THE WHOLE THING CENTRE

        fp.add(content);
        rect.add(fp);
        panel.add(rect);
        //panel.add(fp);
        canvas.setContents(panel);

    }

}

1 个答案:

答案 0 :(得分:0)

首先感谢您的代码,它帮助我找出了我自己的问题(在图中添加文本)。现在回答你的问题:

在第一个中使用另一个矩形图。第一个因某种原因总是占据整个空间。我添加了一些应该解释它的代码,无论你设置的是什么大小,第一个矩形都会被调整大小。我不久前在创建日食视图时遇到了这个问题。

...

RectangleFigure rect=new RectangleFigure();
rect.setBounds(new Rectangle(10, 10, 5, 25));
rect.setBorder(new SimpleRaisedBorder());
rect.setLayoutManager(new StackLayout());

RectangleFigure rect2=new RectangleFigure();
rect2.setBounds(new Rectangle(10, 10, 5, 25));
rect2.setBorder(new SimpleRaisedBorder());
rect2.setLayoutManager(new StackLayout());

...

fp.add(content);
rect2.add(fp);
rect.add(rect2);
panel.add(rect);