public class BenchmarkTextPackage {
static class StyledLine extends Figure {
Dimension pref;
private String text;
public Dimension getPreferredSize(int wHint, int hHint) {
if (pref == null)
pref = FigureUtilities.getTextExtents(text, getFont());
return pref;
}
protected void paintFigure(Graphics graphics) {
graphics.setFont(getFont());
graphics.drawText(text, bounds.x, bounds.y);
}
public void setText(String text) {
this.text = text;
pref = null;
revalidate();
}
public String getText() {
return text;
}
}
static Font BIG = new Font(null, "Times", 18, SWT.BOLD);
static StyledLine typing;
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell(SWT.SHELL_TRIM);
shell.setLayout(new FillLayout());
//ImageData data = new ImageData(StyleLines.class.getResourceAsStream("class.gif"));
// final Image image = new Image(null, data, data.getTransparencyMask());
FigureCanvas canvas = new FigureCanvas(shell);
Figure page = new Figure();
ToolbarLayout layout = new ToolbarLayout();
// layout.setStretchMinorAxis(false);
// layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
page.setLayoutManager(layout);
for (int i = 0; i < 50; i++) {
StyledLine line = new StyledLine();
if (i % 20 == 0)
line.setFont(BIG);
line.setText("This is line number " + i);
if (i == 10)
typing = line;
page.add(line);
}
canvas.setContents(page);
canvas.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
typing.setText(typing.getText() + e.character);
typing.revalidate();
typing.repaint();
}
public void keyReleased(KeyEvent e) {}
});
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
}
}
使用Swing创建文本绘制应用
本文介绍如何使用Swing库中的SwingUtilities类来获取文本的尺寸,并通过自定义类实现文本绘制功能,最终创建一个可以动态更新并显示文本的应用程序。
10万+

被折叠的 条评论
为什么被折叠?



