Draw2D里的Label不支持自动换行,虽然可以插入换行符手动换行。用TextFlow和适当的Layout可以实现文字的自动换行。以下代码由sean朋友贡献,原文链接。
class
LabelEx
extends
FlowPage {
private TextFlow contents;
public LabelEx() {
this ( "" );
}
public LabelEx(String text) {
contents = new TextFlow();
contents.setLayoutManager( new ParagraphTextLayout(contents, ParagraphTextLayout.WORD_WRAP_SOFT));
contents.setText(text);
add(contents);
}
public void setText(String text) {
contents.setText(text);
}
public String getText() {
return contents.getText();
}
}
private TextFlow contents;
public LabelEx() {
this ( "" );
}
public LabelEx(String text) {
contents = new TextFlow();
contents.setLayoutManager( new ParagraphTextLayout(contents, ParagraphTextLayout.WORD_WRAP_SOFT));
contents.setText(text);
add(contents);
}
public void setText(String text) {
contents.setText(text);
}
public String getText() {
return contents.getText();
}
}