如何在SWT ToolItem中左对齐文本? [英] How to left-align text in a SWT ToolItem?

查看:137
本文介绍了如何在SWT ToolItem中左对齐文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我可以使用以下代码片段重现问题,从标准 SWT片段。它只需创建一个具有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 = new Display();
图像=新图像(显示,16,16);
颜色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,有点长,OK}; (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 ?


I am upgrading an RCP application to use Eclipse 4.2.1. One of the problems I am having is that the alignment of text in toolbars has changed.

I can reproduce the issue with the following snippet, adapted from a standard SWT snippet. It simply creates a vertical ToolBar with 3 ToolItems. Each item has an image and some text.

When I run this snippet in Eclipse 3.7.2, the text of each ToolItem is left-aligned. In Eclipse 4.2.1, it is centre-aligned. I would like the text to remain left-aligned. Setting the style to SWT.LEFT does not help. Any suggestions?

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 ();
  }
}

解决方案

Just tried it with Eclipse 4.2.1 and this it what I get:

Everything works fine.

Which version of SWT are you using?

这篇关于如何在SWT ToolItem中左对齐文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆