【发布时间】:2014-08-02 07:03:40
【问题描述】:
我正在尝试将文本放置在 RectangleFigure 的中心。我需要根据图形的边界来环绕单词,所以我使用的是文本流。问题是尽管设置了 RectangleFigure 的边界,但它占据了整个外壳,并且文本流环绕外壳的宽度而不是 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);
}
}
【问题讨论】:
标签: java eclipse user-interface swt draw2d