java 将 SWT 标签样式设置为斜体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1830058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:09:12  来源:igfitidea点击:

Styling a SWT label to be italic

javaswt

提问by Simon Lieschke

How would I go about styling a SWT label created along the following lines so it is displayed italicised?

我将如何设置按照以下几行创建的 SWT 标签的样式,使其显示为斜体?

Label label = formToolkit.createLabel(composite, "My label name");

回答by McDowell

Create a new Fontobject.

创建一个新的Font对象。

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

回答by Esteve

It would be better to use FontRegistryclass from JFaces, like this:

最好使用FontRegistryclass from JFaces,如下所示:

label.setFont(
    JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT)
);

回答by VonC

A recent article(February 2014 from Jordi B?hme López) suggest another way to get the current font in order to modify it:

一个最近的一篇文章(从2014年2月?霍尔迪乙HME洛佩斯)建议另一种方式来获得当前字体,以便修改:

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");

回答by Cjo

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");