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

4
我正在升级一个RCP应用程序以使用Eclipse 4.2.1。我遇到的问题之一是工具栏中文本的对齐方式已更改。
我可以使用以下摘录重现此问题,该摘录改编自标准SWT snippet。它只是创建了一个带有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 ();
  }
}

1
这个问题只出现在Linux上,并已经被提出,网址为https://bugs.eclipse.org/bugs/show_bug.cgi?id=397880。 - Emily M
1个回答

0

我刚在Eclipse 4.2.1上尝试了一下,这是我得到的结果:

enter image description here

一切运作正常。

你正在使用哪个版本的SWT?


1
谢谢您的回复。我正在使用SWT版本4.2.1。我现在相信这是特定于平台的 - 我正在Linux上开发,但已确认在Windows上不会出现这种情况。 - Emily M
@EmilyM 我会在家里的linux上检查并汇报。 - Baz
@EmilyM 好的,我可以确认这是平台相关的。在Windows上可以工作,在带有SWT 4.2.1的Linux上无法工作。但我没有解决方案给你。也许你可以提交一个错误报告 - Baz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接