问题
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 ();
}
}
回答1:
Just tried it with Eclipse 4.2.1 and this it what I get:

Everything works fine.
Which version of SWT are you using?
来源:https://stackoverflow.com/questions/13514603/how-to-left-align-text-in-a-swt-toolitem