如何左对齐 SWT ToolItem 中的文本?

2024-01-03

我正在升级 RCP 应用程序以使用 Eclipse 4.2.1。我遇到的问题之一是工具栏中文本的对齐方式发生了变化。

我可以使用以下片段重现该问题,该片段改编自标准SWT 片段 http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet36.java。它只是创建一个带有 3 个 ToolItem 的垂直 ToolBar。每个项目都有一个图像和一些文本。

当我在 Eclipse 3.7.2 中运行此代码片段时,每个 ToolItem 的文本都是左对齐的。在 Eclipse 4.2.1 中,它是中心对齐的。我希望文本保持左对齐。将样式设置为 SWT.LEFT 没有帮助。有什么建议么?

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class Snippet36 {

  public static void main (final String [] args) {
    Display display = new Display();
    Image image = new Image (display, 16, 16);
    Color color = display.getSystemColor (SWT.COLOR_RED);
    GC gc = new GC (image);
    gc.setBackground (color);
    gc.fillRectangle (image.getBounds ());
    gc.dispose ();
    Shell shell = new Shell (display);
    ToolBar toolBar = new ToolBar (shell, SWT.FLAT | SWT.BORDER | SWT.VERTICAL | SWT.RIGHT);
    Rectangle clientArea = shell.getClientArea ();
    toolBar.setLocation (clientArea.x, clientArea.y);
    String[] labels = new String[] {"Short", "Quite a bit longer", "OK"};
    for (int i=0; i<3; i++) {
      ToolItem item = new ToolItem (toolBar, SWT.PUSH);
      item.setText(labels[i]);
      item.setImage (image);
    }
    toolBar.pack ();
    shell.open ();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch ()) {
        display.sleep ();
      }
    }
    image.dispose ();
    display.dispose ();
  }
}

刚刚用 Eclipse 4.2.1 尝试过,这就是我得到的:

一切正常。

您使用的是哪个版本的 SWT?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何左对齐 SWT ToolItem 中的文本? 的相关文章

随机推荐