Java SWT:在工具提示中显示绘画

Java SWT: show paintings in tooltip

我想在 Java SWT 中为 StyledText 设置工具提示。这个想法是显示鼠标点击文本的信息。到目前为止,我只能在工具提示中显示文本,但我希望整个工具提示是一个图像,使用 SWT 中的绘图方法绘制,以显示信息中的一些结构。 Tooltip 似乎无法处理这个问题,所以有人可以建议我如何做吗,或者我可以使用哪个小部件来实现它?

编辑:如何设置 createToolTipContentArea returns 的合成尺寸?

protected Composite createToolTipContentArea(Event event, Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.addPaintListener(new SPPaintListener());
composite.setSize(680, 600);

return composite;
}
private class SPPaintListener implements PaintListener {

public void paintControl(PaintEvent e) {

  drawLyrics(e);
  e.gc.dispose();
}
}

private void drawLyrics(PaintEvent e) {
e.gc.setAntialias(SWT.ON);

Font font = new Font(e.display, "Purisa", 10, SWT.NORMAL);
Color color = new Color(e.display, 25, 25, 25);

e.gc.setForeground(color);
e.gc.setFont(font);

e.gc.drawText("em so transitory", 20, 30);
e.gc.drawText("em so transitory", 180, 30);
e.gc.drawText("Theermanent one", 20, 60);
e.gc.drawText("Theermanent one", 180, 60);

e.gc.drawLine(10, 30, 15, 30);

font.dispose();
}

如前所述,我需要一些合成图,但无法完全显示。

您可以使用从 org.eclipse.jface.window.ToolTip 派生的 class 在工具提示中绘制您喜欢的任何内容。

public class MyToolTip extends ToolTip
{
  public MyToolTip(Control control)
  {
    super(control, NO_RECREATE, false);
  }

  @Override
  protected Composite createToolTipContentArea(final Event event, final Composite parent)
  {
    // TODO add your controls here
  }
}

搭配使用:

new MyToolTip(control);

其中 control 是应该有工具提示的控件。

我已经展示了整个控件的工具提示区域相同的最简单形式。您还可以覆盖 getToolTipArea 方法以根据控件中的位置更改工具提示。

对于图像,createToolTopArea 可能如下所示:

@Override
protected Composite createToolTipContentArea(final Event event, final Composite parent)
{
  Composite body = new Composite(parent, SWT.NONE);

  body.setLayout(new GridLayout());

  Label label = new Label(body, SWT.LEAD);

  Image image = ... your image

  label.setImage(image);

  return body;
}