简体   繁体   English

SWT Java:如何在Label控件中更改文本的颜色?

[英]SWT Java: how to change colour of text in Label control?

I know how to change size, style but how can I set colour of text in Label control? 我知道如何更改大小,样式但是如何在Label控件中设置文本颜色? Here is my code so far: 到目前为止,这是我的代码:

Label myLabel = new Label(shell, SWT.NONE);
myLabel.setText("some text that needs to be for example green");
FontData[] fD = myLabel.getFont().getFontData();
fD[0].setHeight(16);
fD[0].setStyle(SWT.BOLD);
myLabel.setFont( new Font(display,fD[0]));

I see there is no colour property in FontData class. 我看到FontData类中没有颜色属性。

Make sure you don't mix SWT and AWT colors, and if you build a Color object, make sure you dispose it. 确保不要混合使用SWT和AWT颜色,如果构建Color对象,请确保将其丢弃。 You want something like: 你想要的东西:

final Color myColor = new Color(getDisplay(), 102, 255, 102);
myLabel.setForeground(color);
myLabel.addDisposeListener(new DisposeListener() {
    public void widgetDisposed(DisposeEvent e)
    {
        myColor.dispose();
    }
});

Or you can just use the built-in system colors: 或者您可以使用内置系统颜色:

myLabel.setForeground(getDisplay().getSystemColor(SWT.COLOR_GREEN));

(Do not dispose the system colors.) (不要丢弃系统颜色。)

myLabel.setForeground(Color fg).

color : The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace. color:Color类用于封装默认sRGB颜色空间中的颜色或ColorSpace标识的任意颜色空间中的颜色。

For more information : see this 有关更多信息: 请参阅此内容

For green it'd be something like : myLabel.setForeground(new org.eclipse.swt.graphics.Color(getDisplay(), 102, 255, 102)); 对于绿色,它类似于: myLabel.setForeground(new org.eclipse.swt.graphics.Color(getDisplay(), 102, 255, 102));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM