【问题标题】:How to left-align text in a SWT ToolItem?如何在 SWT ToolItem 中左对齐文本?
【发布时间】:2012-11-10 23:08:03
【问题描述】:

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

我可以使用以下 sn-p 重现该问题,改编自标准 SWT snippet。它只是创建一个带有 3 个 ToolItems 的垂直 ToolBar。每个项目都有一个图像和一些文本。

当我在 Eclipse 3.7.2 中运行这个 sn-p 时,每个 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 ();
  }
}

【问题讨论】:

标签: java eclipse swt


【解决方案1】:

刚刚在 Eclipse 4.2.1 上尝试过,这就是我得到的结果:

一切正常。

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

【讨论】:

  • 感谢您的回复。我正在使用 SWT 4.2.1 版。我现在认为这是特定于平台的 - 我正在 Linux 上开发,但已确认这不会在 Windows 上发生。
  • @EmilyM 我会在家里检查 linux 并报告。
  • @EmilyM 好的,我可以确认,它依赖于平台。适用于 Windows,不适用于带有 SWT 4.2.1 的 Linux。不过,没有给你解决方案。也许file a bug
猜你喜欢
  • 2014-06-07
  • 2022-11-01
  • 2015-02-12
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多