How would I go about styling a SWT label created along the following lines so it is displayed italicised?
Label label = formToolkit.createLabel(composit开发者_如何学JAVAe, "My label name");
Create a new Font object.
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Label label = new Label(shell, SWT.NONE);
label.setText("I am italic");
FontData fontData = label.getFont().getFontData()[0];
Font font = new Font(display, new FontData(fontData.getName(), fontData
.getHeight(), SWT.ITALIC));
label.setFont(font);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
font.dispose();
display.dispose();
It would be better to use FontRegistry
class from JFaces
, like this:
label.setFont(
JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT)
);
A recent article (February 2014 from Jordi Böhme López) suggest another way to get the current font in order to modify it:
it’s like getting the blueprint of the default font, making some changes and building a new font with the modified blueprint:
Label label = new Label(parent, SWT.NONE);
FontDescriptor descriptor = FontDescriptor.createFrom(label.getFont());
// setStyle method returns a new font descriptor for the given style
descriptor = descriptor.setStyle(SWT.BOLD);
label.setFont(descriptor.createFont(label.getDisplay));
label.setText("Bold Label");
The below code should work:
Label lblSample = new Label(shell, SWT.BORDER_SOLID);
lblSample.setFont(new org.eclipse.swt.graphics.Font(null, "Times New Roman", 12, SWT.BOLD | SWT.ITALIC));
lblSample.setText("Enter Text Here");
精彩评论