Bad text alignment in SWT Label
I am trying to do something that should be absolutely trivial, namely add a product version number to an RCP application's splash screen. So my code goes like this:
class ARMSplashHandler extends EclipseSplashHandler {
private Font font;
private int heightInPx = 12;
private开发者_运维百科 int fontHeightInPt(Display display, int heightInPx) {
return 72 * heightInPx / display.getDPI().y;
}
@Override void init(Shell splash) {
Product product = Platform.getProduct();
Display display = splash.getDisplay();
FontData fontData = splash.getFont.getFontData()[0];
fontData.setHeight(fontHeightInPt(display, fontHeightInPx));
font = new Font(display, fontData);
splash.setBackgroundMode(SWT.INHERIT_DEFAULT);
splash.setText(product.getName());
splash.setFont(font);
String version = "v"+product.getDefiningBundle().getHeaders().get("Bundle-Version");
String versionLocString = product.getProperty("versionLocation");
String versionColorString = product.getProperty("versionColor");
Rectangle versionLoc = StringConverter.asRectangle(versionLocString, new Rectangle(405, 260, 45, 12));
Color versionColor = new Color(display, StringConverter.asRGB(versionColorString, new RGB(193, 202, 212)));
CLabel versionLabel = new CLabel(splash, SWT.CENTER | SWT.BORDER);
versionLabel.setForeground(versionColor);
versionLabel.setFont(font);
versionLabel.setBounds(versionLoc);
versionLabel.setText(version);
splash.addDisposeListener(new DisposeListener {
void widgetDisposed(DisposeEvent e) {
versionColor.dispose();
font.dispose();
}
});
super.init(splash);
}
This works fine (except for a problem on Mac I'll ask about in a separate question; of course, the border is just for debugging):
But if I replace CLabel
with Label
, this happens:
It isn't centered, as expected. If I set alignment to RIGHT
(which is what I really want), it disappears completely. Initially I thought the problem simply was that there isn't enough space, but increasing height of the rectangle doesn't fix the problem, and the width is certainly large enough:
Is this somehow expected behavior? Or an SWT bug (I certainly don't expect there to be one in something this basic)?
精彩评论